WKWebView基本使用

來源:互聯網
上載者:User

標籤:abs   nis   int   hao123   and   off   com   服務   方法   

WKWebView的基本使用和幾個基本的代理方法

 1 #import "ViewController.h" 2 #import <WebKit/WebKit.h> 3 @interface ViewController ()<WKNavigationDelegate,WKUIDelegate> 4 @property(nonatomic,strong)WKWebView *webView; 5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad {10     [super viewDidLoad];11     UIImage *bgImage = [UIImage imageNamed:@"圓角矩形"];12     [self.navigationController.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];13     [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];14     // Do any additional setup after loading the view, typically from a nib.15     self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];16     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.hao123.com"]]];17     self.webView.navigationDelegate = self;18     self.webView.allowsBackForwardNavigationGestures = YES;19     [self.view addSubview:self.webView];20 }21 22 -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation23 {24     NSLog(@"當頁面開始載入時");25 }26 -(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation27 {28     NSLog(@"當內容開始返回時調用");29 }30 -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation31 {32     NSLog(@"頁面載入完成之後調用");33     [_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {34         NSLog(@"abc:%f",[result doubleValue]);35     }];36 37 }38 -(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error39 {40     NSLog(@"頁面載入失敗時調用");41 }42 //頁面跳轉的代理方法43 -(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation44 {45     NSLog(@" 接收到伺服器跳轉請求之後調用");46 }47 -(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler48 {49     NSLog(@"在收到響應後,決定是否跳轉");50     NSLog(@"%@",navigationResponse.response);51     decisionHandler(WKNavigationResponsePolicyAllow);52 53 }54 -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler55 {56     NSLog(@"在發送請求之前,決定是否跳轉");57     NSLog(@"%@",navigationAction.request.URL.absoluteString);58     if ([navigationAction.request.URL.absoluteString rangeOfString:@"https://www.baidu.com"].location != NSNotFound) {59         decisionHandler(WKNavigationActionPolicyAllow);60     }else{61         decisionHandler(WKNavigationActionPolicyAllow);62     }63 }

 

WKWebView基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.