I'm looking for a way to listen for an action in a UIWebView. For example: When I tap a link or button inside a UIWebView, I want to call up a new native component, like a comment page. Or. take some other action like change a navigationBar item.
Asked
Active
Viewed 4,998 times
1 Answers
4
Yes you can do this. Implement
– webView:shouldStartLoadWithRequest:navigationType:
This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.
Example - here you are trying to intercept any links clicked on your webpage & pass it through myMethodAction
first.
- (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;
}
Hope this helps...

Srikar Appalaraju
- 71,928
- 54
- 216
- 264
-
Thanks for your reply! How do I know the content of click link? Cos you know usually there are more than one link in one page, if click link 1, call up native code page1, click link2 open native code page2... so I need to figure out which link is clicked to open up corresponding native code page. – Jason Zhao Dec 08 '11 at 10:26
-
1that info would be there in `request` arg that you get. Please read here - http://developer.apple.com/library/IOs/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html – Srikar Appalaraju Dec 08 '11 at 12:00