Fork us on GitHub

Intercepting URL's On iOS & Android

Notice: the original version of this post incorrectly specifi...

Intercepting URL's On iOS & Android

Notice: the original version of this post incorrectly specified the property as AppArgs instead of AppArg. This is now fixed. For Android you would probably also want to add the build argument android.xactivity=android:exported="false".

A common trick in mobile application development, is communication between two unrelated applications. In Android we have intents which are pretty elaborate and can be used via Display.execute, however what if you would like to expose the functionality of your application to a different application running on the device. This would allow that application to launch your application.
This isn't something we builtin to Codename One, however we did expose enough of the platform capabilities to enable that functionality rather easily on Android.

On Android we need to define an intent filter which we can do using the android.xintent_filter build argument, this accepts the XML to filter whether a request is relevant to our application:
android.xintent_filter=<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="myapp" />  </intent-filter>

This is taken from this stack overflow question , to bind the myapp:// URL to your application. So if you will type myapp://x into the Android browser your application will launch.

So how do you get the "launch arguments", passed to your application?

Display.getInstance().getProperty("AppArg") should contain the value of the URL that launched the app or would be null if it was launched via the icon.

iOS is practically identical with some small caveats, iOS's equivalent of the manifest is the plist. So we allow injecting more data into the plist thru the

ios.plistInject build argument.
So the equivalent in the iOS side would be ios.plistInject=<key>CFBundleURLTypes</key>     <array>         <dict>             <key>CFBundleURLName</key>             <string>com.yourcompany.myapp</string>         </dict>         <dict>             <key>CFBundleURLSchemes</key>             <array>                 <string>myapp</string>             </array>         </dict>     </array>

On a separate unrelated note the guys from Java Code Geeks announced the winners of our raffle of two tickets , yesterday. Congratulations to both winners!

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.