方法decisionPolicyForNavigationAction从不调用,WKWebView Swift

时间:2018-01-27 16:19:29

标签: ios swift xcode webview wkwebview

我正在开发一个在Swift中使用WKWebView显示网页的应用程序。我需要禁用用户选择和标注(因为Web加载图形),我找不到任何方法使用WKWebView。

我正在尝试使用此代码但不幸的是,似乎永远不会调用此方法。你知道我的代码的哪一部分可能是错误或我能做什么?

public func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        if (navigationAction.navigationType == .linkActivated){
            decisionHandler(.allow)
        } else {
            decisionHandler(.cancel)
        }
}

这是我的完整代码:

import WebKit
import UIKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "http://danfg95glucose.azurewebsites.net")
    let request = URLRequest(url: url!)

    webView.navigationDelegate = self
    self.webView.uiDelegate = self
    webView.load(request)

}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("loaded")

}

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    if navigationAction.targetFrame == nil {
        webView.load(navigationAction.request)
    }
    return nil
}


public func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    if (navigationAction.navigationType == .linkActivated){
        decisionHandler(.allow)
    } else {
        decisionHandler(.cancel)
    }
}
}

非常感谢您的回复。我正在学习快速编程,我有点迷失。

1 个答案:

答案 0 :(得分:2)

忘记_

public func webView(_ webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    if (navigationAction.navigationType == .linkActivated){
        decisionHandler(.allow)
    } else {
        decisionHandler(.cancel)
    }
}