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

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.
Published in:
Leave a comment
5 comments
Nice post 🙂 it helped me. but i have a que , whether same implementation can be done , if it is not webview?
Hi Thanks, it Helped me.
Thanks! 😉
Thanks – that was very helpful. Completely solved the problem.
S
Good One.I was looking for such customization of web view.Thanks for the post.