Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active April 6, 2020 14:57
Show Gist options
  • Save FokkeZB/6340484 to your computer and use it in GitHub Desktop.
Save FokkeZB/6340484 to your computer and use it in GitHub Desktop.
URL schemes for iOS and Android (1/2)
if (OS_ANDROID) {
// Somehow, only in alloy.js we can get the data (URL) that opened the app
Alloy.Globals.url = Ti.Android.currentActivity.intent.data;
}
// We don't want our URL to do anything before our main window is open
$.index.addEventListener('open', function (e) {
if (OS_IOS) {
// Handle the URL in case it opened the app
handleURL(Ti.App.getArguments().url);
// Handle the URL in case it resumed the app
Ti.App.addEventListener('resumed', function () {
handleURL(Ti.App.getArguments().url);
});
} else if (OS_ANDROID) {
// On Android, somehow the app always opens as new
handleURL(Alloy.globals.url);
}
});
// Source: https://github.com/FokkeZB/UTiL/blob/master/XCallbackURL/XCallbackURL.js
var XCallbackURL = require('XCallbackURL');
function handleUrl(url) {
var URL = XCallbackURL.parse(url),
controller = URL.action(),
args = URL.params();
// Add some better logic here ;)
Alloy.createController(controller, args || {}).getView().open();
}
<ti:app>
<!-- other stuff -->
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- keep any custom attributes for manifest -->
<manifest>
<!-- keep any custom attributes for application -->
<application>
<activity android:configChanges="keyboardHidden|orientation" android:label="My App"
android:name=".MyAppActivity" android:theme="@style/Theme.Titanium"
android:launchMode="singleTask" >
<!-- add the above launchMode attribute -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- add the below additional intent-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>
</activity>
</application>
</manifest>
<!-- other android stuff -->
</android>
</ti:app>
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<!-- other stuff -->
<ios>
<plist>
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<!-- same as ti:app/id -->
<string>my.app.id</string>
<key>CFBundleURLSchemes</key>
<array>
<!-- your custom scheme -->
<string>myapp</string>
<!-- same as 'fb' plus ti:app/property[name=ti.facebook.appid] -->
<string>fb123456789</string>
</array>
</dict>
</array>
<!-- other ios/plist stuff -->
</dict>
</plist>
<!-- other ios stuff -->
</ios>
</ti:app>
@trs-mark
Copy link

index.js
line10-12

Ti.App.addEventListener('resume', function () {
        handleURL(Ti.App.getArguments().url);
});

It will pass an empty url. Use "resumed" event instead.
I tried it and it worked.

Sources:
https://developer.appcelerator.com/question/93841/any-getarguments-equivalent-in-resume-event
http://stackoverflow.com/questions/15241739/take-user-to-app-store-if-app-is-not-installed-on-android-iphone

@DouglasHennrich
Copy link

Can't make it work on android... how I launch my app from another app?
I'm trying to launch as

var didItWork = Ti.Platform.openURL('feedback://estabelecimento?id_empresa=1');
    if(!didItWork) {
        alert('You need to install the Target app');
    }

but I always get that this app is not installed... and on my tiapp.xml on android's section I have this:

<android xmlns:android="http://schemas.android.com/apk/res/android">
        <manifest>
            <uses-sdk android:minSdkVersion="21"/>
            <uses-permission android:name="android.permission.INTERNET"/>

            <activity android:name=".FeedbackActivity" 
                android:label="Feedback Brasil" 
                android:theme="@style/Theme.Titanium" 
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:alwaysRetainTaskState="true">

                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>

                <intent-filter>
                    <data android:scheme="feedback" android:host="" />
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                </intent-filter>
            </activity>
        </manifest>
    </android>

what i'm doing wrong?

Thanks

@mitulbhalia
Copy link

@FokkeZB I followed the steps and it works well for iphone and for android, it launches app and shows passed url first time only.
If the app is in background mode then it shows blank screen.
Do you have work around for this issue?

@FokkeZB
Copy link
Author

FokkeZB commented Oct 30, 2017

@DouglasHennrich @mitulbhalia opening a Titanium Android app from an URL has had (and probably still has) lots of issues and edge cases. See https://jira.appcelerator.org/browse/TIMOB-20490

@mitulbhalia
Copy link

@FokkeZB
The above was working fine till now but i need to change the urlscheme for ios something like https://www.url.com/param/?view=123
so i tried to set CFBundleURLSchemes to https://www.url.com and www.url.com but did not work.
should i need to use something else for CFBundleURLSchemes?

@FokkeZB
Copy link
Author

FokkeZB commented Apr 9, 2018

@mitulbhalia I have no idea, sorry. I haven't been using Titanium for 2 years anymore. I'd suggest to try TiSlack.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment