A majority of mobile enterprise apps are designed around a specific task such as time reporting. Through the use of URL schemes or Android intent filters you can open and communicate between different apps on the user's device. For example, you can provide an option for your organization's time-reporting app to open the expense app when an employee is traveling and needs to record additional information.
This recipe demonstrates how to launch different apps using the native platform's integration pattern. The following screenshot illustrates this recipe running on both an iOS and Android device:
.
This recipe uses the schemeList
CommonJS module. This module and other code assets can be downloaded from the source provided by the book. Installing these in your project is straightforward. Simply copy the schemeList.js
file into your project as shown in the following screenshot:
After adding the schemaList
module, you will need to create your application namespace in the app.js
file and use require
to import the module into your code as the following code snippet demonstrates:
//Create our application namespace var my = { schemes : require('schemeList'), isAndroid : Ti.Platform.osname === 'android' };
Adding the ability to allow other iOS apps to launch your apps is straightforward. The following code snippet demonstrates how to add a custom scheme named, bencoding-linkLauncher
to this recipe. This allows any iOS app to launch this sample in a similar fashion as discussed later in this recipe.
<ios> <plist> <dict>
The CFBundleURLlTypes
node is an array of dictionaries describing the URL schemes supported by the bundle. Under this node, you will list all of the URL and scheme names your app will respond to.
<key>CFBundleURLTypes </key> <array> <dict>
The CFBundleURLName
is the key that contains the abstract name for this URL type. This is the main way to refer to a particular type. To ensure uniqueness, it is recommended that you use a Java-package style identifier.
<key>CFBundleURLName </key> <string>bencoding-linkLauncher</string>
The CFBundleURLSchemes
is the key that contains an array of strings, each of which identifies a URL scheme handled by this type.
<key>CFBundleURLSchemes</key> <array> <string>bencoding-linkLauncher</string> </array> </dict> </array> </dict> /plist> </ios>
The schemeList
CommonJS module, added to the my.scheme
property provides a list of iOS and Android scheme examples. The following sections describe how schemes are created on both platforms.
On iOS, a URL scheme works just like a URL on the web. The only difference is the app's registered URL scheme is used as the protocol. The following steps provide detailed examples of the specific URL formats available.
getiOSList
provides an array of objects designed to be bound to a Ti.UI.TableView
and later used to launch other apps.exports.getiOSList = function(){ var apps =[];
CFBundleURLName
of the app. This is shown in the following examples for Evernote and LinkedIn.apps.push({ title:'Evernote', url:'evernote://', errorMessage:'Check Evernote is installed' }); apps.push({ title:'LinkedIn', url:'linkedin://', errorMessage:'Check Linkedin is installed' });
about
route.apps.push({ title:'Recipe Example 1', url:'bencoding-linkrecipe://about', errorMessage:'Check Link Receiver Recipe is installed' });
apps.push({ title:'Recipe Example 2', url:'bencodinglinkrecipe: //'+'login?user=chris&token=12345', errorMessage:'Check Link Receiver Recipe is installed' }); return apps; };
Android has the ability to launch applications in a variety of ways. This recipe focuses on how to use a URL scheme similar to iOS and intents for app integration.
The getAndroidList
provides an array of objects designed to be bound to a Ti.UI.TableView
and later used to launch other apps.
exports.getAndroidList = function(){ var apps =[];
about
route.apps.push({ title:'Recipe Example 1', type:'url', color:'#000', url:'bencoding-linkrecipe://com.bencoding/about', errorMessage:'Check Link Receiver Recipe is installed' });
apps.push({ title:'Recipe Example 2', type:'url', color:'#000', url:'bencoding-linkrecipe://'+'login?user=chris&token=12345', errorMessage:'Check Link Receiver Recipe is installed' });
var linkedInIntent = Ti.Android.createIntent({ action: Ti.Android.ACTION_SEND, packageName :'com.linkedin.android', className: 'com.linkedin.android.home.UpdateStatusActivity' });
apps.push({ title:'LinkedIn', type:'intent', color:'#000', intentObject :linkedInIntent });
var everNoteIntent = Ti.Android.createIntent({
action: "com.evernote.action.CREATE_NEW_NOTE"
});
apps.push({ title:'Evernote', type:'intent', color:'#000', intentObject :everNoteIntent }); return apps; };
This section of the recipe is the UI used to launch the third-party apps.
Ti.UI.Window
is created to attach all UI elements.var win = Ti.UI.createWindow({ backgroundColor: '#fff', title: 'App Launcher', barColor:'#000',layout:'vertical',fullscreen:false });
schemeList
CommonJS module is called to return a list of the applications to launch.var schemeData = ((my.isAndroid) ? my.schemes.getAndroidList() : my.schemes.getiOSList())
schemaData
object is then formatted and bound to a Ti.UI.TableView
for display.var tableView = Ti.UI.createTableView({ data : my.schemes.format(schemeData), top:10, bottom:0, width:Ti.UI.FILL }); win.add(tableView);
The final section of this recipe demonstrates how to launch third-party apps using the list displayed in the Ti.UI.TableView
.
click
event is added to the Ti.UI.TableView
. This event will be fired when the user taps on a row in the Ti.UI.TableView
.tableView.addEventListener('click',function(e){
if(my.isAndroid){ try{
if(e.rowData.type == "intent"){
Ti.Android.currentActivity.startActivity
is called using the provided intent as shown in the following snippet. This will launch the third-party app if it is installed on the user's device.Ti.Android.currentActivity.startActivity( e.rowData.intentObject); }else{
Ti.Platform.openURL
method is used to open the app using the provided url
property as shown in the following snippet. This will launch the third-party app if it is installed on the user's device.Ti.Platform.openURL(e.rowData.url); } }catch(err){ alert(e.rowData.errorMessage); } }else{
Ti.Platform.openURL
method. The following snippet demonstrates how to launch a third-party app using the url
property provided by the schemeList
CommonJS module:if(Ti.Platform.canOpenURL(e.rowData.url)){ Ti.Platform.openURL(e.rowData.url); }else{ alert(e.rowData.errorMessage); } } }); win.open({modal:true});
3.148.117.212