WKWebView on link click listener?

前端 未结 1 2057
旧时难觅i
旧时难觅i 2021-02-07 06:11

Does there exist something like a onLinkClickListener in the WKWebView class? I tried googling it but found nothing, I also found some unanswered questions on stackoverflow of s

1条回答
  •  醉梦人生
    2021-02-07 06:45

    You can do it like this

    add WKNavigationDelegate to your class

    class ViewController: UIViewController, WKNavigationDelegate
    

    set a navigation delegate

    yourWKWebview.navigationDelegate = self
    

    after that you will be able to use decidePolicyFor navigationAction function

     func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            if navigationAction.navigationType == WKNavigationType.linkActivated {
                print("link")
    
                decisionHandler(WKNavigationActionPolicy.cancel)
                return
            }
            print("no link")
            decisionHandler(WKNavigationActionPolicy.allow)
     }
    

    0 讨论(0)
提交回复
热议问题