16

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.

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Nano
  • 1,398
  • 6
  • 20
  • 32
  • have you tried using the http url as url-scheme? – jcesarmobile Jan 20 '15 at 13:45
  • @jcesarmobile wouldn't that mean (if it actually works) that **every** website would open my app? – Nano Jan 20 '15 at 15:08
  • I mean the whole website url h ttp://www.mycoolapp.com/, not just the http://. Some apple apps can be opened with a regular web url, but maybe they use some private API, I have not tested. – jcesarmobile Jan 20 '15 at 15:16
  • I think I want to do this ; so if they have my app it opens that, and if not it opens the website. Will it work like that for the EddyVerbruggen plugin linked to in this question? – kris Aug 19 '16 at 12:53

5 Answers5

19

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.

extempl
  • 2,987
  • 1
  • 26
  • 38
rhorvath
  • 3,525
  • 2
  • 23
  • 30
  • 1
    i found out one small issue with the code, you should also add the onNewIntent and maybe change the LaunchMode, please take a look at the edit – rhorvath Jan 29 '15 at 15:28
  • This works really well without the need to install a plugin or set anything up. What would be the equivalent of this for the iOS? – J.Do Feb 04 '19 at 15:31
  • Works fine when app is not launched, if launched in background it doesn't works, had to add window.plugins.webintent.onNewIntent – user2455079 Mar 11 '20 at 10:09
5

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

jwolinsky
  • 159
  • 4
  • 2
0

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.

Quickly
  • 35
  • 3
  • The Browser has to render the whole page with your example. I want that the user can choose **before** the Browser opens. – Nano Jan 21 '15 at 13:37
  • 1
    Honestly good luck and let me know if you find it. Its an interesting idea but I don't know if you can distinguish that this http request is different then another one. I would look into intents. You could set the server to just redirect to your custom url scheme as soon as it detects its a device. This is how linkedin does it. – Quickly Jan 21 '15 at 17:23
  • Thanks I will take a look at it, but i would preffer a cordova Plugin or something like this :S – Nano Jan 22 '15 at 07:54
0

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

Community
  • 1
  • 1
Vlad Stirbu
  • 1,732
  • 2
  • 16
  • 27
  • Does it works for cordova (phonegap) Applcations? I dont think, that i have access to the android manifest . – Nano Jan 25 '15 at 16:07
  • Should work fine. I typically create an app specific plugin that has the whole purpose to configure the android manifest. Check the [plugin.xml](http://docs.phonegap.com/en/edge/plugin_ref_spec.md.html#Plugin%20Specification) spec. You are interested in the config-file Element. Add the above snipped and you are done, cordova-cli should patch the android manifest as specified in the plugin.xml – Vlad Stirbu Jan 25 '15 at 16:20
0

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>
Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137