美文网首页
WKWebView+webkit

WKWebView+webkit

作者: 靠北的北 | 来源:发表于2017-03-28 10:27 被阅读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) {      
        }];
    

    相关文章

      网友评论

          本文标题:WKWebView+webkit

          本文链接:https://www.haomeiwen.com/subject/uqxjottx.html