当前位置:主页 > 移动开发 > iOS代码 >

iOS开发之WKWebViewJavascriptBridge Xcode9中导致crash的解决

时间:2021-08-21 09:11:14 | 栏目:iOS代码 | 点击:

前言

本文主要给大家介绍了关于iOS WKWebViewJavascriptBridge Xcode9中导致crash的相关解决办法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

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 {
  /// 在这新添加了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
  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

您可能感兴趣的文章:

相关文章