iOS Mobile Development. Titanium vs Native, a Dev’s Perspective

I have been developing native iOS applications since 2007, back when the first App Store was introduced, and have only been involved in writing native code (Objective C) mixed with some C/C++ (for usage with libraries and frameworks).

When Objective C meets Javascript.

It wasn’t until a few months ago that I started working with Appcelerator’s Titanium. For those who don’t know what Titanium is, it’s basically a developer environment that allows you to write apps for iOS, Android and web, all using one code base and without ever touching native code.
How it works is that you write these apps using Javascript (a very common language for web developers) and use the Titanium API to do things like network requests, create your UI, respond to touch events, etc.

While Titanium is a great way to do cross-platform mobile applications, it has its pros and cons. However, Titanium allows you to add extra functionality using the Javascript API through the use of modules. These are similar to libraries that are written using native code but work with the Javascript runtime.

If you want to support Android and iOS, then you’ll have to write 2 modules, one for each platform and, since it’s using Javascript, you’ll have to make sure you don’t misspell your variables since Javascript can create variable on the fly without a warning. The general idea about modules is to create the new functionality in native code and then export some functions so that they can work in Javascript, this is a whole different topic that we can cover in the future.

Diving into the depths of Titanium development

The first project I did with Titanium was an app that communicated with Apple’s movie trailers page. It retrieved a JSON file from Apple’s server, parsed it and converted it to useful data and then showed it in a table view.

movie trailers app created in titanium

Movie Trailers app created in Titanium

The way table views are created in Titanium is quite different from native Cocoa Touch. Both have table rows and a table header and footer, but in Titanium you have full control over the elements. Almost the same kind of control you have when manipulating <div>’s in HTML using Javascript.

Cocoa Touch doesn’t offer this kind of control because the behaviour of the table view is done through delegates. You can customize the table view, and the table rows, but it’s a bit limiting and requires you to do subclassing.

Also, the way events are detected, like selecting a row is different. Cocoa Touch offers a delegate method for detecting a selection. In Titanium, since it uses Javascript, you need to add event listeners to the row element, or even the entire table view.

But that’s not all, you can add event listeners to all elements you create, which makes virtually every visual component respond to a specific event. You can’t do that in Cocoa Touch, not for all elements at least. For example, you could add an event listener to a window in Titanium, but in Cocoa you can’t. You would need to add a subview and make that respond to events.

Event triggered when selecting a row

Event triggered when selecting a row

When it comes to handling network requests I must say that Titanium offered a very easy way to do this. As you can see in the image below, the Titanium SDK offers a class to retrieve data from over a network. Since the data I was interested in was in JSON format, and JSON is native to Javascript, I had not much to do with the data I received from the server.

Receiving data from a URL in Titanium

Receiving data from a URL in Titanium

In Cocoa Touch, it used to be a lot harder because you had to implement delegates and for some developers that was confusing and a lot of work. There were quite some delegate methods you needed to implement because many things could happen when you did a network request.

The more recent versions of the iOS SDK offer a simplified way of doing this: just one call, thanks to the blocks feature of the language.

Receiving data from a URL in Cocoa Touch

Receiving data from a URL in Cocoa Touch

The result and final thoughts…

In the end I finished the Movie Trailers application using Cocoa Touch, simply because I’m more familiar with it and because I wanted to add more features which I didn’t know how to do in Titanium. Some of those things include creating a grid view where each cell has rounded corners and has a drop shadow (generated by Core Graphics, an API for iOS) and some cool transition effects.

You can see the final version of the app in the screenshot below.

Final Movie Trailers - done in Cocoa Touch

Final Movie Trailers - done in Cocoa Touch

Like I said in the beginning of this article, Titanium is great for doing cross platform development, but for some other application which require delving deeper into what the OS has to offer you may be better doing it natively. But for general purpose apps Titanium is a great solution and it’s easy to learn.