Launching one app from another

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:

.

Launching one app from another

Note

This recipe must be run on a device to fully experience all features. A device is required as the simulator or emulator do not allow apps from the different app stores to be run.

Getting ready

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:

Getting ready

Network connection

This recipe requires that the following apps are installed on your device:

  • LinkedIn
  • Evernote
  • Google Maps (on iOS and Android)
  • The URL Scheme example recipe discussed later in this chapter

How to do it...

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'
};

iOS updating tiapp.xml

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>

Creating an app launch list

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.

iOS scheme list

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.

  1. The 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 =[];
  2. The most basic type of URL scheme is simply the 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'
      });
  3. More complex URL schemes can be used, such as the following example showing a URL scheme including the about route.
      apps.push({
        title:'Recipe Example 1',
        url:'bencoding-linkrecipe://about',
        errorMessage:'Check Link Receiver Recipe is installed'
      });
  4. URL schemes can also include query parameters similar to a web page, as demonstrated in the following code snippet:
      apps.push({
        title:'Recipe Example 2',    
          url:'bencodinglinkrecipe: //'+'login?user=chris&token=12345',
        errorMessage:'Check Link Receiver Recipe is installed'
      });
    
      return apps;
    };

Android scheme list

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 =[];
  1. Similar to iOS apps, you can use a route base URL to launch a third-party app. The following code snippet shows how to construct a URL with an 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'
      });
  2. Just as with web pages, you can build more complex URLs with query string parameters. The following snippet demonstrates how to call a login screen while passing query string information.
      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'
      });
  3. A more preferred way to launch apps on Android is to use intents. The following example shows how to create an intent to launch the LinkedIn app.
      var linkedInIntent = Ti.Android.createIntent({
        action: Ti.Android.ACTION_SEND,
        packageName :'com.linkedin.android',
        className:
        'com.linkedin.android.home.UpdateStatusActivity'
      });
  4. Once the LinkedIn intent has been created, it is added to the app's array with a type of intent. This will later be used to launch the LinkedIn app.
      apps.push({
        title:'LinkedIn',
        type:'intent',
        color:'#000',
        intentObject :linkedInIntent
      });

    Note

    The LinkedIn app must be installed on the user's device in order to have the intent launch the app. If the app is not installed, an exception will be generated.

  5. A more preferred way to launch apps on Android is to use intents. The following example shows how to create an intent to launch the Evernote app.
      var everNoteIntent = Ti.Android.createIntent({
        action: "com.evernote.action.CREATE_NEW_NOTE"
      });

    Note

    The Evernote app must be installed on the user's device in order to have the intent launch the app. If the app is not installed, an exception will be generated.

  6. Once the Evernote intent has been created, it is added to the app's array with a type of intent. This will later be used to launch the Evernote app.
      apps.push({
        title:'Evernote',
        type:'intent',
        color:'#000',
        intentObject :everNoteIntent
      });
    
      return apps;	
    };

The recipe's UI

This section of the recipe is the UI used to launch the third-party apps.

  1. First, a 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
    });
  2. Next the schemeList CommonJS module is called to return a list of the applications to launch.
    var schemeData = ((my.isAndroid) ? 
    my.schemes.getAndroidList() : my.schemes.getiOSList())
  3. The 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.

  1. A 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){
  2. Once the event is fired, the first step is to check the platform on which the recipe is running.
      if(my.isAndroid){
    
        try{
  3. If running under Android, a check must be performed to determine if the launch type is an intent or URL.
          if(e.rowData.type == "intent"){
  4. If the launch type is an 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{
  5. If the launch type is a URL, the 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{

    Note

    If the app is not installed, an exception will be generated.

  6. The iOS platform launches third-party apps using a URL scheme similar to how web pages are loaded using the 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});

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.103