WKWebView的使用心得

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

@interface ViewController : UIViewController<WKNavigationDelegate,WKScriptMessageHandler,WKUIDelegate>


@end

.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *controller = [[WKUserContentController alloc] init];
    
    [controller addScriptMessageHandler:self name:@"observe"];
    configuration.userContentController = controller;
    
    NSURL *jsUrl = [NSURL URLWithString:@"file:///Users/zhengzeqiang/Desktop/test.html"];
    
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
    
    //打开左划回退功能
    _webView.allowsBackForwardNavigationGestures =YES;
    
    _webView.navigationDelegate = self;
    
    _webView.UIDelegate = self;
    
    [_webView loadRequest:[NSURLRequest requestWithURL:jsUrl]];
    
    [self.view addSubview:_webView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - WKScriptMessageHandler

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    // Check to make sure the name is correct
    if ([message.name isEqualToString:@"observe"]) {
        // Log out the message received
        NSLog(@"Received event %@", message.body);
        
        NSString *sendCode = @"goToHtml();";
        [_webView evaluateJavaScript:sendCode completionHandler:nil];
    }
}

#pragma mark - WKNavigationDelegate
//根据webView、navigationAction相关信息决定这次跳转是否可以继续进行,这些信息包含HTTP发送请求,如头部包含User-Agent,Accept
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    decisionHandler(WKNavigationActionPolicyAllow);
    
        NSLog(@"%@", navigationAction.request.URL);
    
    switch (navigationAction.navigationType) {
        case WKNavigationTypeBackForward: {
            
            NSLog(@"WKNavigationTypeBackForward");
        }
            break;
        case WKNavigationTypeFormResubmitted: {
            
            NSLog(@"WKNavigationTypeFormResubmitted");
        }
            break;
        case WKNavigationTypeFormSubmitted: {
            
            NSLog(@"WKNavigationTypeFormSubmitted");
        }
            break;
        case WKNavigationTypeLinkActivated: {
            
            NSLog(@"WKNavigationTypeLinkActivated");
        }
            break;
        case WKNavigationTypeOther: {
            
            NSLog(@"WKNavigationTypeOther");
        }
            break;
        case WKNavigationTypeReload: {
            
            NSLog(@"WKNavigationTypeReload");
        }
            break;
            
        default:
            break;
    }
    
}

@end
</pre><pre name="code" class="objc">.html
<pre name="code" class="html"><html>
<head>
    <meta charset="UTF-8">
    <style>
        body{text-align:center}
        .buttonClass {
 			height: 100px;
 			width: 500px;
 			font-size:50px;
 		}
    </style>
</head>
<body>
	<font size = >
<br>
<br>
<br>
<br>
<input type="button" value="发送命令" class="buttonClass" style="height=20px;width=100px;" οnclick="postMyMessage()"/>
<br>
<script language="javascript" type="text/javascript">

function postMyMessage()
 {

    var message = { 'message' : 'Hello, World!', 'numbers' : [ 1, 2, 3 ] };

    window.webkit.messageHandlers.observe.postMessage(message);

}
function goToHtml(){
<span style="white-space:pre">	</span>//do something!
}
</script>
</body>
</html>


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值