Search code examples
iphoneobjective-ciosuiwebviewmobile-safari

WebView vs. Safari


I have a web-app, that I'm trying to wrap as a native app. I'm using a webView, to display the web-app. The app contains a lot of articles, and some of them, have links to external pages. When I tap these links, the webView will load the external page, and all my navigation disappears, and I'm essentially locked at the external site. I have no possibility of going back to the app, without quitting it completely.

What I want to do is, have a script that evaluates the URL's before loading them in the webView. If the server differs from the one where I have the web-app, I want the link to launch mobile Safari. I have found a lot of scripts online, that does something like what I want, but not quite. They check for an href, and invokes the mobile Safari if the href is present. This will not work in my case, as pretty much all links in the App contains href's.

I have an example of what I'm trying to do here:

NSURL *requestURL = [ [request URL] retain ];

if (( ![ [requestURL scheme] isEqualToString: @"http://www.some-site.dk/" ] ) && ( navigationType == UIWebViewNavigationTypeLinkClicked ))
{
NSLog(@"Link opened in native Safari");
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
NSLog(@"Link opened in webView");
[ requestURL release ];
return YES;

How do I check if an url string is not equal to something in objective-c?

Any help will be greatly appreciated!


Solution

  • I ended up using the rangeOfString function to determine if the selected link included the domain name of the web-server containing the web-app. Everything else gets sent to the native Safari.

    Here's an example:

    NSRange infoUrl = [filePath rangeOfString:@"DOMAIN_NAME.COM"];
    
    if (infoUrl.location == NSNotFound)
    {
         return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
    }
    
    return YES;