【问题标题】:PhoneGap 2.5.0, how to open external URL for iPad/iPhone?PhoneGap 2.5.0,如何打开 iPad/iPhone 的外部 URL?
【发布时间】:2013-04-13 12:55:40
【问题描述】:

我尝试像这样打开外部网址,

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

也可以尝试使用_blank,但它在同一个应用程序屏幕中打开,而不是在 safari 浏览器中打开,

如何解决..?

【问题讨论】:

    标签: cordova phonegap-build


    【解决方案1】:

    如果您更改链接以使用新的 InAppBrowser 语法,则可以轻松地在系统 Web 浏览器、InAppBrowser 或应用的实际 Web 视图中打开您的 URL。

    此代码应在系统网络浏览器(iOS 上的 Safari)中打开您的 URL:

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

    '_system' 更改为 '_blank' 将在 InAppBrowser 中打开 URL。

    '_system' 更改为'_self' 将在您的应用(如果域被列入白名单)或InAppBrowser(如果域未被列入白名单)的webview 中打开该URL。

    示例要点:https://gist.github.com/wicketyjarjar/7043336

    注意:Cordova/PhoneGap 3.0+ 需要安装 InAppBrowser 插件才能工作。

    安装 InAppBrowser 插件(如有必要)...

    使用 Cordova:cordova plugin add org.apache.cordova.inappbrowser

    使用PhoneGap:phonegap local plugin add org.apache.cordova.inappbrowser

    【讨论】:

    • 我也尝试了这个,但在同一个 AppBrowser 中打开的链接不起作用
    • 它对我不起作用。对于所有选项页面正在应用中打开
    • 如果您使用的是 Cordova/PhoneGap 3.0+,您现在需要通过命令行单独添加 InAppBrowser 插件,然后它才能工作。刚刚用更多信息和更新的要点更新了我上面的答案。另外,这是我对可能有帮助的类似问题的回答:stackoverflow.com/a/19453481/2103983
    【解决方案2】:

    我的解决方案如下: 首先我定义一个函数来打开 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");
       }
    }
    

    那么您必须更改 CordovaLic\Classses\CDViewController.m (CordovaLib 3.0.0) 中的代码来处理您的空间网址: 我在第 685 行添加了它:

        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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多