WKWebView上的链接单击侦听器? [英] WKWebView on link click listener?
问题描述
WKWebView类中是否存在类似onLinkClickListener的东西?我尝试使用Google搜索,但一无所获,还发现了关于类似类型的stackoverflow的一些未解决的问题.
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.
我需要linkClickListener的原因是,当我单击链接并且页面尚未加载时,它也不会加载网站.当页面使用侦听器加载时,我还可以创建一个精美的加载屏幕.
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.
推荐答案
您可以这样做
将WKNavigationDelegate添加到您的班级
add WKNavigationDelegate to your class
class ViewController: UIViewController, WKNavigationDelegate
设置导航代表
yourWKWebview.navigationDelegate = self
之后,您将可以使用definePolicyFor navigationAction函数
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)
}
这篇关于WKWebView上的链接单击侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!