检测何时在 WKWebView 中单击 link
Detect when a link is clicked in a WKWebView
如何检测 link 在 WKWebView 中被点击的时间?我正在 UIWebView 中寻找与此等效的内容。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
}
return YES;
}
我在 WKNavigationDelegate
中尝试过此操作,但即使单击 link,我也只能获得所有请求的 WKNavigationTypeOther
。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if(navigationAction.navigationType == WKNavigationTypeLinkActivated)
{
}
decisionHandler(WKNavigationActionPolicyAllow);
}
您可以在 WkWebView
中执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView()
self.view.addSubview(webView) // you need to also setup constraints here - I left out for clarity
// Make sure you set the delegate, so you get the callback
self.webView.navigationDelegate = self
}
// WKWebViewNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url, let scheme = url.scheme, scheme.contains("http") else {
// This is not HTTP link - can be a local file or a mailto
decisionHandler(.cancel)
return
}
// This is a HTTP link
open(url: url)
decisionHandler(.allow)
}
在此委托方法中,您将获得 WKWebView
试图打开的 URL 请求。在那里您可以检查 URLRequest
的属性并做出相应的响应。
您甚至可以为 URLRequest
编写处理该逻辑的扩展,以便您可以重用它。
extension URLRequest {
var isHttpLink: Bool {
return self.url?.scheme?.contains("http") ?? false
}
}
那么可以将委托方法中的长条件改成:
// WKWebViewNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard navigationAction.request.isHttpLink else {
decisionHandler(.allow)
return
}
// ... handle url
如何检测 link 在 WKWebView 中被点击的时间?我正在 UIWebView 中寻找与此等效的内容。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
}
return YES;
}
我在 WKNavigationDelegate
中尝试过此操作,但即使单击 link,我也只能获得所有请求的 WKNavigationTypeOther
。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if(navigationAction.navigationType == WKNavigationTypeLinkActivated)
{
}
decisionHandler(WKNavigationActionPolicyAllow);
}
您可以在 WkWebView
中执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView()
self.view.addSubview(webView) // you need to also setup constraints here - I left out for clarity
// Make sure you set the delegate, so you get the callback
self.webView.navigationDelegate = self
}
// WKWebViewNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url, let scheme = url.scheme, scheme.contains("http") else {
// This is not HTTP link - can be a local file or a mailto
decisionHandler(.cancel)
return
}
// This is a HTTP link
open(url: url)
decisionHandler(.allow)
}
在此委托方法中,您将获得 WKWebView
试图打开的 URL 请求。在那里您可以检查 URLRequest
的属性并做出相应的响应。
您甚至可以为 URLRequest
编写处理该逻辑的扩展,以便您可以重用它。
extension URLRequest {
var isHttpLink: Bool {
return self.url?.scheme?.contains("http") ?? false
}
}
那么可以将委托方法中的长条件改成:
// WKWebViewNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard navigationAction.request.isHttpLink else {
decisionHandler(.allow)
return
}
// ... handle url