Hugo
Hugo

Reputation: 75

Cordova 3.1 open link in Safari on iOS6

I'm building a iOS app with Cordova 3.1. I have a link I would like to open in Safari. I've installed the org.apache.cordova.inappbrowser plugin and it worked well on my iPhone (iOS 7) and on the simulator (iOS5;iOS6.1;iOS7) but if I try (iOS6) on all devices it doesn't work.

Does anybody know how to fix this or tried it on a real device running iOS6? I'm using this code to open the link:

window.open('http://www.google.nl', '_system');

Upvotes: 6

Views: 732

Answers (2)

Stuart Watt
Stuart Watt

Reputation: 5401

I know this is an old question, but I've encountered it too and just written a small plugin to help with it. Siddhartha's answer is almost right, but when I used it, it intercepted all web requests, including those to my index.html, and this seemed to move my app into Safari. What I needed was a way to handle only explicit requests, so I could open specific (external) URLs in Safari.

There are many similar questions about Phonegap, which appears to embed special handling for window.open with _system. That would be nice, but Cordova doesn't have this functionality.

In the end, I wrote a small plugin that embeds enough Objective C (closely modelled on Siddhartha's answer) but due to the magic of plugins, it can be executed on demand, using cordova.exec. I'd love this to patch into window.open to achieve the same functionality as Phonegap, but that'll be for another day, and it doesn't really affect this answer.

As far as I can tell, in modernish Cordova, this is the only viable strategy.

Upvotes: 0

Siddhartha Gupta
Siddhartha Gupta

Reputation: 1168

well I've implemented this through native side (Objective C)

Add this method in 'MainViewController.m'

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    NSString *str = url.absoluteString;
    NSRange range = [str rangeOfString:@"http://"];
    NSRange range1 = [str rangeOfString:@"https://"];

    if (range.location != NSNotFound || range1.location != NSNotFound) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

This take cares of both 'http' and 'https' link for both iOS6 & iOS7, and opens the link in default browser of the device.

Upvotes: 3

Related Questions