In WWDC2014, Apple has introduced the latest IOS8 system, which is accompanied by a number of control updates and upgrades. One of the new WebKit libraries is very exciting. This paper will also explain the new features and usage of the updated Wkwebview control in WebKit, which solves many problems such as the memory and loading speed of UIWebView.
Environmental information:
Mac OS X 10.10.1
Xcode 6.1.1
IOS 8.1
Text: First, Wkwebview new characteristics in performance, stability, functional aspects have a great promotion (the most intuitive embodiment is to load the page is occupied by memory, simulator load Baidu and open source China site, Wkwebview occupy 23M, and UIWebView occupy 85M) Allow JavaScript nitro libraries to load and use (UIWebView), support more HTML5 features, up to 60fps of scrolling refresh rate, and built-in gestures; The uiwebviewdelegate and UIWebView are made up of 14 classes with 3 protocols (see Apple's Official document); two, initialize 1. First, we need to introduce the WebKit library
#import <WebKit/WebKit.h>
2. The initialization method is divided into the following two kinds
Default initialization
-(Instancetype) initWithFrame: (CGRect) frame;
Initialize based on the configuration of the WebView
-(Instancetype) initWithFrame: (CGRect) frame configuration: (Wkwebviewconfiguration *) Configuration Ns_designated_initializer;
3. Load Web page and HTML code in the same way as UIWebView, the code is as follows:
Wkwebview *webview = [[Wkwebview alloc] initWithFrame:self.view.bounds];
[WebView loadrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"]];
[Self.view Addsubview:webview];
Third, the Wkwebview agent method 1. Wknavigationdelegate
The method provided by this agent can be used to track the loading process (the page starts loading, loading completed, loading failed), and decides whether to perform a jump.
Call when page starts loading
-(void) WebView: (Wkwebview *) WebView didstartprovisionalnavigation: (wknavigation *) navigation;
Called when the content begins to return
-(void) WebView: (Wkwebview *) WebView didcommitnavigation: (wknavigation *) navigation;
Call
-(void) WebView: (Wkwebview *) WebView didfinishnavigation: (wknavigation *) navigation after page loading completes;
Call when page load fails
-(void) WebView: (Wkwebview *) WebView didfailprovisionalnavigation: (wknavigation *) navigation;
Page Jump Proxy method has three kinds, divides into (receives jumps and decides whether jumps two kinds)
Call
-(void) WebView: (Wkwebview *) WebView didreceiveserverredirectforprovisionalnavigation after receiving a server jump request: ( Wknavigation *) navigation;
After receiving the response, decide whether to jump
-(void) WebView: (Wkwebview *) WebView decidepolicyfornavigationresponse: (Wknavigationresponse * ) Navigationresponse Decisionhandler: (void (^) (wknavigationresponsepolicy)) Decisionhandler;
Before sending the request, decide whether to jump
-(void) WebView: (Wkwebview *) WebView decidepolicyfornavigationaction: (wknavigationaction *) Navigationaction Decisionhandler: (void (^) (wknavigationactionpolicy)) Decisionhandler;
2. Wkuidelegate
Create a new WebView
-(Wkwebview *) WebView: (Wkwebview *) WebView createwebviewwithconfiguration: ( Wkwebviewconfiguration *) configuration fornavigationaction: (wknavigationaction *) navigationaction windowFeatures: ( Wkwindowfeatures *) Windowfeatures;
The remaining three agent methods are all related to the pop-up prompt box, for the Web interface three kinds of balloon (warning box, confirmation box, input box) correspond to three proxy methods respectively. The following is just an example of a warning box.
/**
* Web interface has a pop-up warning box called
*
* @param webview Implementation of the proxy webview
* @param Message The contents of the Warning box
* @param frame main Window
* @param completionhandler warning box disappear call
/-(void) WebView: (Wkwebview *) WebView runjavascriptalertpanelwithmessage: (NSString *) message initiatedbyframe: (void (^) ()) Completionhandler;
3. Wkscriptmessagehandler
This protocol contains a method that must be implemented, which is the key to increasing the interaction between the app and the web side, and it can directly convert the received JS script to OC or Swift object. (Of course, the UIWebView can also interact with the web through the "curve salvation" approach, the famous Cordova framework is this mechanism)
Called when a script is received from the Web interface
-(void) Usercontentcontroller: (Wkusercontentcontroller *) Usercontentcontroller Didreceivescriptmessage: (wkscriptmessage *) message;
four, Wkwebview load JS
Picture scaling JS Code
nsstring *js = @ "var count = Document.images.length;for (var i = 0; i < count; i++) {var image = Docum ent.images[i];image.style.width=320;}; Window.alert (' find ' + count + ' map '); ";
Initializes the Wkuserscript object according to the JS string
wkuserscript *script = [[Wkuserscript alloc] Initwithsource:js injectiontime: Wkuserscriptinjectiontimeatdocumentend Formainframeonly:yes];
Initializes the wkwebviewconfiguration
wkwebviewconfiguration *config = [[wkwebviewconfiguration] According to the generated Wkuserscript object. ALLOC] init];
[Config.usercontentcontroller Adduserscript:script];
_webview = [[Wkwebview alloc] initWithFrame:self.view.bounds configuration:config];
[_webview loadhtmlstring:@ "
Five, this article demo download
? https://github.com/saitjr/WKWebViewSimpleDemo.git VI, on the loading of JS or webview other use tips, you can view the article:
Using Safari to debug a WebView
WebView Load HTML image size adaptive with article wrapping
Resources:
http://nshipster.cn/wkwebkit/
Http://www.cocoachina.com/webapp/20141121/10277.html
http://blog.csdn.net/cyforce/article/details/37657009