SoFunction
Updated on 2025-03-04

iOS development WKWebViewJavascriptBridge solution resulting in crash in Xcode9

Preface

This article mainly introduces to you the relevant solutions to the crash in iOS WKWebViewJavascriptBridge Xcode9. I will share it for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

The third party WKWebViewJavascriptBridge is quite good, but recently crash appeared on Xcode9; after watching the official github of WKWebViewJavascriptBridge, everyone has this problem, and finally solved it;

It needs to be modified as follows in the WKWebViewJavascriptBridge class:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
 if (webView != _webView) { return; }
 NSURL *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 {
  /// Added else here  if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
   [_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
  } else {
   decisionHandler(WKNavigationActionPolicyAllow);
  }
 }
} 

Or modify it like this

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
 if (webView != _webView) { return; }
 NSURL *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);
  /// Added a new return here  return;
 }

 if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
  [_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
 } else {
  decisionHandler(WKNavigationActionPolicyAllow);
 }
}

I guess the main reason is on Xcode9,decisionHandler() This block callback does not allow multiple calls;

Summarize

The above is the entire content of this article. There are still many shortcomings in this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.

Reference link

1、 /marcuswestin/WebViewJavascriptBridge/issues/302

2、/p/909afcbc401e