Closed
Description
- (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 isCorrectProcotocolScheme:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
} else if ([_base isQueueMessageURL:url]) {
[self WKFlushMessageQueue];
} else {
[_base logUnkownMessage:url];
}
**decisionHandler(WKNavigationActionPolicyCancel); // executed**
}
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
**[_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler]; // executed**
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
I implement the selector @selector(webView:decidePolicyForNavigationAction:decisionHandler:)
and execute "decisionHandler(WKNavigationActionPolicyAllow);"
It seems the decisionHandler can be called twice and cause the crash.
No such thing has happened before...
Activity
lokimeyburg commentedon Apr 12, 2017
(I updated your issue to make your code render in Markdown... makes it easier to read)
Your conditionals allow for more than one handler to be called. Perhaps try and make the statements mutually exclusive?
yudun1989 commentedon Jul 31, 2017
same issue. I met this problem on iOS 11 devices.
rollingstoneW commentedon Jul 31, 2017
It should be this?
wunshine commentedon Aug 1, 2017
got the same problem and not fix yet
swifterfit commentedon Aug 2, 2017
add
return;
. underdecisionHandler(WKNavigationActionPolicyCancel);
iPermanent commentedon Oct 20, 2017
(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 isCorrectProcotocolScheme:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
} else if ([_base isQueueMessageURL:url]) {
[self WKFlushMessageQueue];
} else {
[_base logUnkownMessage:url];
}
I disabled this code first
//decisionHandler(WKNavigationActionPolicyCancel);
}
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
//Here you need to add some code judge if it's the nav by jsbridge then cancel it,
if ([[URL.absoluteString lowercaseString] hasPrefix:@"https://wvjb_queue_message/"] || [[URL.absoluteString lowercaseString] hasPrefix:@"wvjbscheme://"]) {
decisionHandler(WKNavigationActionPolicyCancel);
}else{
decisionHandler(WKNavigationActionPolicyAllow);
}
the code upon is my way to deal it. Hope can help you fix this problem
[_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler]; // executed**
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
iPermanent commentedon Oct 20, 2017
the code need to be added in your own delegate, and delete the cancel action decisionHandler(WKNavigationActionPolicyCancel); in WKWebViewJavascriptBridge.m
Fix completion duplication
pilot34 commentedon Oct 24, 2017
Hm, why this issue is closed? I think, it is not fixed in the last version.
iPermanent commentedon Oct 24, 2017
I think it's duplicate with the others, the same issues has exist already. It's not fixed at all.
iosfighterlb commentedon Nov 6, 2017
Adding 'return'. Can't solve the problem on me.
It seems some sutiation it doesn't enter : “ if ([_base isWebViewJavascriptBridgeURL:url]) ”
For example , if you only use WKWebView.
i fix this bug with adding these code in Delegate method:
With these, you can compulsively enter IF and execute return .
marcuswestin commentedon Nov 8, 2017
Fixed in latest release (6.0.3)
Dandre126 commentedon Nov 27, 2017
(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (webView != _webView) {
return;
}
NSURL *url = navigationAction.request.URL;
if ([navigationAction.request.URL.scheme.lowercaseString isEqualToString:@"pingchang"]) {
if (![self.jsHelper shouldLoadURLRequestWithURL:navigationAction.request.URL]) {
decisionHandler(WKNavigationActionPolicyCancel);
return; // iOS11或xcode9中,此处必须要加一个return,否则就会报上述错误。加了return就可以解决。
}
}else if ([url.scheme.lowercaseString isEqualToString:@"http"] ||
[url.scheme.lowercaseString isEqualToString:@"https"]) {
_seTipLabel.text = [NSString stringWithFormat:@"此内容由 %@ 提供",url.host];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
housenkui commentedon Mar 20, 2018

Xcode 8.2 gave me an error if i use Iphone, but if i use Simulator that is ok.what should i do?
If i user Xcode 9.2 that do well both in Iphone and Simulator.
iPermanent commentedon Mar 20, 2018
You should declare an interface by protocol at the .h file, so the xcode can recognize this. And by the way, we always use the latest version Xcode to avoid strange problems on iOS, I recommend you to do it as well.
Prathapreddy26 commentedon Jan 8, 2019
What is the purpose of this method?
?