New features and use of Ios-wkwebview

Source: Internet
Author: User

WebKit solves many problems such as memory, loading speed and so on UIWebView exist.

First, Wkwebview new features
在性能、稳定性、功能方面有很大提升(最直观的体现就是加载网页是占用的内存,模拟器加载百度与开源中国网站时,WKWebView占用23M,而UIWebView占用85M);允许JavaScript的Nitro库加载并使用(UIWebView中限制);支持了更多的HTML5特性;高达60fps的滚动刷新率以及内置手势;将UIWebViewDelegate与UIWebView重构成了14类与3个协议(查看苹果官方文档);
Second, initialize
    1. First you need to introduce the WebKit library

    import <WebKit/WebKit.h>
  1. The initialization method is divided into the following two types

     // 默认初始化 - (instancetype)initWithFrame:(CGRect)frame; // 根据对webview的相关配置,进行初始化 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
  2. The Web page and HTML code are loaded in the same way as UIWebView, with the following code:

     WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]]; [self.view addSubview:webView];
Three, the agent method of Wkwebview
    1. Wknavigationdelegate

The agent provides methods that can be used to track the loading process (the page starts loading, loading completes, loading fails), and decides whether to perform a jump.

//page starts to load when called-(void) WebView: (Wkwebview *) WebView didstartprovisionalnavigation: ( Wknavigation *) navigation;//called when content starts to return-(void) WebView: (Wkwebview *) WebView didcommitnavigation: (wknavigation *) navigation;//page load is complete call-(void) WebView: (Wkwebview *) WebView didfinishnavigation: (wknavigation *) navigation;// Page load failed when called-(void) WebView: (Wkwebview *) WebView didfailprovisionalnavigation: (wknavigation *) navigation;  

page jump Agent method has three, divided into (receive jump and decide whether to jump two kinds)

//received a server jump request after the call-(void) WebView: (Wkwebview *) WebView Didreceiveserverredirectforprovisionalnavigation: (wknavigation *) navigation;//after receiving the response, decide whether to jump-(void) WebView: ( Wkwebview *) WebView decidepolicyfornavigationresponse: (wknavigationresponse *) Navigationresponse DecisionHandler: ( void (^) (wknavigationresponsepolicy)) decisionhandler;//decides whether to jump-(void) WebView before sending a request: (Wkwebview *) WebView Decidepolicyfornavigationaction: (wknavigationaction *) navigationaction Decisionhandler: (void (^) ( Wknavigationactionpolicy)) Decisionhandler; 
  1. Wkuidelegate

     // 创建一个新的WebView - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

The remaining three agent methods are all related to the interface pop-up prompt box, for the Web interface three kinds of prompt box (warning box, confirmation box, input box) corresponding to three agent methods. Here are just a few examples of warning boxes.

/** * Web interface has pop-up warning box when called * * @param webView Implement the proxy WebView * @param message warning Box Contents * @param frame main window * @param completionhandler warning box disappears call */-(void) WebView: (Wkwebview *) WebView RUNJAVASC Riptalertpanelwithmessage: (NSString *) message initiatedbyframe: (void (^) ()) Completionhandler;  
    1. wkscriptmessagehandler

This protocol contains a method that must be implemented, which is the key to improving the interaction between the app and the Web, and it can convert the received JS script directly to OC or Swift object. (Of course, UIWebView can also interact with the web through the "curve salvation" approach, as the famous Cordova Framework is)

//called when a script is received from the Web interface-(void) Usercontentcontroller: (Wkusercontentcontroller *) Usercontentcontroller didreceivescriptmessage: (wkscriptmessage *) message;  

Four, Wkwebview load JS

Image scaling js code nsstring *JS = @ "var count = Document.images.length;for (var i = 0; i < count; i++) {var image = Document.i mages[i];image.style.width=320;}; Window.alert (' find ' + count + ' map '); /Initialize the Wkuserscript object according to the JS string wkuserscript *script = [[Wkuserscript alloc] Initwithsource:js injectiontime: Wkuserscriptinjectiontimeatdocumentend formainframeonly:yes];//based on the generated Wkuserscript object, Initialize wkwebviewconfigurationwkwebviewconfiguration *config = [[Wkwebviewconfiguration alloc] init]; [Config.usercontentcontroller Adduserscript:script];_webview = [[Wkwebview alloc] InitWithFrame:self.view.bounds Configuration:config]; [_webview loadhtmlstring:@ "

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

New features and use of Ios-wkwebview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.