javascript cordova 如何从 http 或 https url 打开应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28041677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can cordova open app from http or https url?
提问by Nano
I found many answers for a custom URL-Scheme like this (mycoolapp://somepath
).
我为这样的自定义 URL-Scheme 找到了很多答案 ( mycoolapp://somepath
)。
This pluginfor example adds a custom URL-Sheme.*
例如,此插件添加了自定义 URL-Sheme.*
But I don't want a custom URL-Scheme, I want a "normal" URL like this (http://www.mycoolapp.com/somepath
).
但我不想要一个自定义的 URL-Scheme,我想要一个像这样的“普通”URL ( http://www.mycoolapp.com/somepath
)。
If you open this in you Browser or click on a Hyperlink for example, then it should ask you to open my app (like google maps does it).
例如,如果您在浏览器中打开它或单击超链接,那么它应该要求您打开我的应用程序(就像谷歌地图那样)。
This question maybe already has an answer, but i can't find it.
这个问题可能已经有了答案,但我找不到。
If you don't know what I mean, that's how it should look if you click on the link to my website on an Android Device:
如果您不明白我的意思,那么如果您在 Android 设备上单击指向我的网站的链接,它应该是这样的:
Just with my app to select.
只需用我的应用程序来选择。
回答by rhorvath
For the same problem I've used existing webintentplugin, modified the android manifest file - add those lines to activity
对于同样的问题,我使用了现有的webintent插件,修改了 android 清单文件 - 将这些行添加到活动中
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="example.com" android:scheme="http" />
</intent-filter>
and modified the index.html ondeviceready:
并修改了 index.html ondeviceready:
function deviceReady() {
window.plugins.webintent.getUri(function(url) {
console.log("INTENT URL: " + url);
//...
});
}
EDIT
编辑
I've just noticed a behavior which may be unwanted. When you open the app using the link (intent) from another application, it will (in many cases) create a new instance and not use the already running one (tested with gmail and skype). To prevent this a solution is to change Android Launch mode in config.xml file:
我刚刚注意到一种可能不需要的行为。当您使用来自另一个应用程序的链接(意图)打开应用程序时,它会(在许多情况下)创建一个新实例,而不使用已经运行的实例(使用 gmail 和 Skype 测试)。为了防止这种情况,解决方案是在 config.xml 文件中更改 Android 启动模式:
<preference name="AndroidLaunchMode" value="singleTask" />
(It works with cordova 3.5, not sure about the older version)
(它适用于cordova 3.5,不确定旧版本)
Then you need to add one more function to ondeviceready:
然后你需要在 ondeviceready 中再添加一个函数:
window.plugins.webintent.onNewIntent(function(url) {
console.log("INTENT onNewIntent: " + url);
});
This one is triggered when the app was already running and was brought to front with intent.
当应用程序已经在运行并被有意地带到前面时,就会触发这个。
回答by jwolinsky
What you are looking for is called "Universal Links" on iOS and "Deep Linking" on Android.
您正在寻找的是 iOS 上的“通用链接”和 Android 上的“深度链接”。
And there is a Cordova plugin to handle that: https://www.npmjs.com/package/cordova-universal-links-plugin
并且有一个 Cordova 插件来处理:https: //www.npmjs.com/package/cordova-universal-links-plugin
回答by Quickly
What you need to do is detect the device that is connecting to http://www.mycoolapp.com/somepath
您需要做的是检测连接到http://www.mycoolapp.com/somepath的设备
If it is a mobile device then you can present them with a page with a link with your custom url scheme that opens your app. Or auto open the custom url of the app if you want.
如果它是移动设备,那么您可以向他们展示一个页面,其中包含可以打开您的应用程序的自定义 url 方案的链接。或者,如果需要,可以自动打开应用程序的自定义 url。
回答by Vlad Stirbu
You should add an intent-filter
to your activity
in the android manifest. Something like this:
不应该将添加intent-filter
到您activity
的Android清单。像这样的东西:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:host="www.mycoolapp.com" />
<data android:pathPrefix="/somepath" />
</intent-filter>
more on what data
you can add here: http://developer.android.com/guide/topics/manifest/data-element.html
更多关于data
你可以在这里添加的内容:http: //developer.android.com/guide/topics/manifest/data-element.html
and even more hereon stackoverflow...
更这里的计算器...