Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS WKWebview how to detect when I click on an image inside of <a> tag

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.

like image 680
chaunv Avatar asked Sep 30 '16 02:09

chaunv


1 Answers

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

like image 140
chaunv Avatar answered Sep 30 '22 05:09

chaunv