美文网首页
iOS开发 WKWebViewJavascriptBridge

iOS开发 WKWebViewJavascriptBridge

作者: 开发仔小广 | 来源:发表于2017-09-25 14:12 被阅读73次

    WKWebViewJavascriptBridge 这个第三方还是比较不错的,但是最近Xcode9上,却出现了crash;WKWebViewJavascriptBridge官方github看了大家也都有如此问题,最后终于解决了;需要在WKWebViewJavascriptBridge类里,如下修改:

    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        if (webView != _webView) { return; }
        NSURL *url = navigationAction.request.URL;
        __strong typeof(_webViewDelegate) strongDelegate = _webViewDelegate;
    
        if ([_base isWebViewJavascriptBridgeURL:url]) {
            if ([_base isBridgeLoadedURL:url]) {
                [_base injectJavascriptFile];
            } else if ([_base isQueueMessageURL:url]) {
                [self WKFlushMessageQueue];
            } else {
                [_base logUnkownMessage:url];
            }
            decisionHandler(WKNavigationActionPolicyCancel);
            
        } else {
            if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
                [_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
            } else {
                decisionHandler(WKNavigationActionPolicyAllow);
            }
        }
    }
    
    

    或这样修改

    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        if (webView != _webView) { return; }
        NSURL *url = navigationAction.request.URL;
        __strong typeof(_webViewDelegate) strongDelegate = _webViewDelegate;
    
        if ([_base isWebViewJavascriptBridgeURL:url]) {
            if ([_base isBridgeLoadedURL:url]) {
                [_base injectJavascriptFile];
            } else if ([_base isQueueMessageURL:url]) {
                [self WKFlushMessageQueue];
            } else {
                [_base logUnkownMessage:url];
            }
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
        
        if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
            [_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
        } else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }
    

    我猜测主要原因是 Xcode9上,decisionHandler() 这个block回调,不允许多次被调用;
    参考链接
    1、 https://github.com/marcuswestin/WebViewJavascriptBridge/issues/302
    2、http://www.jianshu.com/p/909afcbc401e

    相关文章

      网友评论

          本文标题:iOS开发 WKWebViewJavascriptBridge

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