我已将委托(delegate)分配给 wkwebview uiDelegate 并实现了它的方法 create web view with configuration,但它没有被调用。我什至设置了断点,但它没有命中。
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.uiDelegate = self
let requestURL = URL(string: "https://www.google.com")!
let request = URLRequest(url: requestURL)
webView.load(request as URLRequest)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let config = WKWebViewConfiguration()
return WKWebView(frame: webView.frame, configuration: config)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
}
最佳答案
似乎它没有在 webview 的初始初始化时调用委托(delegate)...我建议您只从代码创建 webview
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
configuration.allowsAirPlayForMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = []
webView = WKWebView(frame: containerView.frame, configuration: configuration)
webView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(webView)
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
webView.topAnchor.constraint(equalTo: containerView.topAnchor),
webView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
containerview 只是在我的 xib 中设置的一个基本 UIView 所以只需在你的 xib 中设置这个 uiview 而不是 wkwebview 就可以了
关于swift - uiDelegate 方法创建未调用配置的 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57144195/