WKWebView基本使用

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 *)navigation
23 {
24     NSLog(@"当页面开始加载时");
25 }
26 -(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
27 {
28     NSLog(@"当内容开始返回时调用");
29 }
30 -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
31 {
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 *)error
39 {
40     NSLog(@"页面加载失败时调用");
41 }
42 //页面跳转的代理方法
43 -(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
44 {
45     NSLog(@" 接收到服务器跳转请求之后调用");
46 }
47 -(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
48 {
49     NSLog(@"在收到响应后,决定是否跳转");
50     NSLog(@"%@",navigationResponse.response);
51     decisionHandler(WKNavigationResponsePolicyAllow);
52 
53 }
54 -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
55 {
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 }

 

转载于:https://www.cnblogs.com/kfgcs/p/6386336.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值