how can cordova open app from http or https url?

风格不统一 提交于 2020-11-30 06:38:05

问题


I found many answers for a custom URL-Scheme like this (mycoolapp://somepath).

This plugin for example adds a custom URL-Sheme.*

But I don't want a custom URL-Scheme, I want a "normal" URL like this (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:

application link

Just with my app to select.


回答1:


For the same problem I've used existing webintent plugin, modified the android manifest file - add those lines to activity

<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:

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:

<preference name="AndroidLaunchMode" value="singleTask" />

(It works with cordova 3.5, not sure about the older version)

Then you need to add one more function to 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.




回答2:


What you are looking for is called "Universal Links" on iOS and "Deep Linking" on Android.

And there is a Cordova plugin to handle that: https://www.npmjs.com/package/cordova-universal-links-plugin




回答3:


What you need to do is detect the device that is connecting to 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.




回答4:


You should add an intent-filter to your activity in the android manifest. Something like this:

<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

and even more here on stackoverflow...



来源:https://stackoverflow.com/questions/28041677/how-can-cordova-open-app-from-http-or-https-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!