PhoneGap:在默认浏览器中打开外部链接(在应用程序外部)

问题描述 投票:26回答:16

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

我在页面上有两个链接(如下面的www / index.html所示)。两个链接都在PhoneGap应用程序内打开。我可以看到PhoneGap已正确加载,因为警报('设备就绪!');被触发了。

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

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="[email protected]">
    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
cordova phonegap-plugins phonegap-build inappbrowser
16个回答
16
投票

这就是我在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>

0
投票

这就是我开始工作的方式。

  1. 在config.xml(phonegap)中添加function openURL(urlString){ let url = encodeURI(urlString); window.open(url, '_system', 'location=yes'); }
  2. 我的hrefs看起来如下:<a href="#" onClick="openURL('http://www.google.com')">Go to Google</a>
  3. 非常重要的是,我从一开始就缺少了,在你的标题中添加了cordova脚本:<gap:plugin name="org.apache.cordova.inappbrowser" />我不知道为什么,但对我来说没有它它不起作用。

希望这会有所帮助


0
投票

可能有PATH环境变量问题,尝试此链接可能会有所帮助。

<a onclick='window.open("http://link.com","_system", "location=yes")' href='javascript:void(0)' >link</a>

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


0
投票

编辑config.xml在插件条目中添加source =“npm”。''gap:plugin name =“org.apache.cordova.inappbrowser”source =“npm”>'


0
投票

我改编了Apache Cordova Documentation,也可以在一个可以在cordova InAppBrowser中打开的WebApp中工作,也可以在普通的Web-App中打开(例如用于测试):

Phonegap/Cordova – How to link to external pages

0
投票

而不是pastullo's answer,使用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', ''); } 作为您希望在外部浏览器(应用程序之外)打开的链接。然后,当您点击这些链接时,您的设备将从您的应用切换到系统的浏览器应用(即Safari或Chrome)以打开链接。


-1
投票

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

target="_blank"

或者你可以做

target="_system"

允许任何事情


-3
投票

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

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

希望这可以帮助!


10
投票

这个怎么样?

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

编辑:

这可能属于:How to call multiple JavaScript functions in onclick event?

我在想,怎么样:

添加到代码:

$(".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>

9
投票

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

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

正如记载here


6
投票

我使用的是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:这是最重要的一步,因为我遇到了很多问题:下载qazxsw poi文件并将其粘贴到qazxsw poi文件夹中。然后在cordova.js文件中引用它。

www

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


3
投票

尝试以下示例。

index.html

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

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

3
投票

我有同样的问题,你和解决方案包括phonegap.js文件到我将使用InAppBrowser的所有页面中的<a class="appopen" onClick="OpenLink();">Sign in</a> <script> function OpenLink(){ window.open("http://www.google.com/", "_system"); } </script>

你的所有代码都可以!你唯一需要补充的是:你的<gap:plugin name="org.apache.cordova.inappbrowser" /> <head>部分的<script src="phonegap.js"></script>

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

“如果一个插件使用<head>元素指示cordova加载插件javascripts,那么加载插件就不需要index.html引用。这就是核心cordova插件的情况”

InAppBrowser是一个核心cordova插件。但是由于一些奇怪的原因,在你将它包含在js-module文件中之前不起作用(至少在0.3.3版本中)。

注意:我发现了一个错误。有些人说你必须包含3个文件:<script>phonegap.jsphonegap.js。但是当我包含这3个文件时,我的应用程序在iOS 7中工作正常,但在iOS 6中忽略了插件的使用(使用:Cordova 3.3.0 + Phonegap Build + InAppBrowser 0.3.3)。

你可以在我的cordova.js中看到更多。

希望这可以帮到你!


1
投票

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

cordova_plugins.js

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

SO question

1
投票

- (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]; } 在iOS上完美地为我工作。

如上面的链接所述:

1-使用以下命令安装插件:

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

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

This

1
投票

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

1)确保你有inAppBrowser插件

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

2)添加到device.js

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 

3)创建新链接

cordova plugin add cordova-plugin-inappbrowser --save
© www.soinside.com 2019 - 2024. All rights reserved.