Would you like to contribute to the Titanium docs? To get started, sign up for an account on the Appcelerator Wiki and sign our CLA.

Skip to end of metadata
Go to start of metadata

Introduction

This guide walks through the steps to create, build and test an iOS module using Studio.  The equivalent CLI instructions are given in the information boxes near the top of each section.

iOS Module Prerequisites

To develop an iOS-based Module, you'll need all of the software required to build a Titanium application for iOS:

  • Titanium SDK
  • Supported versions of Xcode and the iOS SDK, as described in Installing the iOS SDK
  • Studio or the Appcelerator Command-Line Interface (CLI) for creating modules, and building and running test applications

In addition, Python, Python setuptools and the Python markdown (or markdown2) module are both required by the module build scripts. For Mac OS X, Python and the Python setuptools should already be installed.  For the markdown module, see Installing Required Python Packages.

Like iOS application development, iOS module development is only supported on OS X.

Create a New Module

First, create a new module project.

CLI Instructions

Icon

 From a terminal, change the current working directory to your workspace and run:

In Studio:

  1. From the menu, select File > New > Mobile Module Project to open the New Mobile Module Project dialog.
  2. In the Project name field, enter test.
  3. In the Module Id field, enter com.example.test.
  4. In Deployment Targets, select iOS.
  5. Click Next, then click Finish.

Studio sets up a new folder called test that contains your module project.

Build and Package the Module

Next, build the module and package it.  This process produces a ZIP file containing a binary library with unprocessed module assets, example code and documentation.

CLI Instructions

Icon

From a terminal, go to the module's iphone directory and run the build.py script:

After the build completes, unzip the built module in the Titanium SDK home path:

In Studio:

  1. Select your module folder in the Project Explorer view.
  2. Verify Package and iOS Module are displayed in Launch Mode and Launch Target, respectively.
  3. Click the Package icon to open the Package iOS Module dialog.
  4. In Output Location, choose the Titanium SDK to install the module in the Titanium SDK home path to be accessed by any Titanium application.
  5. Click Finish.

Studio builds and installs the module to the Titanium SDK home path.

Test the Module

To test the module, create a test application and add the module as a dependency of the project.  Then, load the module and make module API calls to the module reference.

Create a Test Application

CLI Instructions

Icon

From a new terminal window, change the current working directory to your workspace and run the following commands:

In Studio:

  1. From the menu, select File > New > Mobile App Project to open the New Mobile App Project dialog.
  2. On the Project Template page, select Default Alloy Project as the template type, then click Next.
  3. On the Project Location page, enter the following information:
    • In the Project Name field, enter Hello.
    • In the App ID field, enter com.example.hello.
    • In Deployment Targets, select iPhone and iPad.
  4. Click Finish to create the project. 

Studio sets up a new folder called Hello that contains the test application you will be using to test the module.

Add the Module as a Dependency to the Project

To load the module in the application, you need to add it as a dependency to the project.

CLI Instructions

Icon

Open the tiapp.xml and update the <modules/> element to include the module as a dependency to the project: 

In Studio:

  1. Open the tiapp.xml file located in the root directory of the project.

  2. Under the Modules section, click the Add button.

  3. Select com.example.test.
  4. Click OK.

Load the Module and Make Module API Calls

Icon

The module can be loaded by passing the module ID to the require() method, which returns a reference to the module that API calls can be made on.

Open the app/alloy.js file and replace the code with the following, which invokes API calls to the module:

app/alloy.js

Run the Application

CLI Instructions

Icon

From a terminal that has the test app as its current working directory, run:

In the Studio toolbar, select Run in Launch Modes and select an iOS simulator in Launch Targets.

Studio builds and launches the application on the select iOS simulator.  Monitor the Console view for log output.

The console lines seen below show us that the module is working as expected.

Console

Modify the Module

Let's modify the module code to create a view object and access a string property.

Open the Module in Xcode

Titanium creates a basic Xcode project, which is used to build the module.  You can open this project in Xcode, the IDE used to develop iOS applications and used by the Titanium toolchain to build your iOS applications and modules.

CLI Instructions

Icon

From a terminal, run:

In Studio:

  1. Right-click the test.xcodeproj folder and select Show InTerminal.
  2. In the Terminal, run the following command: 

Your module project is now open in Xcode.  Expand the Classes folder and take a look at the default files created by the Titanium SDK:

  • ComExampleTestModule.h and ComExampleTestModule.m: These are the header and source file for the module class.  Every module requires a module class (and only one module class), which acts as the base API for the module, such as providing the module ID, GUID, etc.
  • ComExampleTestModuleAssets.h and ComExampleTestModuleAssets.m: These are the header and source files to manage module assets.  These files are auto-generated.  You can ignore these for now in this tutorial.

