- 在加载本地Html文件的时候,报了如下的错误:
reason: 'Completion handler passed to -[JavaScriptCallOCViewController webView:decidePolicyForNavigationAction:decisionHandler:] was not called'
错误的原因是: 在方法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
中未实现 decisionHandler(WKNavigationActionPolicyCancel) block 的回调。
代理方法中,未实现block的回调
放开上图中的注释部分就可以了。
// 必须实现decisionHandler的回调,否则就会报错
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
decisionHandler(WKNavigationActionPolicyCancel);
NSLog(@"WKNavigationActionPolicyCancel");
} else {
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"WKNavigationActionPolicyAllow");
}
另外一个代理方法也是,需要实现block的回调,不然也会报错。
这个方法是在加载网络页面的时候调用。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
错误提示如下:
reason: 'Completion handler passed to -[JavaScriptCallOCViewController webView:decidePolicyForNavigationResponse:decisionHandler:] was not called'
#pragma mark 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
NSLog(@"6->在收到响应后,决定是否跳转");
NSLog(@"navigationResponse = %@", navigationResponse);
NSLog(@"navigationResponse.response = %@", navigationResponse.response);
// 必须实现decisionHandler的回调,否则就会报错
decisionHandler(WKNavigationResponsePolicyAllow);
NSLog(@"WKNavigationResponsePolicyAllow");
}