WKWebView function for detecting if the URL has changed

前端 未结 3 950
孤城傲影
孤城傲影 2020-12-03 11:19

Is there a function for the WKWebView class that allows you to detect whenever the URL of that WebView has changed?

The didCommit and didStartProv

3条回答
  •  情歌与酒
    2020-12-03 11:39

    Swift Version

    // Add observer
    webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
    
    // Observe value
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let key = change?[NSKeyValueChangeKey.newKey] {
            print("observeValue \(key)") // url value
        }
    }
    

提交回复
热议问题