JavaScriptCore.JSContext Class
Encapsulates a JavaScript engine.

See Also: JSContext Members

Syntax

[Foundation.Register("JSContext", true)]
[ObjCRuntime.Availability(Introduced=ObjCRuntime.Platform.iOS_7_0)]
[ObjCRuntime.Availability(Introduced=ObjCRuntime.Platform.Mac_10_0 | ObjCRuntime.Platform.Mac_10_1 | ObjCRuntime.Platform.Mac_10_2 | ObjCRuntime.Platform.Mac_10_3 | ObjCRuntime.Platform.Mac_10_4 | ObjCRuntime.Platform.Mac_10_5 | ObjCRuntime.Platform.Mac_10_6 | ObjCRuntime.Platform.Mac_10_7 | ObjCRuntime.Platform.Mac_10_8 | ObjCRuntime.Platform.Mac_10_9 | ObjCRuntime.Platform.Mac_10_10 | ObjCRuntime.Platform.Mac_Version | ObjCRuntime.Platform.Mac_Arch64 | ObjCRuntime.Platform.Mac_Arch)]
public class JSContext : Foundation.NSObject

Remarks

The JavaScriptCore.JSContext is the central object of the JavaScriptCore namespace. The JavaScriptCore.JSContext maintains a JavaScript environment (manipulated by the JSContext.Item property) and evaluates scripts with the JSContext.EvaluateScript method.

Application developers will often want to assign a delegate to the JSContext.ExceptionHandler property to gain access, in their Xamarin.iOS code, of exceptions raised in the JavaScript realm.

The following example shows the basic use of JavaScriptCore.JSContext. The context is instantiated and a simple exception handler is assigned. One of the JSValue.From method overloads is used to assign values to the JavaScript variables arg1 and arg2. The JSContext.EvaluateScript method evaluates the JavaScript and returns the result, which is converted back into a .NET object with the JSResult.ToInt32 method.

C# Example

jsContext = new JSContext();

jsContext.ExceptionHandler = (context, exception) => {
	Console.WriteLine(exception);
};

jsContext[new NSString("arg1")] = JSValue.From(2, jsContext);
jsContext[new NSString("arg2")] = JSValue.From(2, jsContext);

var jsResult = jsContext.EvaluateScript("arg1 + arg2;");

var four = jsResult.ToInt32();
          

The JavaScriptCore.JSContext contains the global JavaScript context, including variables set by JavaScript calculations, as shown in the following example:

C# Example

jsContext.EvaluateScript("sum = 2 + 2;");
var four = jsContext[(NSString)"sum"].ToInt32();
          

Calling C# code from JavaScript

Currently, C# methods cannot be exposed directly to JavaScript via JavaScript Core. The recommended technique for calling C# code from Xamarin.iOS is to use REST, as shown in the following:

In the JavaScript code, use XMLHttpRequest and standard JSON techniques to post and parse a query to a REST service running on the local device:

JavaScript Example

<html>
    <head>
        <title></title>
        <script type="text/javascript">
           function callCSharp(msg) {
                var request = new XMLHttpRequest();
                request.open('GET','http://127.0.0.1:1711/', false);
                request.send();

                if(request.status == 200){
                    alert(JSON.parse(request.responseText));
                }else{
                    alert("Error");
                }
            }
        </script>
    </head>
    <body>
        <div onclick="callCSharp('this is a test')" class="button">
            Click Me
        </div>
    </body>
</html>
        

In the application, use System.Net.HttpListener to listen and respond to that request:

C# Example

//Wire up listener
listener = new HttpListener();
listener.Prefixes.Add("http://*:1711/");
listener.Start();
listener.BeginGetContext(new AsyncCallback(Callback), listener);

//....etc...

void Callback(IAsyncResult result)
{
	//Get the listener context
	var context = listener.EndGetContext(result);

	//Start listening for the next request
	listener.BeginGetContext(new AsyncCallback(Callback), listener);

	var response = CalculateResponse();
	var responseBytes = System.Text.Encoding.UTF8.GetBytes(response);

	context.Response.ContentType = "text/json";
	context.Response.StatusCode = HttpStatusCode.OK;
	context.Response.ContentLength64 = responseBytes.Length;
	context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
	context.Response.OutputStream.Close();
}
          

An alternative technique is to poll the JavaScriptCore.JSContext for a flag set by a JavaScript calculation.

Requirements

Namespace: JavaScriptCore
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0