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.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


5 comments

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.