Editor’s note: The most up-to-date version of this tutorial is available in our wiki titled, Configuring Apps to Use Modules.
One of the amazing and powerful features of Titanium Mobile, is the ability to extend the platform using modules. These modules are written and compiled in the native language of the OS and allow for your app to use features and APIs not part of the Titanium Mobile core.
How do you install these modules?
- First off, download the module you want to use, for this example we will be downloading the AdMob module for iOS. The module can be downloaded here.
- The module is downloaded as a zip file, so un-zip it if it hasn’t automatically been expanded. You should now have a folder called modules. Inside that folder there will be another folder called iphone. Inside the iphone folder there should be another folder called ti.admob.
- Navigate to your /Library/Application Support/Titanium/modules directory. (note that it is in the root Library, not the one based off of the users folder) If there is not an iphone folder already inside the modules folder, create it.
- Drag the ti.admob folder inside this iphone folder, and your done!
How do you use the modules?
- In order to use a module in a Titanium Mobile project, you have to explicitly state the module(s) your app is using within the tiapp.xml file. In your tiapp.xml file you should find the following code near the end of the file… You simply need to alter that to this: It is important to include the correct version number. If you don’t know what version you have installed, just navigate back to the /Library/Application Support/Titanium/modules/iphone/ (again, note the root Library) folder and inside the ti.admob directory you should see another folder with a version number. Include this version number in your code.
- The next step is the use of the module in your app, to do so we simply need to require (not Ti.include) the module with the following code:
- And finally its time to use the module:
It might look like a lot of work at first, but once you get the hang of it its rather easy.
In this repo (in the admob directory) we have a sample app that makes use of the admob module for you to import into Titanium Studio and examine for your own use. While the code is all there, you will still need to install the module itself using the first set of instruction, but this will provide you with code that you know to be functional as long as the module is installed.