WKWebView is a browser view that was added in iOS8
New features
High performance, good stability, relatively small memory consumption support JS interaction support HTML5 new features can add progress bar support built-in gesture is said to be up to 60fps refresh rate
1. Import the WebKit library
#import <WebKit/WebKit.h>
Copy the code
######2. Abide by the agreement
WKNavigationDelegate The agent provides methods that can be used to track the loading process (the page starts loading, the page finishes loading, the page fails loading) and determine whether to perform a jump.
The proxy method of WKUIDelegate is basically related to the pop-up prompt box on the interface. The three prompt boxes (warning box, confirmation box, input box) on the Web interface correspond to three proxy methods respectively. The other thing is to create a new WebView
######3. Initialize
// default initialization - (instancetype)initWithFrame:(CGRect)frame; // According to the webView configuration, Initialize - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;Copy the code
Whether WKWebView AllowsBackForwardNavigationGestures attributes can be left to return to
You load a web page or HTML the same way you load a UIWebView
######4. WKNavigationDelegate tracks the loading process
/ / start page load when the - (void) webView: (WKWebView *) webView didStartProvisionalNavigation (WKNavigation *) navigation; // call - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation; // Call - (void)webView (WKWebView *)webView didFinishNavigation (WKNavigation *)navigation; / / page load when the - (void) failure webView: (WKWebView *) webView didFailProvisionalNavigation (WKNavigation *) navigation;Copy the code
Page jump
/ jump/receiving server request then perform - (void) webView: (WKWebView *) webView didReceiveServerRedirectForProvisionalNavigation: (WKNavigation *)navigation; // After receiving the response, Deciding whether to jump - (void) webView: (WKWebView *) webView decidePolicyForNavigationResponse: (navigationResponse WKNavigationResponse *) decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler; // Before sending the request, Deciding whether to jump - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (navigationAction WKNavigationAction *) decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;Copy the code
# # # # # # 5, WKUIDelegate
/ / create a new WebView - (WKWebView *) WebView: (WKWebView *) WebView createWebViewWithConfiguration: (WKWebViewConfiguration *)configurationforNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
Copy the code
Interact with JS
/ / show a JS Alert (interact with JS) - (void) webView: (WKWebView *) webView runJavaScriptAlertPanelWithMessage message: (nsstrings *) initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler; / / pop up a input box (with JS interaction) - (void) webView: (WKWebView *) webView runJavaScriptTextInputPanelWithPrompt prompt: (nsstrings *) defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler; / / show a confirmation dialog (JS) - (void) webView: (WKWebView *) webView runJavaScriptConfirmPanelWithMessage message: (nsstrings *) initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler; //WebVeiw closed (new method in 9.0) - (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);Copy the code
# # # # # # 6, WKScriptMessageHandler
This protocol contains a method that must be implemented. This method is the key to improve the interaction between App and Web side. It can directly convert the JS scripts received into OC or Swift objects.
- (void)userContentController:(WKUserContentController *)userContentController is called when a script is received from the web interface didReceiveScriptMessage:(WKScriptMessage *)message;Copy the code
######7, easy to come across the problem, click on the link does not respond
If the jump mode of some HTML page tags is
<a href = "xxx" target = "_black">
Copy the code
So the way that clicking on the link doesn’t load “_black” is to open a new page, just like in Safari a new page pops up. The solution is to reload and implement the following proxy approach
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if(navigationAction targetFrame = = nil) {/ / reload a new page [webView loadRequest: navigationAction. Request]; } decisionHandler(WKNavigationActionPolicyAllow); }Copy the code
Or call Safari to open the link