我正在尝试禁用wkWebView中的链接加载,所以我正在使用decisionPolicyForNavigationAction,但是我的应用程序给了我这个错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用 原因:'完成处理程序传递给 - [ViewController webView:decisionPolicyForNavigationAction:decisionHandler:]未被调用'
这是给我这个错误的简化代码:
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)
}
}
}
答案 0 :(得分:16)
你必须为每个案件打电话decisionHanler
。
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == .linkActivated){
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
答案 1 :(得分:3)
这意味着,如果您拨打decidePolicyForNavigationAction
,则一次只能拨打decisionHandler(XX)
。
否则,应用程序可能会在NSInternalInconsistencyException: webView:decidePolicyForNavigationAction:decisionHandler was not called
(1)迅速
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);
}