How to write an iPhone application completely in JavaScript without making it just a web application?

I do not want to waste time learning Obj-C. I spent 7 years programming web applications. Shouldn't I use WebView and just write the whole application in javascript by pulling files directly from the project resources?

+83
javascript ios objective-c
Sep 15 '08 at 16:27
source share
11 answers

I found the answer after searching. Here is what I did:

  • Create a new project in Xcode. I think I used a presentation based application.

  • Drag the WebView object onto your interface and resize.

  • Inside your WebViewController.m (or similarly to the file name, depending on the name of your view), in the viewDidLoad method:

      NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "index" ofType: @ "html"];  
     NSData * htmlData = [NSData dataWithContentsOfFile: filePath];  
     if (htmlData) {  
       NSBundle * bundle = [NSBundle mainBundle]; 
       NSString * path = [bundle bundlePath];
       NSString * fullPath = [NSBundle pathForResource: @ "index" ofType: @ "html" inDirectory: path];
       [webView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: fullPath]]];
     } 
  • Now any files that you added as resources for the project are available for use in your web application. I have an index.html file including javascript and css and image files without problems. The only limitation I have found so far is that I cannot create new folders, so all files clutter up the resource folder.

  • Trick: make sure you add the file as a resource in Xcode or the file will not be available. I add an empty file to Xcode and then drag and drop the file at the top to finder. This worked for me.

Note. I understand that Obj-C should not be so difficult to learn. But since I already have this application that exists in JS, and I know that it works in Safari, for me it is much faster. One day I'm sure that I will have to break down and learn Obj-C.

A few other resources that I found useful:

Call Obj-C from javascript: call objective-c from javascript

Javascript call from Obj-C: developing iphone apps for web hackers

Reading Files from the Application Suite : uiwebview

+71
Sep 15 '08 at 17:07
source share

Check out PhoneGap at http://www.phonegap.com , which claims to allow you to embed JavaScript, HTML, and CSS into your native iPhone app.

+22
Oct 22 '08 at 22:47
source share

For those who do this on the iPhone 2.1 (possibly 2.0), you do not need to create special services for local data storage. MobileSafari seems to support the HTML5 / WHATWG SQL database API. This is the same API that is supported by the latest versions of the desktop Safari and Firefox.

If you use a tool kit like Dojo or ExtJS that offers storage abstraction, your code should work with almost any modern browser, including MobileSafari.

To check, open http://robertsanders.name/dev/stackoverflow/html5.html on your iPhone.

If you open this page, look at the Jailbroken iPhone file system, you will see a database somewhere in / private / var / mobile / Library / WebKit / Databases /. There is even a catalog of databases opened on the Internet.

root # sqlite3 / private / var / mobile / Library / WebKit / Databases / Databases.db SQLite version 3.5.9 Enter ".help" for the statement

sqlite> .databases seq name file




0 main / private / var / mobile / Library / WebKit / Databases / Databases.db

sqlite> .tables

Database sources

sqlite> select * from the Database;

1 | http_robertsanders.name_0 | Note | Database | API Example | 20000 | 0000000000000001.db

sqlite> select * from Origins;

http_robertsanders.name_0 | 5242880

+7
Sep 21 '08 at 23:29
source share

You can create an application without knowing any obj-C. This allows you to use QuickConnectiPhone. Check out http://tetontech.wordpress.com for how to use it, as well as other ways to do what you requested.

+4
Oct 24 '08 at 4:12
source share

You must have your own shell written in Objective C. This shell can actually contain several lines of code (for example, 10) needed to create a WebView, and move it to a given address on the Internet (where your application is located). But in this case, your application should be a full-featured web application (I mean using not only JavaScript, but also HTML for markup).

+2
Sep 15 '08 at 16:35
source share

I ran into this problem. I already have a game written entirely in Javascript. I would like to make the iPhone a friendly version, but Obj-C is redundant. As a result, I used WebView to point to the special URL of the iphone application. Thinking about this, I suggested that I could just transfer these files to the application directory and run them locally.

+2
Sep 15 '08 at 16:35
source share

There is no way to do this using modern Apple APIs. Your closest bet is to write a simple iPhone app with the webkit browser built in. This will allow you to view your xhtml / js application locally.

If you want to store data, you need to take another step and turn on the light weight HTTP server that serves your application and provides calls for storing and retrieving data. This may not be the perfect solution for you, but perhaps less work than the full Obj-C app.

As a side note, Obj-C is pretty easy to learn. There are many examples in the SDK. The community is strong and will answer questions asked without problems.

+2
Sep 15 '08 at 16:36
source share

look at the new o'reilly book on this

+2
Oct 11 '09 at 17:29
source share

I have been using telephone interchange for a while, and this seems to have the best results for me. I will post my experience in a week or so using the link to my application.

+1
Jun 16 '09 at 20:32
source share

Titanium Mobile is also an option - it allows you to write JavaScript, which translates to Objective-C.

+1
Apr 05 '10 at 18:09
source share

At least 2 people mentioned phone disassembly, but I thought that I would post it too, and mention that Apple approved the telephone conversation scheme. So now you don’t get Apple rejecting Apple just because you use the handset.

Blog post about phone and Apple - http://blogs.nitobi.com/jesse/2009/11/20/phonegapp-store-approval/

Telephone break at home

0
Dec 04 '09 at 20:52
source share



All Articles