Jeff English: Demystifying Module Development - How to Extend Titanium

25
Demystifying Module Development Jeff English

description

The ability to extend your application with custom functionality, whether an external library or your own custom library, opens up numerous possibilities for what you can create. Titanium enables this with what are called 'modules'. Modules provide a bridge between your Javascript application code and your native library code. Getting started writing your own modules can seem confusing, but once your understand the architecture of a module and how it integrates with the Titanium platform you will be able to add your own functionality in short order.In this session you will learn: How to create your module project How to call methods and pass parameters to your module from your Javascript code How to make callbacks into your Javascript code from your moduleThis session is for anyone interested in understanding module development and how to get started.Jeff English is the Modules Development Manager at Appcelerator. Jeff and his team designed and developed many of the Appcelerator modules found in the Marketplace.

Transcript of Jeff English: Demystifying Module Development - How to Extend Titanium

Page 1: Jeff English: Demystifying Module Development - How to Extend Titanium

Demystifying Module Development

Jeff English

Page 2: Jeff English: Demystifying Module Development - How to Extend Titanium

Jeff English

•  30+ years of software development

•  Cross-platform SDKs, operating system development, games, mobile encryption management, et. al.

•  50+ consumer, commercial, and enterprise applications

•  “Real-World Programming for OS/2”

Engineering Manager, Modules [email protected]

Page 3: Jeff English: Demystifying Module Development - How to Extend Titanium

Copyright © 1992, Scott Adams

Dilbert, September 8, 1992

Page 4: Jeff English: Demystifying Module Development - How to Extend Titanium

What Are Modules?

Extensions to the core Titanium platform Titanium itself is written as a collection of modules

Written in native code iOS (Objective-C), Android(Java), JavaScript (JavaScript)

Provide a bridge between JS and native code Methods, Properties, Notifications, Data Types, Assets

Page 5: Jeff English: Demystifying Module Development - How to Extend Titanium

Life Cycle Events Module loading, proxy loading, view proxy loading

Method Parameters Type validation, type conversion, pass-by-value / reference

Properties Types, getters, settings, property change notifications

Callbacks & Events Event registration, asynchronous notifications, synchronous callbacks

Methods Method signatures / annotations

Miscellaneous Assets, documentation, example, license, manifest, packaging

Module Features

Page 6: Jeff English: Demystifying Module Development - How to Extend Titanium

Basic Module Development (iOS)

Create a module titanium create --platform=iphone --type=module --dir=. --name=calc --id=com.companyname.calc

Build a module ./build.py

Test a module titanium run

Deploy a module Copy module zip file into system folder or application folder

Page 7: Jeff English: Demystifying Module Development - How to Extend Titanium

Basic Module Development (Android)

Create a module titanium create --platform=android --type=module --dir=. --name=calc --id=org.companyname.calc --android=/usr/android-sdk

Build a module ant

Test a module ant run

Deploy a module Copy module zip file into system folder or application folder

Page 8: Jeff English: Demystifying Module Development - How to Extend Titanium

Basic Module Architecture

JavaScript

Module (TiModule / KrollModule)

Proxy (TiProxy / KrollProxy)

View Proxy (TiViewProxy)

require

create

createView

View (TiUIView)

Page 9: Jeff English: Demystifying Module Development - How to Extend Titanium

JavaScript / Module Interactions

var m = require(‘…’);!

m.fooInt = 42;!

m.fooString = ‘Hello World’;!Ti.API.info(m.fooString);!

m.foo({ X: 5, Y: 10 });!

m.addEventListener(! ‘myEvent’,! myHandler);!function myHandler(e) {! Ti.API.info(‘myEvent’);!}!

fooInt property

fooString property

foo method

Signal Event

Page 10: Jeff English: Demystifying Module Development - How to Extend Titanium

JavaScript / Proxy Interactions

var m = require(‘…’);!

p.fooInt = 42;!

p.fooString = ‘Hello World’;!Ti.API.info(m.fooString);!

p.foo({ X: 5, Y: 10 });!

p.addEventListener(! ‘myEvent’,! myHandler);!function myHandler(e) {! Ti.API.info(‘myEvent’);!}!

fooInt property

fooString property

foo method

Signal Event

var p = m.createBar(…);! createBar method

Page 11: Jeff English: Demystifying Module Development - How to Extend Titanium

JavaScript / ViewProxy Interactions

var m = require(‘…’);!

p.fooInt = 42;!

p.fooString = ‘Hello World’;!Ti.API.info(m.fooString);!

p.foo({ X: 5, Y: 10 });!

p.addEventListener(! ‘myEvent’,! myHandler);!function myHandler(e) {! Ti.API.info(‘myEvent);!}!

fooInt property

fooString property

foo method

Signal Event

var p = m.createView(…);! createView method

Page 12: Jeff English: Demystifying Module Development - How to Extend Titanium

Proxy Properties

Properties can be set in the constructor Setter is automatically called

Properties can be set directly after proxy creation

Many primitive values are automatically converted

iOS Properties Getters and Setters can be defined by using @property or property signatures

Android Properties Getters and Setters can be defined by using @Kroll.getProperty and @Kroll.setProperty annotations

Page 13: Jeff English: Demystifying Module Development - How to Extend Titanium

Proxy Methods

Module methods can be exposed to JS

iOS – Signature determines availability (id)methodName:(id)args (void)methodName:(id)args

Android – Annotations determine availability @KrollMethod Use KrollInvocation if your method requires the current activity

Page 14: Jeff English: Demystifying Module Development - How to Extend Titanium

Method Parameters

Parameters can be passed from JS to your methods

iOS All parameters are passed in an NSArray object Use TiUtils methods to get correct datatype

Android All parameters are passed as defined – KrollDict recommended Use validation methods (e.g. TiConvert) to get correct datatype

Page 15: Jeff English: Demystifying Module Development - How to Extend Titanium

Callbacks and Events

Methods exist for notifying JS of module events Recommended to use events rather than callbacks when possible

iOS fireEvent and _fireEventToListener call

Android fireEvent and fireSingleEvent callAsync and callSync

Page 16: Jeff English: Demystifying Module Development - How to Extend Titanium

Miscellaneous

Assets

iOS Build Options titanium.xcconfig

Android Build Options timodule.xml

Manifest

Documentation

Page 17: Jeff English: Demystifying Module Development - How to Extend Titanium

Miscellaneous (cont)

LICENSE

Example

Page 18: Jeff English: Demystifying Module Development - How to Extend Titanium

Module Development Guide Reference Module

Life Cycle events

Kroll usage

Cut-and-paste example code

Page 19: Jeff English: Demystifying Module Development - How to Extend Titanium

Module Load

Module Unload

Page 20: Jeff English: Demystifying Module Development - How to Extend Titanium

Proxy Create

Proxy Destroy

Page 21: Jeff English: Demystifying Module Development - How to Extend Titanium

ViewProxy Create

Page 22: Jeff English: Demystifying Module Development - How to Extend Titanium

ViewProxy Create

Page 23: Jeff English: Demystifying Module Development - How to Extend Titanium

Parameters Demo

Page 24: Jeff English: Demystifying Module Development - How to Extend Titanium

Module Development Resources

Module Development Guide for iOS

Module Development Guide for Android

Open source modules available on GitHub https://github.com/appcelerator/titanium_modules

Module Development Guide reference module moddevguide

Page 25: Jeff English: Demystifying Module Development - How to Extend Titanium

Questions