Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Use JavaScript with the iPhone to create smart phone apps

Objective-C remains the most prevalent iPhone development language, but JavaScript has been gaining traction as a viable alternative. You can use JavaScript with the iPhone to create smart phone apps.

While Objective-C remains the most powerful iPhone development language, JavaScript has been gaining traction as a viable alternative, particularly for basic applications and rapid prototyping. The benefits of JavaScript are a far larger audience of programmers and development tools, and a number of bridges have emerged to help JavaScript take advantage of native iPhone resources.

JavaScript alone does not give the developer access to all of the native resources of the iPhone but there are ways of accessing iPhone functionality for basic applications or prototyping. For more sophisticated apps developers will have to turn to Objective-C the native Mac programming language, which Simon Brocklehurst points is not nearly as popular as other languages like JavaScript.

One of the problems is that JavaScript must be interpreted by the WebKit embedded browser. As Dr. Nic noted, WebKit apps are slow. It can take a few seconds for the WebKit object to become available. It also suffers from running inside a JavaScript interpreter on top of a limited processor with small memory. Another drawback is that the bridge is only one directional. From Objective-C you can call JavaScript, but you can't invoke native Objective-C objects. However, he notes that JavaScript is a sweet option for rapid prototyping, particularly if your objective-C skills are limited because it gives the designer immediate access to building the app.

The limitations are also becoming less onerous. The second version of the iPhone firmware has doubled JavaScript performance compared to the first release. This was made possible with an update to the WebKit rendering engine. The iPhone 3G processor is also 35% faster than the original hardware, which can also enhances performance.

John Resig has summarized his take on different strategies for writing JavaScript apps for the iPhone using JiggyApp, JSCocoa, tuning apps, PhoneGap, and WebTouch.

JiggyApp was one of the first iPhone scripting languages. Unfortunately it required a jailbroken iPhone, and the main site has since died. It gained a lot of publicity as it enabled a developer to code right from the phone.

JSCocoa is a bridge from JavaScript Core to Cocoa, allowing developers to create Objective-C applications using JavaScript. Developers can call C code, Objective-C code, and build JavaScript classes inheriting from Objective-C classes. Resig believes it is better suited for OX-X applications. Note that the main JSCocoa site has documentation and tips and tricks. The Google code site has more documentation and downloads for writing and testing apps.

Tuning Web Apps
Developers can tune their JavaScript mobile web application code to look a little more like native iPhone apps by doing things like providing a tray icon and a full screen view without the browser toolbar. However, these kinds of apps have limited access to native iPhone resources.

PhoneGap bridges the gap between web apps and native iPhone APIs for resources like the accelerometer and geoloaction. Future versions will support the camera and audio features. The goal of the project is to drive the adoption of open device standards for JavaScript applications running on major mobile platforms including the iPhone, Android, and Blackberry.

Meanwhile, WebTouch is a WebKit instance to render an iPhone application. At the moment, the project only provides a few snippets of sample code, but Resig believes it is a good entry point for developing hybrid HTML/CSS, JavaScript/Objective-C/Cocoa apps.

Dig Deeper on Topics Archive

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.