Osheep

时光不回头,当下最重要。

iOS11 Xcode9 WKWebView崩溃问题解决方案

正式版的iOS11&Xcode 9已经发布,乘着版本空档期,赶紧花点时间完成适配工作。

《iOS11 Xcode9 WKWebView崩溃问题解决方案》

iPhone X.png

在用iPhone X 的模拟器进入Hybrid项目时,发现一进去就崩溃,崩溃信息少的可怜:

libc++abi.dylib: terminating with uncaught exception of type NSException

靠这玩意儿肯定是定位不出bug的,不过全局断点还是给出了一点信息:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString *requestString = navigationAction.request.URL.absoluteString;
    //对外链、拨号和跳转appstore做特殊处理
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *url = [navigationAction.request URL];
    //电话
    //此处省略若干业务代码
    if ([url.absoluteString containsString:@"itunes.apple.com"])
    {
        if ([app canOpenURL:url])
        {
            [app openURL:url];
            decisionHandler(WKNavigationActionPolicyCancel);
        }
    }
    if ([requestString hasPrefix:@"easy-js:"]) {
        [self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
    {
        [self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
    }
    decisionHandler(WKNavigationActionPolicyAllow);//崩在这里
}

仍然不知道为啥子崩在这儿?之前一直是没问题的啊??

小Tips:
为了获取一些堆栈信息以便于快准狠的定位问题,可以在main函数里:

int main(int argc, char * argv[]) {
    @try {
        @autoreleasepool
        {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }
    @catch (NSException* exception)
    {
        NSDebugLog(@"Exception=%@\nStack Trace:%@", exception, [exception callStackSymbols]);
    }
}

最终得到一条关键报错:

Completion handler passed to -[WKPrivateNavigationDelegate webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once

意思就是WKWebView的这个代理方法被多次调用了。

if ([requestString hasPrefix:@"easy-js:"]) {
        [self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
    {
        [self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
    }
    decisionHandler(WKNavigationActionPolicyAllow);//崩在这里

简单分析一下被多次调用的原因:
1、系统判断这个方法被多次执行,主要是看decisionHandler()是否被多次执行;
2、由于if判断里会执行decisionHandler(),最后一行代码也会执行decisionHandler(),并且self.realDelegate中也会执行decisionHandler(),这就导致了decisionHandler()这个handler可能会被多次执行。
那解决问题的方向就是修改代码保证WKWebView单次LoadRequest只调一次此代理方法~
修改如下:

    if ([requestString hasPrefix:@"easy-js:"]) {
        [self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    else if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
    {
        [self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }

即保证了单次LoadRequest只执行一次decisionHandler()

参考:https://github.com/marcuswestin/WebViewJavascriptBridge/issues/278

点赞