Wkwebview Basic Use

Source: Internet
Author: User

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

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.