Open a link in a UIWebView

Discussion in 'iPhone/iPad Programming' started by SqueegyX, Jul 31, 2008.

  1. macrumors regular

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

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #3
    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 :(
     
  4. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    Cursor inside editable textfield when the view loads?

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

    Joined:
    Sep 9, 2009
    #5
    Thanks, Very useful. This is a gold mine :)
     

Share This Page