WKWebView+webkit

2017-03-28  本文已影响10人  靠北的北

发布于:http://he8090.cn/2017/03/28/WKWebView-webkit/

近年来由于混合编程的发展趋势,越来越多的企业选择通过Native + Hybrid方式来减轻开发压力和减少开发成本 。 iOS8以后,Apple新推出了WKWebview来替换UIWebView。

WKWebview通过WKWebViewConfiguration注册可以被Js调用的方法。

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = [WKUserContentController new];
[configuration.userContentController addScriptMessageHandler:self name:@"setUserInfo"];
[configuration.userContentController addScriptMessageHandler:self name:@"copyToClipboard"];
[configuration.userContentController addScriptMessageHandler:self name:@"alertScanView"];

同时在代理WKScriptMessageHandler中获取Js调用的方法名字

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
}

Native调用Js (使用字符串拼接Js代码串)

NSString *action = [NSString stringWithFormat:@"%@('%@','')",actionName,result];
    [self.webView evaluateJavaScript:action completionHandler:^(id _Nullable obj, NSError * _Nullable error) {      
    }];
上一篇下一篇

猜你喜欢

热点阅读