John Resig - JavaScript iPhone Apps

John Resig - JavaScript iPhone Apps
sodanovels

sodanovels in iphone

A listing of various efforts to create a javascript interface for writing iPhone Apps.

Captured on 08 Jan 2010 from ejohn.org

JavaScript iPhone Apps


I've been watching, with interest, developers create new applications for the iPhone. Owning one myself - and being knowledgeable in JavaScript - I've been wondering what options there were for creating downloadable JavaScript applications or the iPhone. In doing some research I found a number of solutions, some simpler than others, some requiring more knowledge of Objective-C than others.

Before looking at the options I should note that given the requirements of the platform if you're really serious about getting in to iPhone development you should learn Objective-C. None of the options appear to provide the full range of functionality that a normally-written application would.

JiggyApp

This was an early entry to the JavaScript iPhone application market - arriving back in 2007. It requires a Jailbroken iPhone (likely running version 1.1 of the Operating System).

JiggyApp provides a full API for developing an application - apparently separate from most of the typical APIs. Arguably, though, the code ends up being relatively usable.

Plugins.load( "UIKit" );

var window = new UIWindow( UIHardware.fullScreenApplicationContentRect );
window.setHidden( false );
window.orderFront();
window.makeKey();
window.backgroundColor = [ 0.8 , 0 , 0, 1 ];

var mainView = new UIScroller();
mainView.contentSize = [ window.bounds[ 2 ] * 2 , window.bounds[ 3 ] * 2 ];
mainView.backgroundColor = [ 0 , 0 , 0 , 0 ];

window.setContentView( mainView );

var hello = new UITextLabel( [ 20 , 20 , window.bounds[ 2 ] - 40 , 100 ] );

hello.text = "Hello World!";
hello.backgroundColor = [ 0 , 0 , 0 , 0.25 ];
hello.setFont( new Font( "Trebuchet MS" , 2 , 46 ) );
hello.color = [ 1 , 1 , 1 , 1 ];
hello.centersHorizontally = true;

mainView.addSubview( hello );

The above snippet was from the Getting Started with Jiggy page.

JSCocoa

JSCocoa is a full bridge that maps Cocoa development into JavaScript (instead of the typical Objective-C/Cocoa mapping). The result ends up working in both OS X and on the iPhone.

It's a pretty-clear port of Objective-C style and mannerisms but with a JavaScript syntax. Note some of the differences:

Objective-C/Cocoa:

[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 40)];

JSCocoa:

NSButton.instance({ withFrame:NSMakeRect(0, 0, 100, 40) }) 

Right now it seems like JSCocoa is more usable for developing OS X applications but the progress moving forward is certainly promising.

"Applications" in MobileSafari

While it's not, technically, a true iPhone application one option is to adapt your existing web applications to be used in a more application-centric manner.

Apple provides a number of tips for improving the style of your web application. The most important points of which are:

  1. Providing a tray icon for the application (to be used when the user saves it).
  2. Providing a full-screen view of the application (with no MobileSafari toolbars showing).

This will give the full appearance of a regular iPhone application (after using some more styling and setup from iui, or similar).

PhoneGap

The next step to making your iPhone web application more native-like is to tap into some of the underlying native APIs. One recent release that will help is that of PhoneGap.

PhoneGap is an application that exposes a few JavaScript APIs to pages running MobileSafari. Right now this includes Geolocation and access to the Accelerometer.

Geolocation:

getLocation();

function gotLocation(lat,lon){
  document.body.innerHTML = "latitude: " + lat +
    " longitude: " + lon;
}

Accelerometer:

function updateAccel(){
  document.body.innerHTML = "accel: " + accelX + " " + accelY + " " + accelZ;   
  setTimeout(updateAccel, 100);
}

They're also working on access to the camera, sound, and vibration tools of the phone.

WebTouch

The other day "Dr Nic" wrote up an article on how he had used a WebKit instance (along with HTML, CSS, and JavaScript) to render a portion of his iPhone application.

I contacted him about the project and wondered if he'd be willing to provide some of the code that backs it. Written up by his co-worker at Mocra, Anthony Mittaz, the result is called WebTouch.

Right now it's just a zip file of sample code but hopefully it'll be expanded at some point.

It's really simple and gives you a good entry point into the world of hybrid HTML/CSS/JavaScript/Objective-C/Cocoa development. If you've been interested in Objective-C this might make for a good starting point, as well.

Bonus

While this isn't something that you can actually use, I think it's pretty cool. This guy ported my Processing.js work to the iPhone, writing his own Canvas implementation using OpenGL ES hooking in to SpiderMonkey.

 

There are a lot of options available for the discerning JavaScript developer - the most promising of which is, I think, WebTouch. It's pretty obvious that in order to be able to build the best possible iPhone application you would have to know Objective-C. Having a clear path, paved with JavaScript, to that end result should be any JavaScript developer's goal.

Posted: November 18th, 2008 · Tags: iphone, javascript, apple · ♻ Retweet

20 Comments (Show Comments)

  1. Daniel Lucraft (November 18, 2008 at 8:19 am)

    Don't forget there's also the HTML5 SQL storage option. http://ichi2.net/anki/wiki/iAnki manages to have offline storage and a usable application working, and all you have to do to install is navigate to the page and bookmark it.

  2. Patrick Geiller (November 18, 2008 at 8:30 am)

    Hello ! I'm the JSCocoa dev. Now that JSCocoa works on the simulator, I'm looking for someone to test JSCocoa on the actual device. Anyone interested please send a mail at parmanoir@gmail.com

    Also, a limitation on the iPhone :
    On OSX, bridgesupport files describes C functions, enums, constants — that's how you can call C functions like CGContextSetShadow() and use constants like kCGColorWhite.
    On the iPhone, no bridgesupport files exist — you must create them yourself.

  3. Brian (November 18, 2008 at 9:40 am)

    Another possibility for creating download iPhone JS apps is to create a "shell" obj-c application that only contains a WebUIView. This WebUIView can expose different APIs available only in Obj-C (like the accelerometer) to the Javascript code in a currently loaded page. This technique is also possible with Android.

  4. John Resig (November 18, 2008 at 9:58 am)

    @Brian: That's exactly what WebTouch is - just check out the zip for a full "shell" application.

  5. Howard Katz (November 18, 2008 at 10:05 am)

    Hi John, have you come across Cappuccino and Objective-J?

    http://cappuccino.org/learn/tutorials/objective-j-tutorial.php

    Very impressive!
    Howard

  6. Howard Katz (November 18, 2008 at 10:13 am)

    I should hasten to add that Cappuccino and Objective-J aren't for building iPhone web apps specifically, but rather for building multi-platform web apps using a Javascript-based language that's astonishingly close to Cocoa and Objective-C. Presumably (among other virtues), this makes it extremely easy to eventually move your app code over onto the native iPhone platform, if that's where you're ultimately interested in going.
    Howard

  7. Stan Wiechers (November 18, 2008 at 11:13 am)

    Check out me example for the google android g1. The g1 comes with google gears !!preinstalled!!:

    http://www.semapedia.org/merkwelt

    if you don't have the g1 you can test the app on a regular browser as well it just gets your location via the IP address.

    Like it?
    Stan

  8. Brian LeRoux (November 18, 2008 at 11:46 am)

    Hey John, thanks for mentioning PhoneGap!

    Something not currently clear by the website is that PhoneGap also allows native features within Android and Blackberry: with more devices on the way. We are very committed to building web apps.

    Sure, obj-c is nice but writing your app three times is barbaric.

  9. Steve Streza (November 18, 2008 at 12:03 pm)

    As a heads up, Apple added DOM events for getting multi-touch and gesture events in iPhone 2.0. They are also return-falseable, so you can disable scrolling and stuff like that.

  10. Dr. Drang (November 18, 2008 at 1:24 pm)

    The "transfer and view" apps, like Files, allow you to copy HTML files and JS libraries to your iPhone. When you open the HTML in the viewer (which is based on WebKit), the libraries load and you have a "web" app running locally on your iPhone or iPod Touch. No need for an internet connection, no wait for a large library to download over a slow connection.

    It seems like a quick way to make simple apps for yourself, with no need for a developer kit. I have a (slightly) more detailed description at

    http://www.leancrew.com/all-this/2008/11/local-html-and-javascript-on-the-iphone/

  11. Timothy (November 18, 2008 at 4:36 pm)

    That's sick

  12. William J. Edney (November 18, 2008 at 7:52 pm)

    John -

    Unfortunately, Mobile Safari limits JS alloc'ed memory to 10MB. In my experience of writing more or less full-blown apps using JS, this is about an order of magnitude too low.

    I guess we need something like a 'Prism for iPhone'... of course, Apple wouldn't allow it... sigh.

    Cheers,

    - Bill

  13. Lee (November 19, 2008 at 11:50 am)

    You missed QuickConnect. It comes in both iPhone and Mac flavors. It allows complete writing of the application in JavaScript and allows you access to location, acceleration, vibration, OS sounds, recording and playing audio on the device. Within the week it will also include embedding Google maps that look like the Map app in your JavaScript Application.

    The download includes Xcode and Dashcode templates that allow you to create new applications just like using the built-in templates that ship with Xcode and Dashcode.

    It also includes easy to use wrappers for accessing the SQLite database and AJAX.

    There is an example application called DeviceCatalog that ships with the framework download.

    There is ab example Dashcode project that shows how you can develop an application completely in Dashcode and then drop it into the Xcode framework and compile it up.

    You can download it from http://sourceforge.net/projects/quickconnect/.

    The blog covering its' development is found at http://tetontech.wordpress.com

    Of all the options around this one has been under development the longest. Development actually started 4 years ago.

    The idea is that the framework handles all of the 'linking code' that you usually would have to write in an application. You get to focus on the code that is the brains of the application. The data access/manipulation, and the view creation/showing. Most everything else is already done for you.

  14. Peter Robinett (November 19, 2008 at 5:15 pm)

    John, why do you prefer WebTouch over PhoneGap? The two strike me as very similar...

  15. perenzo (November 21, 2008 at 11:56 am)

    Another framework to get native applications using HTML and Javascript:
    http://www.big5apps.com

  16. Dr Nic (November 24, 2008 at 8:42 pm)

    WebTouch now pushed to a git repo http://github.com/mocra/webtouch/tree/master

  17. Ken Law (December 8, 2008 at 4:17 am)

    Hi John,

    MotherApp is different from some of the existing frameworks in that we generate 100% Objective-C application. This means:
    - It is not just an iPhone app enclosed by Webkit
    - You can create native UIs such as tab and access phone features such as the camera
    - We enable things like downloading images on a separate thread and caching large files and images

    For more details, take a look at:
    http://motherapp.com/

    Best,

    Ken

  18. Perenzo (December 17, 2008 at 2:54 am)

    Finally Big Five is available in the Apple App Store so you can use native features of the iPhone in your Web App without additional software: http://www.big5apps.com/

  19. Hasan Tayyar BE??K (January 26, 2009 at 8:22 am)

    Hey I can't open the site. www.jiggyapp.com is still alive?

  20. Den (March 7, 2009 at 9:26 pm)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


JavaScript Books

Secrets of the JavaScript Ninja

JavaScript Secrets

Secret techniques of top JavaScript programmers. Coming Fall 2009.

Pro JavaScript Techniques

Pro JavaScript

The best techniques for professional JavaScript. Published by Apress.

Micro Updates

John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.

JavaScript Jobs



Hosting provided by: Ruby Hosting by Engine Yard

Responses

Please Login to respond

Get Gleanr!

What is Gleanr?

Gleanr is the networking engine for digital-age professionals. Get impact (& income!) in the information streams you care about.

How does it work?

Your custom Gleanr channels automate information flow relevant to you. All you do is "click" - we do the rest (instant capture, indexing, and networking).

What is the value?

Gleanr is the only web service where professionals can manage and monetize their expertise.

Is this more web 2.0?

Yes, but for work. Now you can capitalize on your unique ability to filter and enrich professional information streams.

Show me!

Explore the public parts of professional information streams here, or take the Gleanr Tour.

Sign me up!