Opening links from a UIWebView

If you are an iOS developer then sooner or later you’ll need to display some formatted text. And most likely you are using a UIWebView. One specific issue is opening links. Since you are not implementing a full blown browser, if the user selects a link then you don’t want to open it in the web view, but rather in Safari which is easily done (see this article on stackoverflow).

Now if you want to open App Store links of the type itms-apps:// then you usually don’t want to use Safari but the App Store app itself. So I used to adapt the code like this in my apps:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest
    if ( inType == UIWebViewNavigationTypeLinkClicked )
        NSString* url = inRequest.URL.absoluteString;
        // Open all URLs that are not app store links with Safari
        if ([url rangeOfString:@"itms-apps://"].location == NSNotFound)
            [[UIApplication sharedApplication] openURL:[inRequest URL]];
            return NO;

    return YES;

That worked fine until I noticed that it did not work on the iPad. So for some reason the iPad refused to open the link at all. But if I reverted the code to the original from stackoverflow then everything was fine on all my test devices (iPhone 3GS, 5 and iPad 2). So lesson learned: openURL does not necessarily go via Safari if the URL is itms-apps://

Share this: