Get Updates By E-mail 

Article Written By:
Johann Dowa

Interacting With Javascript Iphone Apps Using Objective-C


Ok, if you’ve been following my last few posts I introduced several frameworks that are designed to enable developoment of native Iphone apps using HTML, CSS, and Javascript.

First, a visitor asked why would you write an app using Javascript instead of Objective-C?

I can think of several reasons.

1. You’ve already written an Iphone web app, but want to expand your reach by placing your app in the app store.
2. You are or are working with web developers who don’t understand Objective-C and are unwilling or unable to learn how to use Objective-C.
3. You want your app to run on other platforms without having to port your code. (can be done using some of the available javascript Iphone frameworks)
4. You want to interact with web services, and find it cumbersome to do so using Objective-C code.

The third and fourth reasons are why I’m interested in this type of development. I like Objective-C development, but writing the code to interact with different web services can really be tedious, and there are many more examples and libraries available for doing this using Ajax. Also, every platform seems to be coming out with an app store.

On to the topic at hand.

Sending data from your Objective-C code to an app written in Javascript is not difficult, as Apple has written a command.

The procedure for doing this is shown here: Iphone App Development For Web Hackers.  In a nutshell you send Javascript commands by using the stringByEvaluatingJavaScriptFromString: message in the UIWebView class.   In this example, this is done with the following code:

-(void)updateLatitude:(NSString *)latitude andLongitude:(NSString *) longitude {
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"update_position(%@, %@);", latitude, longitude]];

This code executes the update_position function within javascript contained on the page loaded in the UIWebView very nifty. The strings is executed as if it were javascript actually on the page. This can be used to easily send values like location as above, touches, or accelerometer details that normally wouldn’t be available to your javascript code. This is an excellent tutorial if you have not created a “native” app on the Iphone using javascript before.

Tommorow I will get into the more complex matter of interacting with Objective-C code using javascript running in a UIWebView.


Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.