Nano
Nano

Reputation: 1398

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

Upvotes: 16

Views: 28000

Answers (5)

Chris Marasti-Georg
Chris Marasti-Georg

Reputation: 34650

For anyone looking to use this answer, but modifying the AndroidManifest via config.xml, the following did the trick for me. Trying to match on android:name didn't work no matter which permutations I used.

        <config-file target="AndroidManifest.xml" parent="/manifest/application/activity[@android:label='@string/activity_name']">
            <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:scheme="https" />
                <data android:host="www.mysite.com" />
                <data android:pathPrefix="/whatever/path" />
            </intent-filter>
        </config-file>

Upvotes: 2

rhorvath
rhorvath

Reputation: 3725

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.

Upvotes: 19

jwolinsky
jwolinsky

Reputation: 159

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

Upvotes: 5

Vlad Stirbu
Vlad Stirbu

Reputation: 1792

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

Upvotes: 0

Quickly
Quickly

Reputation: 35

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.

Upvotes: 0

Related Questions