How to override UIWebView links request action with your own custom method

Featured Image

If you did not understand from the post title what I am trying to say, here is another explanation. If you have an instance of UIWebView class implemented in your view and you want to call one of your custom method when user clicks on link in that UIWebView instance, first thing you must do to achieve that is set the delegate of that UIWebView class instance on you instance of UIViewController class. This is almost in same time last thing. Now just copy&paste this code into your class that implements UIWebView instance and add call to your method.

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
if (overrideLinksSwitch.on == TRUE) {
[self myMethodAction];
[myWebView stopLoading];
return YES;
}
else {
return YES;
}
}
return YES;
}

You can download example app here.

5
Top

Care to rate this post?

Author

Ivan Kalaica

Ex. Inchooer

At Inchoo, Ivan Kalaica was an iOS SDK developer. He worked with us from 2008. to 2012.

Other posts from this author

Discussion 5 Comments

Add Comment
  1. Vinay

    Good One.I was looking for such customization of web view.Thanks for the post.

  2. Sarah

    Thanks – that was very helpful. Completely solved the problem.
    S

  3. Ivan Kalaica

    Thanks! ;)

  4. Hi Thanks, it Helped me.

  5. Madhu

    Nice post :) it helped me. but i have a que , whether same implementation can be done , if it is not webview?

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top