David
David

Reputation: 560

Open link in browser inside iOS app with web techniques

Let's assume someone is running a web app directly inside a simple iOS app with UIWebView. It it possible to open a external website link inside the browser (Safari Mobile) with web techniques like JavaScript? If yes, how could it be done?

Upvotes: 1

Views: 2621

Answers (1)

CSmith
CSmith

Reputation: 13458

To open a URL programmatically:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"];

As far as your WebView, you can use your WebViewDelegate to catch and inspect any hyperlinks the user selects. For example:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *urlPath = [request URL];
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        if ([[urlPath scheme] hasPrefix:@"http"]))
        {
            [[UIApplication sharedApplication] openURL:urlPath];
            return (NO);
        }

    }

    return (YES);
}

This should open any hyperlink click in browser app.

You can also create a custom protocol, see Android / iOS - Custom URI / Protocol Handling, and catch those in this same delegate handler by checking the [url scheme] property. This will allow you to explicitly do something with javascript.

Upvotes: 2

Related Questions