Notice that all the files start with the module ID in camel case notation.  Every file and class you add to the module project must start with name and every file and class you add must end with ProxyViewProxy or View, which determines how Titanium uses the files.  Titanium uses a strict naming convention and directory structure to manage the module classes and resources. If a file or class is added to the project and does not conform to these conventions, it will be treated as a normal non-Titanium class. It will be accessible from Objective-C code but not from JavaScript.

Add a View Proxy and View

To display any UI with a module, create a view proxy and view in pairs.  Create the four files below for your Xcode project and save them in the module's iphone/Classes directory.

In Xcode, for each file:

  1. Right-click the project and select New File...
  2. For the header files, select Header File, and for the source files, select Objective-C File, then click Next.
  3. For the Objective-C files, enter the name of the file and click Next to proceed to the last dialog.
  4. For the header files, enter the name of the file in the Save As field.
  5. For both files, select the module's Classes folder.
  6. Ensure that the Target test is selected.
  7. Click Create.

 

ComExampleTestViewProxy.h
ComExampleTestViewProxy.m
ComExampleTestView.h
ComExampleTestView.m

 

The ComExampleTestViewProxy class extends the TiViewProxy class.  This class exposes the view to the JavaScript and acts as an intermediary between the JavaScript and the native view.  Normally, you do not need to implement any APIs in this class, but you can hook into the View's lifecycle events. 

The ComExampleTestView class extends the TiUIView class. The TiUIView can be added to other Titanium views and windows, which makes it the perfect place for a UIView to be added so that it can be displayed in a Titanium app. This class creates the native view to display.  The class implements three methods of the TiUIView class and a custom setter method:

  • initializeState: This method is called when the view is initialized. In this example, we are using this method as a place to create a native UIView which is called square.
  • dealloc: This method is called to release the view. We use this method to release square.
  • frameSizeChanged:  This method is called when the view's dimensions change. The method calls a TiUtils helper function to update the dimensions of square. In JavaScript, this occurs when the width, height, top, bottom, left or right properties are invoked.
  • setColor_: All setter methods in a View class must end with an underscore (_), which exposes the property to the JavaScript application.  When color property is invoked, the method updates the background color of the square.

Notice that there is no code tying ComExampleTestView to ComExampleTestViewProxy. The naming convention is what causes the View to be connected to its ViewProxy.

To call the method to create the view from JavaScript, call the module's createView() method.  The name of the method is the name of the view class without the module ID, then prefixed with create.  For example, if the class was called ComExampleTestMyView (rather than ComExampleTestView), the method would be called createMyView() (rather than createView()).

Below is an example of calling createView(), and passing dimensions and color properties to the method.

Example

Add a Property

A Proxy is a key/value store like an NSDictionary (Objective-C) or an Object (JavaScript). Without any modification, you can set properties on a Module, Proxy, or ViewProxy and then read them back at will as if they were properties. You can also override the getters and setters and do some custom logic.

Modify the default module class files to store and retrieve a string value.

First, modify the ComExampleTestModule.h file to declare a variable to hold the string: 

Next, modify the example setter and getter to actually set and get the variable you just declared. These methods are already declared in the ComExampleTestModule.m file but not implemented. Titanium requires that all setter methods be declared with the method name starting with set and being passed an id datatype.

In the JavaScript code, the foo string can be accessed using the exampleProp property, and getExampleProp() and setExampleProp() methods.

Icon

If you do not need a custom getter or setter, you can use the @property notation with the copy attribute to create a property.

Test the Module

Open the app/views/index.xml file and replace the code with the following, which loads the module and displays a red square:

app/views/index.xml

Open the app/controllers/index.js file and replace the code with the following, which invokes API calls to the module:

app/controllers/index.js

Build and install your module, then run the example app. 

When the application starts running, you see should a red square in the middle of the screen and see the log output below, which means the application successfully loaded the module and called its APIs.

Console

Next Steps

  • For information about how to structure your module project, add assets or third-party frameworks to your module project or more details on how to use the CLI or Studio, see iOS Module Project.
  • For information about how to construct the class components for your project, see iOS Module Quick Start.
  • For more examples of using the module API, see the ti.moddevguide Github project.

 

  • No labels

1 Comment

  1. foo variable should be a class member of ComExampleTestModule inside the ComExampleTestModule.h file else it will error out as Undeclared identifier foo variable when you would try to use it inside the ComExampleTestModule.m file. Thanks