How to override UIWebView links request action with your own custom method
4 Comments 15th JUN 2009 | Posted by Ivan Kalaica in iPhone dev

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.
To post code in comments, place your code inside [code] and [/code] tags.


















July 8th, 2009 at 8:47
Good One.I was looking for such customization of web view.Thanks for the post.
October 12th, 2009 at 18:15
Thanks – that was very helpful. Completely solved the problem.
S
November 16th, 2009 at 19:48
Thanks!
June 2nd, 2010 at 11:32
Hi Thanks, it Helped me.