我想使用 Google Chrome(不是其他浏览器)从我的 Cordova 应用程序打开一个链接。我尝试了几种解决方案,但没有任何效果。
首先,我尝试创建一个指向 googlechrome://
的链接(使用 HTML <a>
标签)。它会打开 Google Chrome,但我无法选择要打开的 URL。
然后,我尝试创建一个指向 intent://my.url.com/#Intent;scheme=https;package=com.android.chrome;end
的链接.当我从浏览器(如 Firefox 或 Google Chrome)打开它时,此链接有效,但当我从我的 Cordova 应用程序打开它时,它不起作用。
我还尝试使用 cordova-webintent插件,就像那样:
window.plugins.webintent.startActivity(
{
action: window.plugins.webintent.ACTION_VIEW,
url: 'intent://my.url.com/#Intent;scheme=https;package=com.android.chrome;end'
},
function() {
alert(url);
},
function(){
alert('Failed to open URL via Android Intent');
}
);
但是我得到一个“无法通过 Android Intent 打开 URL” 错误。
我该怎么办?
PS:我写了<allow-intent href="*" />
在我的 config.xml 文件中。
最佳答案
您可以使用 cordova-plugin-inappbrowser以 _system
为目标:
cordova.InAppBrowser.open('http://stackoverflow.com', '_system', 'location=yes');
或者您可以使用 cordova-plugin-webintent2 (不知道你用的是不是这个),像这样:
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: "googlechrome://navigate?url=http://www.stackoverflow.com"
},
function() {},
function() {
alert('Failed to open URL via Android Intent.');
console.log("Failed to open URL via Android Intent.")
});
url必须以http或https开头,不允许相对url。
关于android - Cordova : Open a link using Google Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094005/