Basic use of Wkwebview and several basic proxy methods
1 #import "ViewController.h"2 #import<WebKit/WebKit.h>3 @interfaceViewcontroller () <WKNavigationDelegate,WKUIDelegate>4@property (nonatomic,strong) Wkwebview *WebView;5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; OneUIImage *bgimage = [UIImage imagenamed:@"Rounded Rectangle"]; A [Self.navigationController.navigationBar setbackgroundimage:bgimage forbarmetrics:uibarmetricsdefault]; - [Self.navigationController.navigationBar setbackgroundcolor:[uicolor clearcolor]; - //additional setup after loading the view, typically from a nib. theSelf.webview =[[Wkwebview alloc] initWithFrame:self.view.bounds]; -[Self.webview loadrequest:[nsurlrequest Requestwithurl:[nsurl urlwithstring:@"http://www.hao123.com"]]]; -Self.webView.navigationDelegate =Self ; -Self.webView.allowsBackForwardNavigationGestures =YES; + [Self.view AddSubview:self.webView]; - } + A-(void) WebView: (Wkwebview *) WebView didstartprovisionalnavigation: (wknavigation *) Navigation at { -NSLog (@"When the page starts to load"); - } --(void) WebView: (Wkwebview *) WebView didcommitnavigation: (wknavigation *) Navigation - { -NSLog (@"called when the content starts to return"); in } --(void) WebView: (Wkwebview *) WebView didfinishnavigation: (wknavigation *) Navigation to { +NSLog (@"called after page load is complete"); -[_webview Evaluatejavascript:@"Document.body.offsetHeight"completionhandler:^ (ID_nullable result, Nserror *_nullable Error) { theNSLog (@"abc:%f", [result Doublevalue]); * }]; $ Panax Notoginseng } --(void) WebView: (Wkwebview *) WebView didfailprovisionalnavigation: (null_unspecified wknavigation *) Navigation Witherror: ( Nonnull Nserror *) Error the { +NSLog (@"called when a page load fails"); A } the //Proxy method for page jumps +-(void) WebView: (Wkwebview *) WebView didreceiveserverredirectforprovisionalnavigation: (wknavigation *) Navigation - { $NSLog (@"called after receiving a server jump request"); $ } --(void) WebView: (Wkwebview *) WebView decidepolicyfornavigationresponse: (Wknavigationresponse *) navigationresponse Decisionhandler: (void(^) (wknavigationresponsepolicy)) Decisionhandler - { theNSLog (@"after receiving the response, decide whether to jump"); -NSLog (@"%@", navigationresponse.response);Wuyi Decisionhandler (wknavigationresponsepolicyallow); the - } Wu-(void) WebView: (Wkwebview *) WebView decidepolicyfornavigationaction: (wknavigationaction *) navigationaction Decisionhandler: (void(^) (wknavigationactionpolicy)) Decisionhandler - { AboutNSLog (@"before sending a request, decide whether to jump"); $NSLog (@"%@", navigationAction.request.URL.absoluteString); - if([navigationAction.request.URL.absoluteString rangeofstring:@"https://www.baidu.com"].location! =nsnotfound) { - Decisionhandler (wknavigationactionpolicyallow); -}Else{ A Decisionhandler (wknavigationactionpolicyallow); + } the}
Wkwebview Basic Use