Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an iPhone app entirely in JavaScript without making it just a web app?

I don't want to take the time to learn Obj-C. I've spent 7+ years doing web application programming. Shouldn't there be a way to use the WebView and just write the whole app in javascript, pulling the files right from the resources of the project?

like image 743
Jeff Avatar asked Sep 15 '08 16:09

Jeff


People also ask

Can I make an iPhone app with JavaScript?

JavaScript frameworks are well-suited to mobile app development, as they can be used across a number of platforms, including iOS, Android, and Windows.

Can I make an app with JavaScript only?

There exists a special framework called NativeScript, which was created specifically to enable building native apps for iOS and Android, always based on JavaScript. Moreover, developers can easily integrate it with the programming language TypeScript or the famous JavaScript framework, Angular. js.

Can you make an iPhone app with HTML?

Yes, you can. You would do this by making a webpage, and attaching that webpage to a UIWebView. You will still need Xcode and other iOS developer tools to work the minimal code you need to write.


2 Answers

I found the answer after searching around. Here's what I have done:

  1. Create a new project in XCode. I think I used the view-based app.

  2. Drag a WebView object onto your interface and resize.

  3. Inside of your WebViewController.m (or similarly named file, 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]]]; }
  4. Now any files you have added as resources to the project are available for use in your web app. I've got an index.html file including javascript and css and image files with no problems. The only limitation I've found so far is that I can't create new folders so all the files clutter up the resources folder.

  5. Trick: make sure you've added the file as a resource in XCode or the file won't be available. I've been adding an empty file in XCode, then dragging my file on top in the finder. That's been working for me.

Note: I realize that Obj-C must not be that hard to learn. But since I already have this app existing in JS and I know it works in Safari this is a much faster dev cycle for me. Some day I'm sure I'll have to break down and learn Obj-C.

A few other resources I found helpful:

Calling Obj-C from javascript: calling objective-c from javascript

Calling javascript from Obj-C: iphone app development for web hackers

Reading files from application bundle: uiwebview

like image 87
Jeff Avatar answered Sep 22 '22 22:09

Jeff


Check out PhoneGap at http://www.phonegap.com they claim it allows you to embed JavaScript, HTML and CSS into a native iPhone app.

like image 27
Chris Samuels Avatar answered Sep 22 '22 22:09

Chris Samuels