5

I've Googled this question in a few variations and can only find answers in the context of using PhoneGap or jQuery mobile. However, I'm using neither... just plain old html and javascript.

I'm trying to launch mobile safari from a full screen web app using window.open()... not an inline anchor. No matter what I do, the url opens in the web app, not in Safari. Does anyone have any suggestions?

Thanks.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Vince
  • 589
  • 9
  • 16

6 Answers6

2

It took me a while but I was able to piece this solution together. In an iOS standalone web app mode, it creates a link element using jQuery, adds it to the body, simulates a click on it, and then removes the link. You can accomplish the same thing without jQuery, it just takes more code using the naive DOM methods.

if (window.navigator.standalone) {
  var $a = $('<a href="' + url + '" target="_blank"/>');
  $("body").append($a);

  var a = $a.get(0);

  var mouseEvent = a.ownerDocument.createEvent('MouseEvents');
  mouseEvent.initMouseEvent('click');
  a.dispatchEvent(mouseEvent);

  $a.remove();
}
else {
  window.open(url, '_blank');
}
Steve
  • 572
  • 1
  • 5
  • 13
1

As of iOS 4.3, the only way I know of is to convert the <div> into an <a target="_blank"> and let the default browser to handler it. It launches the new page into an external Safari.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Thomas Yip
  • 11
  • 1
1

In PhoneGap, all URLs load into WebView by default. Setting targets or being fancy with JS will not break your external links out of that WebView. In order to load external URLs into Safari, rather than the PhoneGap app itself, you need to modify the way URLs are handled in the application delegate.

Open up your PhoneGap app code and locate [projectname]AppDelegate.m ... typically found under [projectname]/Classes folder.

Modify shouldStartLoadWithRequest however you like. Here is a sample implementation that will evaluate requests and handle HTTP or HTTPS schemes to load in Safari (borrowed from http://solutions.michaelbrooks.ca/2011/02/15/open-external-links-in-safariapp/):

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}
  • 1
    +1. Worked perfectly. Also, for those looking in `AppDelegate.m` without success, note that in later versions of PhoneGap/Apache Cordova, including 1.8.0, the generated override of the `webView:shouldStartLoadWithRequest:navigationType:` method has been commented out and moved to the bottom of `MainViewController.m`. – Daniel Trebbien Jun 16 '12 at 10:39
0

If you cannot create a standard HTML link , but you must open Safari programmatically (with Javascript code only), here is the code:

var a = document.createElement("a");
a.setAttribute('href', facebook);
a.setAttribute('target', '_blank');
a.click();
bummi
  • 27,123
  • 14
  • 62
  • 101
Marco Marsala
  • 2,332
  • 5
  • 25
  • 39
0

Have you tried using window.openNative()?

Stack Guru
  • 873
  • 2
  • 8
  • 7
  • It's interesting when you try the various URL Scheme options. Mail links launch the mail app, Map links launch the map app, etc. But a plain old http://www url does not launch Safari. – Vince Mar 25 '11 at 16:55