Tip

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

    Requires Free Membership to View

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.

This was first published in February 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.