iOS 7 - Xamarin Recipe for JavascriptCore Framework

0 Comments

The JavaScriptCore Framework introduced with iOS 7 allows developers to access the full JavaScript runtime from within their apps.

Getting Started

It's really easy to use the JavaScriptCore Framework with Xamarin.iOS. In your project, add a new usingstatement.

using MonoTouch.JavaScriptCore;

JSContext and JSValue

There are two main types used when working with the JavaScriptCore Framework: JSContext and JSValue. A JSContext talks to the JavaScript runtime and provides access to the global object, and the ability to execute scripts. To start, create a new JSContext and call its EvaluateScript(string script)method.

JSContext context = new JSContext ();
JSValue result = context.EvaluateScript ("2 + 2");
Console.WriteLine(result); // prints 4

In this example our JavaScript code is simply "2 + 2". EvaluateScript returns a JSValue, and in this case the code has produced 4. That is all that is required to execute JavaScript code from your Xamarin.iOS app! Now you can easily make your app scriptable!

You can download the source for this project here.

Next Up

Next time I will show how to call JavaScript functions.

Comments