I'm trying to disable link loading inside a wkWebView, so I'm using decidePolicyForNavigationAction for this, but my app gives me this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException' reason: 'Completion handler passed to -[ViewController webView:decidePolicyForNavigationAction:decisionHandler:] was not called'
This is the simplified code that gives me this error:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView
required init?(coder aDecoder: NSCoder) {
let config = WKWebViewConfiguration()
self.webView = WKWebView(frame: CGRectZero, configuration: config)
super.init(coder: aDecoder)
self.webView.navigationDelegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.insertSubview(webView, atIndex: 1)
webView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: -44)
let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
view.addConstraints([height, width])
webView.loadRequest(NSURLRequest(URL:NSURL(string:"https://www.apple.com")!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == WKNavigationType.LinkActivated){
print(navigationAction.request.URL?.relativeString)
decisionHandler(WKNavigationActionPolicy.Cancel)
}
}
}
You have to call the decisionHanler
for each case.
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == .linkActivated){
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
it means that if you call decidePolicyForNavigationAction
,you can only call decisionHandler(XX)
at once a time.
Otherwise the app will crash may occur about NSInternalInconsistencyException: webView:decidePolicyForNavigationAction:decisionHandler was not called
(1)swift
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (custom condition){
decisionHandler(XX) // if... return as well
} else {
decisionHandler(XX)
}
}
(2) OC
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *url = [navigationAction.request.URL absoluteString];
url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([url rangeOfString:@"xxxx://"].location != NSNotFound) {
// custom fundation
decisionHandler(WKNavigationActionPolicyCancel);
return; // the same as to use if...else
}
decisionHandler(WKNavigationActionPolicyAllow);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With