Open a link in a UIWebView

Discussion in 'iOS Programming' started by SqueegyX, Jul 31, 2008.

  1. SqueegyX macrumors regular

    Mar 24, 2008
    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.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    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.
  3. SqueegyX thread starter macrumors regular

    Mar 24, 2008
    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?

    - (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:@""]];
        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 :(
  4. SqueegyX thread starter macrumors regular

    Mar 24, 2008
    Cursor inside editable textfield when the view loads?

    Oops I got it. Apparently links cant have target="_blank" on them
  5. ajaygautam macrumors newbie

    Sep 9, 2009

Share This Page