Custom URL Scheme - iPhone & Android
Hi,
I am looking on how to implement the Custom URL Scheme feature (i.e launch the mobile app when clicking on a link in safari or e-mail) for both iPhone and Android.
My goal is to send an e-mail with an HTML link
<a href="myapp://" />
Such that when the user clicks on it, the device starts the app with url attributes (if they exist)
But I couldn't find anything about it in the docs, neither in the Q&A…
Did anyone already do/implement this ??
How to do it ??
What file to complete or configure in order to do it ??
Hints, ideas ??
Thanks in advance !!
5 Answers
-
For iOS, check the Info.plist file in the (project)/build/iphone folder and look for the CFBundleURLSchemes key. The value it lists under the array element is the name assigned for the custom URL. You can change that if necesary, but just knowing it may be sufficient for you. I think the default is basically your project name in lowercase.
Once you have that name, use it in a URL and add :// followed by whatever you want:
mybundleurlscheme://foobar?attribute=value&another_attribute=value2
In your app, use Ti.App.getArguments() to find out what was passed into the program. The caveat here is detecting the difference between a launch and pause/resume activity.
Last I played with this, a pause/resume would end up still leaving the same value in Ti.App.getArguments() making it harder to correctly detect a new launch vs resume activity.
A couple of pointers here:
Save a copy of Ti.App.getArgumets() for later comparison
Add a app listener for the 'resumed' event (NOT the 'resume' event) and check Ti.App.getArguments(). The value is not yet available during the 'resume' event (when app is preparing to resume) but is during the 'resumed' event (when app has resumed).
When 'resumed' event has the same value you originally saved, either the user did a pause/resume or the same custom URL was launched a second time. In my scenario, I could get by treating both the same but YMMV.
Here is a code snippet from a project where I experimented with this. It is from a few SDK versions back, so perhaps there is a better way now:
// Save initial launch command line arguments Ti.App.launchURL = ''; Ti.App.pauseURL = ''; var cmd = Ti.App.getArguments(); if ( (getTypeOf(cmd) == 'object') && cmd.hasOwnProperty('url') ) { Ti.App.launchURL = cmd.url; Ti.API.info( 'Launched with url = ' + Ti.App.launchURL ); } // Save launch URL at the time last paused Ti.App.addEventListener( 'pause', function(e) { Ti.App.pauseURL = Ti.App.launchURL; }); // After app is fully resumed, recheck if launch arguments // have changed and ignore duplicate schemes. Ti.App.addEventListener( 'resumed', function(e) { Ti.App.launchURL = ''; cmd = Ti.App.getArguments(); if ( (getTypeOf(cmd) == 'object') && cmd.hasOwnProperty('url') ) { if ( cmd.url != Ti.App.pauseURL ) { Ti.App.launchURL = cmd.url; Ti.API.info( 'Resumed with url = ' + Ti.App.launchURL ); } } });
At this point I can check Ti.App.launchURL and deal with it as I like.
-
OK, so here is the recipe for URL scheme on Android…
But if you prefer, post a proper & different question, so that I answer to it rather than with these small comments (who doesn't support lists…), but as you wish…
The first thing you have to do is to create a "custom" AndroidManifest.xml file. in order to do it create a folder in your application project folder () name
platform
, and inside it another folder namedandroid
, and inside this create a folder named android (platform/android
) copy theAndroidManifest.xml
that you can find in yourbuild/android/
.If we resume simply with unix commands :
cd MyAppHome mkdir platform mkdir platform/android cp build/android/AndroidManifest.xml platform/android/
such that you get the following project structure
<MyAppHome> - Resources/ - ... - platform/ - android/ - AndroidManifest.xml
Then the next step is to edit the AndroidManifest.xml. Open it, and go to the lines between the and .
There you should find a the main of your mobile app.
There you should add lines such that you have (some lines can already be there, so just add the lines you do not already have)
<application android:icon="@drawable/appicon" android:label="MobileApp" android:name="MobileappApplication" android:debuggable="false" > <activity android:name=".MobileappActivity" android:label="MobileApp" android:theme="@style/Theme.Titanium" android:configChanges="keyboardHidden|orientation" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.LAUNCHER" /> <data android:scheme="Mobileapp" /> </intent-filter> </activity> <!-- TI_APPLICATION -->
And to call your app from an URL, use
Mobileapp://
Be careful with UPPERCASE and LOWERCASE letters in AndroidManifest.xml….I banged my head to walls for 1-2 days because of this….
Wherever you see 'Mobileapp' instead of 'MobileApp', it is not a mistake…..Android cannot handle the uppercase letters except for the first one…
This is also the case with the URL, if you use 'MobileApp://' nothing will happen, but it will work with 'Mobileapp://' .
(At least, when I first did the URL schemes, I was with SDK 1.6.2, so I had to be careful with letter cases…I don't know if it is still relevant with 1.7.0, but all I know is that my URL schemes still continue to work :) :) )
Hope that this will help you.
Tell me if it is ok for you.
-
In SDK 3.0, there is No need to create a Custom AndroidManifest.xml, but rather simply open your tiapp.xml and find your <android> <application> node, and make sure this is added in there:
<activity android:name=".MobileappActivity" android:label="MobileApp" android:theme="@style/Theme.Titanium" android:configChanges="keyboardHidden|orientation" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.LAUNCHER" /> <data android:scheme="Mobileapp" /> </intent-filter> </activity>
-
It doesn't work for me . When i hit my app name in browser it by default searches in google and open the web version of my app. Please help me.
-
For open link in android you have to write following code:
<application android:icon="@drawable/appicon" android:label="Buddy Drinks" android:name="BuddyDrinksApplication">
<activity android:name="Application_Activity_Name" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name="Package _Name.Application_Activity_Name" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="Schema_name _As_you_want" android:host="As_you_want"/>
</intent-filter>
</activity>Now got to the terminal write command(Mac terminal command)
./adb shell am start -a android.intent.action.VIEW -d "Schema_name://Host_Name" Package_Namerun this command your application will start
You can check this via create a link also.
For create the link follow this link
https://developers.google.com/app-indexing/webmasters/test