我在WKWebView中加载了一个URL。
let url = URL(string: "myURL")
let request = URLRequest(url: url!)
webView1.navigationDelegate = self as! WKNavigationDelegate
webView1.load(request)
但是,委托函数
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Finished navigating to url \(webView.url)")
}
在完成加载时不会触发。
然后我将KVO用于网络视图
webView1.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "loading") {
guard var web1 = object as? WKWebView else { return }
}
}
正在加载正常。但是,加载observer
webview
可用
答案 0 :(得分:5)
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
如果正确设置navigationDelegate
,则必须调用。
你做过这个吗?
class ViewController: UIViewController, WKNavigationDelegate {
}
如果您设置上述WKNavigationDelegate
,您的代码将是这样的,
let url = URL(string: "http://www.google.com")
let request = URLRequest(url: url!)
webView.navigationDelegate = self
webView.load(request)
和委托方法,
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Finished navigating to url \(String(describing: webView.url))")
}
希望这有帮助,如有任何疑问,请与我联系。
FYI。无需设置外部观察员。
<强>更新强>
在您的App Transport Security
中启用info.plist
吗?
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>