27

我正在尝试从 PhoneGap 应用程序打开 Safari(在 iPhone 上)中的链接。我正在使用 PhoneGap 3.1.0 版,并使用PhoneGap Build来构建应用程序。

我在页面上有两个链接(如下 www/index.html 所示)。这两个链接都在 PhoneGap 应用程序中打开。我可以看到 PhoneGap 已正确加载,因为alert('device ready!'); 被触发。

我需要更改什么才能在默认浏览器(应用程序外部)中打开链接?

www/config.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.company.appname" version="0.0.3">
  <name>AppName</name>
  <description>description</description>
  <author href="http://www.example.com/" email="hello@example.com">
    Company
  </author>
  <preference name="phonegap-version" value="3.1.0" />
  <preference name="orientation" value="portrait" />
  <preference name="stay-in-webview" value="false" />

  <gap:plugin name="org.apache.cordova.inappbrowser" version="0.2.3" />
  <gap:plugin name="org.apache.cordova.dialogs" version="0.2.2" />
  <gap:plugin name="com.phonegap.plugins.pushplugin" version="2.0.5" />

  <plugins>
    <plugin name="InAppBrowser" value="CDVInAppBrowser" />
  </plugins>

  <feature name="InAppBrowser">
    <param name="ios-package" value="CDVInAppBrowser" />
  </feature>
  <access origin="*" />
</widget>

www/index.html文件如下所示:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <title>Test application</title>
</head>
<body>
  <a href="#" onclick="openUrl('http://www.google.com/'); return false;">Link test 1</a>
  <a href="#" onclick="window.open('http://www.google.com/', '_system', 'location=yes'); return false;">Test link 2</a>

  <script src="phonegap.js"></script>
  <script src="cordova.js"></script>
  <script src="js/jquery-1.9.1.js"></script>
  <script type="text/javascript">
    function openUrl(url) {
      alert("open url: " + url);
      window.open(url, '_blank', 'location=yes');
    }

    function onDeviceReady() {
      alert('device ready!');
    }
    $(function() {
        document.addEventListener("deviceready", onDeviceReady, true);
    });
  </script>
</body>
</html>

这是项目结构:

├── platforms
├── plugins
│   └── org.apache.cordova.inappbrowser
│       ├── LICENSE
│       ├── package.json
│       ├── plugin.xml
│       ├── README.md
│       ├── RELEASENOTES.md
│       ├── src
│       │   ├── android
│       │   │   ├── InAppBrowser.java
│       │   │   └── InAppChromeClient.java
│       │   ├── blackberry10
│       │   │   └── README.md
│       │   ├── ios
│       │   │   ├── CDVInAppBrowser.h
│       │   │   └── CDVInAppBrowser.m
│       │   └── wp
│       │       └── InAppBrowser.cs
│       └── www
│           ├── InAppBrowser.js
│           └── windows8
│               └── InAppBrowserProxy.js
├── README.md
└── www
    ├── config.xml
    ├── cordova.js
    ├── index.html
    ├── js
    │   └── jquery-1.9.1.js
    └── phonegap.js
4

16 回答 16

17

这就是我在Cordova/Phonegap 3.6.3中解决的方法

安装 inappbroswer cordova 插件:

cordova plugin add org.apache.cordova.inappbrowser

我想让我的 phonegap 应用程序尽可能与标准网页相似:我希望通过在链接上设置 target="_blank",它会在外部页面中打开。

这就是我实现它的方式:

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

所以我所要做的就是使用如下链接

<a href="http://www.example.com" target="_blank">Link</a>
于 2014-10-03T08:53:16.480 回答
10

这个怎么样?

<a href="#" onclick="window.open(encodeURI('http://www.google.com/'), '_system')">Test link 2</a>

编辑:

这可能与: 如何在 onclick 事件中调用多个 JavaScript 函数?

我在想,这个怎么样:

添加到代码:

$(".navLink").on('tap', function (e) {
    //Prevents Default Behavior 
    e.preventDefault();
    // Calls Your Function with the URL from the custom data attribute 
    openUrl($(this).data('url'), '_system');
});

然后:

<a href="#" class="navLink" data-url="http://www.google.com/">Go to Google</a>
于 2013-11-18T16:51:24.433 回答
9

您应该在 config.xml 中使用 gap:plugin 标记和完全限定的 id 来安装插件:

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

如此处所述

于 2013-11-19T00:31:35.910 回答
7

我正在使用cordova 6.0,这是我的解决方案:

1:安装这个cordova插件。

cordova plugin add cordova-plugin-inappbrowser

2:在html中添加打开链接,如下所示。

<a href="#" onclick="window.open('https://www.google.com/', '_system', 'location=yes');" >Google</a>

3:这是最重要的一步,因此我遇到了很多问题:下载cordova.js文件并将其粘贴到www文件夹中。然后在index.html文件中对此进行引用。

<script src="cordova.js"></script>

此解决方案适用于环境 android 和 iPhone。

于 2016-09-01T05:37:06.710 回答
3

试试下面这个例子。

<a class="appopen" onClick="OpenLink();">Sign in</a>


<script>
function OpenLink(){
    window.open("http://www.google.com/", "_system");
}
</script>

如果您使用的是 PhoneGap 3.1 或更高版本,请在 config.xml 中添加以下行

<gap:plugin name="org.apache.cordova.inappbrowser" />
于 2013-11-20T03:39:45.107 回答
3

我遇到了与您相同的问题,解决方案是将 phonegap.js文件包含到<head>我将使用 InAppBrowser 的所有页面中。

你所有的代码都没问题!您唯一需要添加的是:<script src="phonegap.js"></script>在您的<head>部分index.html

这有点奇怪,因为 Phonegap 在他的插件文档部分中说:

“如果一个插件使用该js-module元素来指导 cordova 加载插件 javascript,那么加载插件就不需要<script>引用。核心 cordova 插件就是这种情况

InAppBrowser 是一个核心的 cordova 插件。但是由于某些奇怪的原因,直到您将其包含在phonegap.js文件中(至少在 0.3.3 版本中)才起作用。

注意:我发现了一个错误。有人说你必须包含 3 个文件phonegap.jscordova.jscordova_plugins.js. 但是当我包含这 3 个文件时,我的应用程序在 iOS 7 中运行良好,但在 iOS 6 中忽略了插件的使用(使用:Cordova 3.3.0 + Phonegap Build + InAppBrowser 0.3.3)。

您可以在我的SO question中看到更多信息。

希望这可以帮到你!

于 2014-06-11T18:48:26.477 回答
2

迟到的答案,但也许对某人有帮助。所以我在基于 Cordova 的 iOS 和 Android 应用程序中的工作代码中都有什么。在默认浏览器(Safari 或 Google)中打开外部链接:

1) 确保您有 inAppBrowser 插件

cordova plugin add cordova-plugin-inappbrowser --save

2) 添加到 device.js

function openURL(urlString){
  let url = encodeURI(urlString);
  window.open(url, '_system', 'location=yes');
}

3) 创建新链接

<a href="#" onClick="openURL('http://www.google.com')">Go to Google</a>
于 2018-12-26T14:55:20.323 回答
1

对于 iO,在 MainViewController.m 中执行以下操作

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if (![url isFileURL] && navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
    } 

    return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; 
}

编辑:对于 Android,在 CordovaWebViewClient.java 中,在 shouldOverrideUrlLoading 中执行以下操作:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if(/*check if not external link*) {
            view.loadUrl(url);
          } else {
            //prepend http:// or https:// if needed
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
          }
          return true;
        }
于 2015-05-22T13:18:59.650 回答
1

在 iOS 上非常适合我。

如上面的链接所述:

1-使用命令安装插件:

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git 

2- 在 HTML 文件中,根据需要使用以下选项之一:

window.open(‘http://example.com’, ‘_system’);   Loads in the system browser 
window.open(‘http://example.com’, ‘_blank’);    Loads in the InAppBrowser
window.open(‘http://example.com’, ‘_blank’, ‘location=no’); Loads in the InAppBrowser with no location bar
window.open(‘http://example.com’, ‘_self’); Loads in the Cordova web view 
于 2016-08-22T09:55:46.860 回答
1

我调整了 Pastullo 的答案,使其也适用于可能在 Cordova InAppBrowser 中打开的 WebApp,但也适用于普通的 Web-App(例如用于测试):

function initOpenURLExternally() {
    $("a[target='_blank'],a[target='_system']").each(function (i) {
        var $this = $(this);
        var href = $this.attr('href');
        if (href !== "#") {
            $this
                .attr("onclick", "openURLExternally(\"" + href + "\"); return false;")
                .attr("href", "#");
        }
    });
}

function openURLExternally(url) {
    // if cordova is bundeled with the app (remote injection plugin)
    log.trace("openURLExternally: ", url);
    if (typeof cordova === "object") {
        log.trace("cordova exists ... " + typeof cordova.InAppBrowser);
        if (typeof cordova.InAppBrowser === "object") {
            log.trace("InAppBrowser exists");
            cordova.InAppBrowser.open(url, "_system");
            return;
        }
    }

    // fallback if no cordova is around us:
    //window.open(url, '_system', '');
    window.open(url, '_blank', '');
}
于 2017-10-07T10:53:36.833 回答
0

这就是我让我的工作的方式。

  1. 在 config.xml (phonegap) 添加<gap:plugin name="org.apache.cordova.inappbrowser" />
  2. 我的hrefs如下所示:<a onclick='window.open("http://link.com","_system", "location=yes")' href='javascript:void(0)' >link</a>
  3. 非常重要,我从一开始就缺少什么,在你的标题中添加科尔多瓦脚本:<script src="cordova.js"></script>我不知道为什么,但对我来说没有它就行不通。

希望这会有所帮助

于 2014-06-11T13:05:58.577 回答
0

可能是 PATH 环境变量问题,试试这个链接它可能会有所帮助。

Apache Cordova 文档

Phonegap/Cordova – 如何链接到外部页面

于 2015-06-17T05:13:07.940 回答
0

而不是target="_blank"target="_system"用于您希望在外部浏览器(应用程序外部)中打开的链接。然后当点击这些链接时,您的设备将从您的应用程序切换到系统的浏览器应用程序(即 Safari 或 Chrome)以打开链接。

于 2019-04-02T20:14:50.653 回答
0

编辑 config.xml 在插件入口处添加 source="npm"。'<'gap:plugin name="org.apache.cordova.inappbrowser" source="npm" > '

于 2015-10-11T10:52:32.343 回答
-1

对于那些在原始问题中没有发现它的人,您还需要通过向config.xml添加访问标记来确保您尝试打开的 URL 未列入白名单,如下所示:

<access origin="http://www.example.com" />

或者你可以做

<access origin="*" />

允许任何事情

于 2014-10-16T04:07:36.267 回答
-3

如果您尝试在外部 Web 浏览器中打开链接,请尝试使用 class="external" 作为 Anchor 标签。

<a class="external" href="www.google.com" >Link</a>

希望这可以帮助!

于 2017-06-28T18:24:31.563 回答