Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SqueegyX

macrumors regular
Original poster
Mar 24, 2008
108
1
I have a UIWebView displaying externally fetched content form an API. I want to be able to tap a link in that view and open that link in Safari. Currently tapping links doesn't do anything at all.

Also, and perhaps related, I have a javascript function triggered by an "onclick" that doesnt seem to be firing with the element gets tapped. Is a tap supposed to fire the onclick event, or is there some other event I can capture with javascript to do something with a tap on an html element?

Thanks for the help guys.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You need to implement the webView:shouldStartLoadWithRequest:navigationType: delegate method and if the type is a link, open it as a normal URL and return NO. If not, just return YES.
 

SqueegyX

macrumors regular
Original poster
Mar 24, 2008
108
1
You need to implement the webView:shouldStartLoadWithRequest:navigationType: delegate method and if the type is a link, open it as a normal URL and return NO. If not, just return YES.

Thanks, I'm definitely getting somewhere.

It's doesnt seem to be triggering form a link click. I am loading HTML initailly in with a string, and I am seeing the delegate method get triggered. However, when I actually click a link, nothing happens. I see the a:hover style change in the web view as I touch the link, but when I release, nothing happens. My NSLog() isn't even triggered.

Here's the code I am testing with. What am I missing?

Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSLog(@"expected:%d, got:%d", UIWebViewNavigationTypeLinkClicked, navigationType);
  if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
    return NO;
  }
  
  return YES;
}

I can even hold down on the link and get the little popup telling me what the URL is, but I release it, I get nothing :(
 
  • Like
Reactions: thomtes

SqueegyX

macrumors regular
Original poster
Mar 24, 2008
108
1
Cursor inside editable textfield when the view loads?

Oops I got it. Apparently links cant have target="_blank" on them
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.