Search code examples
iosuiwebviewuiwebviewdelegate

UIWebView get user touch on tapping link


I want to perform some action when the user clicks any link on UIWebView. I am doing this for making this work :

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"request - %@", request.URL.absoluteString);
    [webView.window makeKeyAndVisible];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    if(isFirstTimeLoaded)
    {
        [self shoToolbar];
        [self updateButtons];
    }


    return YES;
}  

- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
    isFirstTimeLoaded = YES;
}

This usually works fine. But it doesn't when UIWebView's first load involves multiple redirection while loading the page. What I want is to fire this condition only when the user intentionally clicks on the link. Not on the url redirection of UIWebView.


Solution

  • You have to check navigationType

        - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
        {
    
            if (navigationType==UIWebViewNavigationTypeLinkClicked) {
                // It was a link
            } else {
               // It wasn't a link
            }
    
    
            return YES;
        }