PhoneGap:在默认浏览器中打开外部链接(在应用程序外) [英] PhoneGap: Open external link in default browser (outside the app)
问题描述
我正在尝试从 PhoneGap 应用程序打开 Safari(在 iPhone 上)中的链接.我正在使用 PhoneGap 3.1.0 版,并使用 PhoneGap Build 来构建应用程序.
I'm trying to open links in Safari (on an iPhone) from a PhoneGap application. I'm using PhoneGap version 3.1.0, and use PhoneGap Build, to build the application.
我在页面上有两个链接(如下所示在 www/index.html 中).这两个链接都在 PhoneGap 应用程序内打开.我可以看到 PhoneGap 已正确加载,因为 alert('device ready!'); 已触发.
I have two links on the page (shown below in www/index.html). Both links open inside the PhoneGap application. I can see that PhoneGap is loaded correctly, because alert('device ready!'); is triggered.
我需要更改什么才能在默认浏览器(应用程序外)中打开链接?
What do I need to change, to open the links in the default browser (outside the app)?
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
推荐答案
这是我在 Cordova/Phonegap 3.6.3
安装 inappbroswer cordova 插件:
Install the inappbroswer cordova plugin:
cordova plugin add org.apache.cordova.inappbrowser
我想让我的 phonegap 应用尽可能与标准网页相似:我希望通过在链接上设置 target="_blank" 来实现它,它可以在外部页面中打开.
I wanted to keep my phonegap app as similar as possible to a standard web page: I wanted that by having target="_blank" on a link, it would open in an external page.
我是这样实现的:
$("a[target='_blank']").click(function(e){
e.preventDefault();
window.open($(e.currentTarget).attr('href'), '_system', '');
});
所以我所要做的就是使用如下链接
so all i have to do is use a link like the following
<a href="http://www.example.com" target="_blank">Link</a>
这篇关于PhoneGap:在默认浏览器中打开外部链接(在应用程序外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!