我正在尝试使用Webview编写控制器,并且我需要WKNavigationDelegate方法,问题是这些方法根本没有被执行。
这是我的控制者:
import UIKit
import WebKit
public class WebViewController: UIViewController {
private let webView: WKWebView = WKWebView()
override public func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.isUserInteractionEnabled = true
view = webView
}
}
extension WebViewController: WKNavigationDelegate {
private func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
private func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
private func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}
}
正在使用容器视图将此控制器添加到顶视图控制器中,并且Web视图也正确加载了网站。有没有人遇到过类似的问题或知道我是否做错了事
答案 0 :(得分:1)
您似乎正在使用private
方法添加WKWebViewDelegate
,删除类似的方法,它将起作用。
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}