27

我刚刚升级到 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];
    }
}

我注意到之前有人问过类似的问题: 如何在 Safari 中打开外部链接而不是应用程序的 UIWebView?

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

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

提前致谢。

4

12 回答 12

42

在新 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。您可以在此处查看/获取有关此要点的代码: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]);
于 2013-03-06T06:25:30.563 回答
9

我在 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 ];
    }
}
于 2013-10-22T11:15:59.730 回答
8

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

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

这将在浏览器中打开 url。使用cordova2.7.0AndroidiPhone中测试

于 2013-07-25T04:48:15.870 回答
5

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

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

这对我有用。

于 2012-04-22T16:13:06.243 回答
4

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

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>
于 2014-04-16T12:02:44.927 回答
3

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

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

    $("a[target='_system']").click(function(event) {
    
        event.preventDefault();
        window.open($(this).attr("href"), "_system");
    });
    
于 2013-03-21T04:01:29.473 回答
3

做这个:

<a href="http://www.google.com/" onclick="window.open(this.href,'_system'); return false;">Google</a>
于 2013-06-08T01:21:59.430 回答
3

今天使用此设置解决了:

  1. 安卓:4.4
  2. iOS:8.2
  3. 科尔多瓦 CLI:5.1.1
  4. 在应用浏览器插件中:1.2.2

然后按照以下步骤操作:

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

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

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

于 2016-02-04T11:48:42.590 回答
2

使用 Phonegap / Cordova 1.7,我已经能够通过执行以下操作在 JavaScript 中的外部浏览器中打开 URL:

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

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

于 2012-06-21T15:44:19.687 回答
2

如果您使用的是 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>
    

谢谢你。

于 2012-11-10T20:12:35.147 回答
1

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

于 2012-05-02T22:13:18.267 回答
1

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

1).Add your link in External Host (white list).with complete url if you want to google url 然后添加
例如:http: //google.com/

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

OpenAllWhitelistURLsInWebView 从是到否

对于 Android 真到假。

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

window.location.href = "http://www.google.com";
于 2014-03-27T05:04:23.933 回答