Search code examples
javascriptcordovaurl-scheme

how can cordova open app from http or https url?


I’ve found several solutions for creating a custom URL scheme, like mycoolapp://somepath.

For instance, this plugin provides functionality for defining a custom URL scheme.

However, I don’t need a custom URL scheme. Instead, I want to use a standard URL structure, like http://www.mycoolapp.com/somepath. Ideally, when this URL is opened in a browser or clicked as a hyperlink, the user should be prompted to open my app—similar to how Google Maps behaves.

To clarify, here’s how I want it to work: when a user clicks a link to my website on an Android device, they should see a prompt to open the link in my app, as shown in the image below:

application link


Solution

  • 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.