Titanium 3.X
Using a Module

Using a Module

Objective

You can install a module so that it is available to a single project, or all projects you create. Then you will see how to use a module within your project.

Installing modules

You can install a module for a single project, or that all Studio projects can use. Once you've installed the module, you then configure it for use in your project (see Configuring your app to use a module).

To install a module:

  1. In Studio, select Help > Install Mobile Module.

  2. In the Module dialog, enter the URL of the module's ZIP file, or click Browse and locate the ZIP file.

    images_download_attachments_30082372_install_dialog.png
  3. For Output Location select one of the following:

    • To install the module so it's available to all projects, select Titanium SDK. See Module install locations.

    • To install the module so its available to a single project, select Mobile App Project, and then choose the desired project from the pop-up menu.

    • To install the module to a folder location, select Location and then browse to the desired output folder.

  4. Click OK.

Module install locations

When you install a module that's available to all projects, the module is installed to one of the following locations, depending on your platform.

Operating System

Local Path

OS X 10.6 (Snow Leopard) and earlier

/Library/Application Support/Titanium

OS X 10.7 (Lion) and later

~/Library/Application Support/Titanium

Windows 7

%ProgramData%\Titanium\mobilesdk\win32

Windows XP

C:\Documents and Settings\All Users\Application Data\Titanium

Linux

~/.titanium/mobilesdk/

By default on Mac OS X 10.7 and later, the ~/Library folder is hidden by default. To permanently show it, run the following command from a terminal:

chflags nohidden ~/Library/

To open it once, run the following command from a terminal:

open ~/Library

Configuring your app to use a module

Once you have installed the module, you must configure your application to use it. This involves two steps:

  • Add the module as a dependency in your application's tiapp.xml file.

  • Call the require() method in your JavaScript code to load the module

Updating the tiapp.xml file

You can update your application's tiapp.xml file visually, or manually in a text editor.

To add a module to your project:

  1. In Studio, open your project and double-click its tiapp.xml file.

  2. Click the Overview tab in the bottom-left of the Editor window.

  3. In the Modules section, click the green add ( + ) button.

    images_download_attachments_30082372_addbutton.png
  4. Select the module you want to add and click OK.

    images_download_attachments_30082372_tiapp.png

    Note the version numbers listed at the bottom of the dialog. See Selecting a module version.

  5. Save your changes to tiapp.xml.

To manually add a module to your project's tiapp.xml file, modify the <modules/> tag in the <ti:app> node as follows:

<!-- $MODULE_VERSION should be the same as "version" in the module manifest and directory number -->
<modules>
  <module version="$MODULE_VERSION">$MODULE_ID</module>
  <!-- For example, if we were adding the calc module: -->
  <module version="0.1">org.appcelerator.calc</module>
</modules>

Selecting a module version

A installed module may include one or more versions that your application can use, as well as versions for production, deployment and testing. By default, Studio selects the latest version of a module when you install it.

To select a specific module version:

  1. In the Modules section of tiapp.xml, double-click on the module to opens the Module Properties dialog.

  2. For each platform, select the version of the module you want to use.

  3. Click the arrow next to the platform to expand it to select to use the module for specific build types.

    images_download_attachments_30082372_version.png
  4. Click OK.

  5. Save tiapp.xml.

Using require() to load the module in the app's code

Within your app's JavaScript files, you'll instantiate the module via the require() function:

var Module = require('$MODULE_ID');
// For example, to load the calc module:
var Calc = require('org.appcelerator.calc');

Finally, you'll use the module's object, properties, and methods to enable its features and functionality. Each module distributed in the Marketplace should include documentation and a sample app that demonstrates the basic use of the module. That information would be a great place to start with learning how to use a specific module.

Example Module Use

The following section illustrates how you would use one of the modules included in the Titanium Plus set.

For this example we will be downloading the AdMob module for iOS available on GitHub. While the example covers an iOS use case, the same general steps apply to an Android module. The ZIP file containing the module distribution can be downloaded here.

  1. Install the module as described above.

  2. Require the module in your project with the following code:

    var admob = require('ti.admob');
  3. You can now call methods on the admob singleton object:

    var adview = admob.createView({
        top:0,
        testing:true,
        adBackgroundColor:'black',
        primaryTextColor:'blue',
        secondaryTextColor:'green',
        publisherId:'<< your ID>>' // Replace this string with your own API key!
    });
    win.add(adview);

Troubleshooting

Requested module could not be found

If you receive an error saying: "Requested module not found." Check the name and version number of the module in the tiapp.xml file. Make sure that the version of the module is installed either locally or globally. You can also remove the version attribute from the module element to use the most recent version of the module.

Summary

In this section, you learned that you can install a module for use by a single project or by all projects that you develop on a computer. Once installed, you have to modify your app's tiapp.xml file to support the module. Then, you must require the module into your app's code before you can implement its functionality.