【问题标题】:PhoneGap: Opening external URL's in SafariPhoneGap:在 Safari 中打开外部 URL
【发布时间】:2012-04-20 11:01:58
【问题描述】:

我刚刚升级到 PhoneGap 1.6.1,我无法再获取外部 URL 以在 Safari 中打开。

在此版本之前,我对 AppDelegate.m 进行了如下修补:

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"http"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    } else {
        return [self.viewController webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
    }
}

我注意到之前有人问过类似的问题: How can I open an external link in Safari not the app's UIWebView?

但在我看来,这个答案不再适用于 1.6.1 版。

我也尝试在 Cordova.plist 中设置 OpenAllWhitelistURLsInWebView,但没有一个设置给我 Safari。

提前致谢。

【问题讨论】:

标签: cordova


【解决方案1】:

在新 URL 中打开链接的最佳方式实际上是使用 window.open。只需将目标设置为“_system”:

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

在我在文档中找到这个之前,我实际上写了一个插件来做同样的事情。我将把这个答案留在这里,因为这会让您对链接的处理进行更精细的控制。

使用 PhoneGap 2.3+,我无法以任何方式获取在 Mobile Safari 中打开的 URL。使用 _blank 不起作用,我尝试了 window.open(url, '_blank'),但这现在使用 InAppBrowser 插件打开了 URL(这非常糟糕)。我觉得那个使用插件很有趣,所以我决定编写一个插件来使用在 iOS 应用程序中打开 URL 的标准方法来打开 URL。您可以在this gist here: https://gist.github.com/typeoneerror/5097118 上查看/获取代码。

在我的示例中,我使用 jQuery 连接了一个名为“_blank”的类的链接,并使用插件调用打开了这些 URL:

// execute the plugin called OpenUrl, signature:
// exec(successCallback, errorCallback, pluginName, pluginMethod, params)
cordova.exec(success, error, "OpenUrl", "openUrl", [url]);

【讨论】:

  • 这是我发现与 Cordova 2.5 一起使用的唯一解决方案。效果很好!为了稍微改进一下,我使用了一个更具体的选择器,这样我就可以像普通的 webapp 一样使用 target="_blank":$("a[target='_blank']")
  • @jonnysamps 刚刚发现您可以通过使用“_system”作为 window.open 的目标来打开这样的 URL,所以也许您可以将 html 中的目标切换到 _system?我和你做了同样的事情,并定位了 [target=_blank] 并将 url 传递给 window.open,但它可能只适用于 HTML 中的 _system。
  • 就像 _system.使用您的自定义插件有什么好处吗?
  • 优势是能够根据请求的 URL 在 Objective-C/Java 中处理自定义情况。
  • 作为参考,使用 Cordova 4.2.0,我发现 _system 似乎也不再工作了。回到绘图板。
【解决方案2】:

我在 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 ];
    }
}

【讨论】:

  • 这几乎达到了在IOS上启动'Safari浏览器'的预期效果,我希望有人能阐明如何在Android上实现同样的效果。
  • 这是唯一适用于 iOS 的解决方案。对于 Android,我正在使用目标为“_system”的 InAppBrowser 插件,但在 iOS 上完全失败了。
  • 这很有效,非常感谢@john。但是,我们确实需要在 js 中正确设置代码。
【解决方案3】:

在早期版本的 cordova 中,您可以通过在链接中添加 target="_blank" 在浏览器中加载 url。但现在您可以使用 inApp 浏览器功能。

var ref = window.open(encodeURI('your url'), '_system', 'location=no');

这会在浏览器中打开网址。使用 cordova2.7.0

AndroidiPhone 中测试

【讨论】:

  • 我想是的。但我努力检查
  • 我实际测试过。 :-)
【解决方案4】:

升级到 Cordova 1.6.1 后遇到同样的问题。

尝试添加 target="_blank" 到您的链接。

这对我有用。

【讨论】:

  • 谢谢!这比我尝试的要简单得多。为了开始工作,我还需要在 Cordova.plist 中将 OpenAllWhitelistURLsInWebView 设置为 No
  • 自 2.3 以来,这不再是一个有效的答案,因为 _blank 现在在 InAppBrowser 中打开。
  • 这不再有效。使用:google.com" onclick="window.open(this.href,'_system'); return false;">谷歌
  • 对我不起作用,似乎需要 InAppBrowser 之类的插件
