In my app certain HTML page is loaded in a webview. I need to get click on certain label like "neuron" and should display their description in another view. How Can i get the label click and clicked label in the webview?
Asked
Active
Viewed 3.3k times
3 Answers
68
Use the Delegate to Determine the Navigation Type!
My Snippet
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked){
NSURL *url = request.URL;
[self openExternalURL:url];//Handle External URL here
}
return YES;
}

Franky
- 1,153
- 1
- 11
- 20
-
1If you have instagram embed and click on profile link, it doesn't work. It detects type as Other, which is strange. – Nemanja Sep 02 '16 at 10:51
36
By "label" do you mean "link"? If so, give the UIWebView a delegate and implement webView:shouldStartLoadWithRequest:navigationType
. It will be called any time the user taps a link in the UIWebView.

matt
- 515,959
- 87
- 875
- 1,141
-
5Actually it won't always be called. For example, in some m.youtube.com pages. – cprcrack Oct 23 '13 at 17:05
-
@cprcrack do you know why it won't be called? I'm seeing similar behavior in some other sites. – vinnybad Sep 24 '16 at 06:48
5
Implementing this is simple. Everytime a webview wants to load something, it will call
webView:shouldStartLoadWithRequest:navigationType
which passes in the url associated with the hyperlink. Here, you can parse the NSURLRequest argument and handle what you want to do in native code.
(Remember, return NO to stop the UIWebView from actually loading the link afterwards)

futureelite7
- 11,462
- 10
- 53
- 87