Executing JavaScript from Objective-C in an iOS App

by
Tags: , ,

New blog post from Steve Smith, one of the Chariot architects working in our mobile practice. Steve is currently working on updating the mobile app for our Emerging Technologies conference and will be posting articles from time to time as he finds challenges or discoveries.

Here is an excerpt:

The other day, I found the need to execute some JavaScript from a native iOS application. After doing a little research, I discovered that the UIWebView has a stringByEvaluatingJavaScriptFromString method. So I decided to create a quick little proof-of-concept to see how to invoke some JavaScript using this. I am not going to walk through all the details of creating a project, etc. and will cut right to the important aspects of making the JavaScript call from the objective-c code.
We are going to need a JavaScript function to call and a UIWebView to evaluate the JavaScript. It would be nice if the JavaScript function took a parameter as well, so we could also prove out the passing of values. Here is the JavaScript function that I will use:

var area = function(diameter) {
  var radius = diameter/2,
  area = Math.PI * radius * radius;
  return Math.round(area*100)/100;
};

Read more