Wiram Rathod
Wiram Rathod

Reputation: 1919

PhoneGap 2.5.0, how to open external URL for iPad/iPhone?

I try to open external url like this,

<a href="http://google.com" target="_system" >

and also try with _blank but it's open in same app screen not open in safari browser,

How to resolve..?

Upvotes: 9

Views: 5393

Answers (2)

stoneskin
stoneskin

Reputation: 317

My solution is like below: first I define a function to open safary url:

LaunchNewWindow: function (url) {       
   if (window.location.toStringing().match(/file/i) && navigator.userAgent.match(/iPhone|iPad|iPod/i) ) {   
    window.open(url+"_xex_", "_blank");}
   else{
    window.open(url, "_blank");
   }
}

then you have to change code in CordovaLic\Classses\CDViewController.m (CordovalLib 3.0.0) to handle your spacial url: I added it in line685 :

    else {

// start stoneskin's change: force open external url in safari                              
                              if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
    NSString *myurlstr = [url absoluteString];
    if ([myurlstr rangeOfString:@"_xex_"].location != NSNotFound){
        myurlstr = [myurlstr stringByReplacingOccurrencesOfString:@"_xex_" withString:@""];
        NSURL *myurl = [NSURL URLWithString:myurlstr];
        [[UIApplication sharedApplication] openURL:myurl];
        return NO;
    }

}
//end start stoneskin's change
if ([self.whitelist schemeIsAllowed:[url scheme]]) {
    return [self.whitelist URLIsAllowed:url];
} else {
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
} else { // handle any custom schemes to plugins
        [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}

return NO;
}

Upvotes: 0

wicketyjarjar
wicketyjarjar

Reputation: 1815

If you change your links to use the new InAppBrowser syntax, then it's easy to open your URLs either in the system web browser, the InAppBrowser or the actual webview of your app.

This code should open your URL in the system web browser (Safari on iOS):

<a href="#" onclick="var ref = window.open('http://google.com', '_system');">

Changing '_system' to '_blank' will open the URL in the InAppBrowser.

Changing '_system' to '_self' will open the URL in the webview of your app (if the domain is whitelisted) or the InAppBrowser (if the domain is not whitelisted).

Sample Gist: https://gist.github.com/wicketyjarjar/7043336

Note: Cordova/PhoneGap 3.0+ require the InAppBrowser plugin to be installed before this will work.

To install the InAppBrowser plugin (if necessary)...

Using Cordova: cordova plugin add org.apache.cordova.inappbrowser

Using PhoneGap: phonegap local plugin add org.apache.cordova.inappbrowser

Upvotes: 13

Related Questions