【解决方案5】:

我设法使它与这个设置一起工作:

1: 将以下行添加到 config.xml - PhoneGap 3.1 及更高版本:

<gap:plugin name="org.apache.cordova.inappbrowser" />

2: 之后你可以这样做:

<a onclick="window.open("http://www.yoururl.com/", "_system");">Link</a>

【讨论】:

  • 我还需要设置 window.open = cordova.InAppBrowser.open;
【解决方案6】:

尝试简化解决方案,这就是我最终的结果,使用 PhoneGap/Cordova 2.5.0 和 jQuery 1.9.1

  • OpenAllWhitelistURLsInWebView 无关紧要。将其设置为 true、false 或省略它似乎对结果没有任何影响。
  • URL 的目标是 _system,如下所示:&lt;a target="_system" href="https://rads.stackoverflow.com/amzn/click/com/B009CZICQ8" rel="nofollow noreferrer"&gt;
  • 然后我打电话:

    $("a[target='_system']").click(function(event) {
    
        event.preventDefault();
        window.open($(this).attr("href"), "_system");
    });
    

【讨论】:

    【解决方案7】:

    这样做:

    <a href="http://www.google.com/" onclick="window.open(this.href,'_system'); return false;">Google</a>
    

    【讨论】:

      【解决方案8】:

      今天使用此设置解决:

      1. 安卓:4.4
      2. iOS:8.2
      3. 科尔多瓦 CLI:5.1.1
      4. In App Browser plugin: 1.2.2

      然后按照以下步骤操作:

      1. 如上添加 In App Browser 插件
      2. 在您的 .html 文件中自定义此行:

      &lt;a href='http://www.arsdigitalia.it' target='_blank'&gt;Go to my nice website&lt;/a&gt;
      1. 在您的 .js 文件中使用以下行:

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

      【讨论】:

      • 效果很好(使用 PhoneGap Build 和 iOS 11 测试)。谢谢;)
      【解决方案9】:

      使用 Phonegap / Cordova 1.7,我已经能够在 JavaScript 中打开外部浏览器中的 URL,只需这样做:

      window.location.href = "http://www.google.com";
      

      我还在 Cordova.plist 中将 OpenAllWhitelistURLsInWebView 设置为 NO

      【讨论】:

      • Cordova.plist 设置对我来说至关重要。谢谢。
      【解决方案10】:

      如果您使用的是 Phonegap(在 iOS 6 中测试),这是 100% 保证的解决方案。

      要在 Safari 中打开外部 URL,请执行以下操作:

      1. 外部主机(白名单)中添加您的链接。例如:http://google.com/
      2. Cordova.plistPhonegap.plist 中,将OpenAllWhitelistURLsInWebViewYes 更改为No
      3. 在您的应用程序中,将target="_blank" 添加到您的链接。示例:

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

      谢谢。

      【讨论】:

      • 勇敢的断言:100% 有保证的解决方案
      • 感谢您提供完美的解决方案。不幸的是,它只能在 iOS 下工作。目前(PhoneGap 2.4.0)所有使用PhoneGap Build 构建的应用程序总是在Android 平台上打开WebView 中的所有链接,无论您采取什么步骤。这只是为寻找解决方案的 PhoneGap Build 和 Android 提供的通知。就目前而言,没有。
      【解决方案11】:

      我认为这个方法曾经在 /Classes/[yourAppName]AppDelegate.m 中,现在它位于 /Classes/MainViewController.m 中,只是将它移到那里看看它是否有效。

      【讨论】:

        【解决方案12】:

        在 Safari 中打开外部 URL 执行以下步骤。

        1).在外部主机(白名单)中添加您的链接。带有完整的 url 如果你想谷歌网址然后添加
        例如:http://google.com/

        2).在Cordova.plist或Phonegap.plist中,改变

        OpenAllWhitelistURLsInWebView 从是到否

        对于 Android True to false。

        3)。编写此代码以打开 URL

        window.location.href = "http://www.google.com";
        

        【讨论】:

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