PhoneGap: Opening external URL's in Safari

后端 未结 12 866
耶瑟儿~
耶瑟儿~ 2020-11-30 03:04

I\'ve just upgraded to PhoneGap 1.6.1 and I can no longer get external URL\'s to open in Safari.

Prior to this version I had patched AppDelegate.m as follows:

<
相关标签:
12条回答
  • 2020-11-30 03:41

    I have used this in the 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://"];
        //HACK to make url open in safari
        if (range.location != NSNotFound) {
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
        else {
            return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
        }
    }
    
    0 讨论(0)
  • I think this method used to be in /Classes/[yourAppName]AppDelegate.m and now its located at /Classes/MainViewController.m Just moved it there to see if it works.

    0 讨论(0)
  • 2020-11-30 03:45

    to open external URL in Safari Do following steps.

    1).Add your link in External Host (white list).with complete url if you want to google url then add
    e.g.: http://google.com/

    2).In Cordova.plist or Phonegap.plist, change

    OpenAllWhitelistURLsInWebView from Yes to No

    for Android True to false.

    3). write this code for opening the URL

    window.location.href = "http://www.google.com";
    
    0 讨论(0)
  • 2020-11-30 03:49

    Do this:

    <a href="http://www.google.com/" onclick="window.open(this.href,'_system'); return false;">Google</a>
    
    0 讨论(0)
  • 2020-11-30 03:49

    Solved today using this settings:

    1. Android: 4.4
    2. iOS: 8.2
    3. Cordova CLI: 5.1.1
    4. In App Browser plugin: 1.2.2

    Then follow these steps:

    1. Add the In App Browser plugin as above
    2. in your .html file customize this line:

    <a href='http://www.arsdigitalia.it' target='_blank'>Go to my nice website</a>

    1. in your .js file use these lines:

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.open = cordova.InAppBrowser.open;
    }
    window.addEventListener('load', function () {    
        $(document).on('click', 'a[target="_system"],a[target="_blank"]', function (e) {
                e.preventDefault();
                var url = this.href;
                window.open(url,"_system");                    
        });
      }
    }, false);

    0 讨论(0)
  • 2020-11-30 03:51

    The best way to open links in a new URL is actually with window.open. Just set the target as "_system":

    window.open("http://stackoverflow.com", "_system");
    

    Before I found this in the docs, I actually wrote a plugin to do the same. I'm going to leave this answer here, because this would give you much more granular control over handling of links.

    With PhoneGap 2.3+, I was unable to get URLs to open in Mobile Safari in any way. Using _blank didn't work, and I tried window.open(url, '_blank'), but this now opens the URL using the InAppBrowser plugin (which pretty much sucks). I thought it was interesting that that one used a plugin though, so I decided to write a plugin to open URLs using the standard method of opening URLs in iOS apps. You can see/grab the code on this gist here: https://gist.github.com/typeoneerror/5097118.

    In my example, I wired up links that have a class called "_blank" with jQuery and opened those URLs with the plugin call:

    // execute the plugin called OpenUrl, signature:
    // exec(successCallback, errorCallback, pluginName, pluginMethod, params)
    cordova.exec(success, error, "OpenUrl", "openUrl", [url]);
    
    0 讨论(0)
提交回复
热议问题