WkWebView内のリンクの読み込みを無効にしようとしているので、これにdecidePolicyForNavigationActionを使用していますが、アプリで次のエラーが発生します。
キャッチされなかった例外のためにアプリを終了します 'NSInternalInconsistencyException'理由: '完了ハンドラーが-[ViewControllerwebView:decidePolicyForNavigationAction: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)
}
}
}
ケースごとにdecisionHanler
を呼び出す必要があります。
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == .linkActivated){
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
つまり、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);
}