I'm trying to change my UIWebView
to WKWebView
in my app (Objective-C
).
I see WKWebView
contain tag "a" and inside of tag "a" contain tag "image":
<a href="http://click.adzcore.com/xyz"><img src="http://www.abc.xyz/smart/images/bnr/yyy.png" width="320" height="50" border="0" alt="+alt[n]+" onclick="_gaq.push(['_trackPageview','/smart/count/frognote']);"></a>
I want detect when user click on image, so I do:
- (void)webView:(WKWebView )webView decidePolicyForNavigationAction:(WKNavigationAction )navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
NSURL *url = navigationAction.request.URL;
[[UIApplication sharedApplication] openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
But this code is not correct because WKNavigationTypeLinkActivated
is not catched when user click on image.
After research I found a solution to solve my problem. I don't use WKNavigationTypeLinkActivated to catch event click on image.
My solution:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if ([navigationAction.request.URL.relativeString hasPrefix:@"http://click.adzcore.com/"]) {
NSURL *url = navigationAction.request.URL;
[[UIApplication sharedApplication] openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
It's OK for my task :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With