4

我已将委托分配给 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!) {
}
}
4

1 回答 1

0

似乎它没有在 webview 的初始初始化时调用委托...我建议您只从代码创建 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 就可以了

于 2020-06-18T14:58:46.957 回答