1

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?

David
  • 560
  • 2
  • 10
  • 26

1 Answers1

2

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.

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42