WKWebView on link click listener?

眉间皱痕 提交于 2020-01-01 08:24:14

问题


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 simillar type.

The reason I need a linkClickListener is that, when I click on a link and the page did not load yet, it does not load the website. I also could create a fancy loading screen, when the page is loading with the listener.


回答1:


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)
 }


来源:https://stackoverflow.com/questions/46254037/wkwebview-on-link-click-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!