Sunday, April 13, 2014

Jint tutorial 007 - Using Jint and Jint.Ex in an iOS Application

Reminder : Jint is a Javascript v 5 interpreter for the .NET Framework/Mono/Xamarin allowing to execute scripts in C# applications on Windows, MacOS, iOS, Android and Linux (Anywhere there is a C# 4.x compiler).

Jint.Ex is event-driven interaction run time for Jint to build non blocking UI and creating asynchronous API.

In this example, I ported the WinForm app described in Jint tutorial 006 - Using Jint and Jint.Ex in a Win Form Application to an iOS application using the Xamarin stack.

For this sample, I used the MonoTouch Dialog library to build the UI.
The overall C# code is the same which is the point of Jint.Ex all the JavaScript execution including callback event are executed in a background thread by Jint.Ex AsyncronousEngine and interact with the UI.



namespace Jint.Ex.iOS {
    public partial class AppDelegate : UIApplicationDelegate {
        UINavigationController navigation;
        UIWindow window;
        private AsyncronousEngine _asyncronousEngine;
        private Section _outputSection;

        private void InitAsyncronousEngine() {

            _asyncronousEngine = new AsyncronousEngine();
            // Expose to the JavaScript the function setUserMessage which add a string in the Listbox 
            _asyncronousEngine.Engine.SetValue("setUserMessage", new Action<string>(__setUserMessage__));

        private void __setUserMessage__(string s) {
                InvokeOnMainThread (delegate {                    
                        _outputSection.Insert (0, UITableViewRowAnimation.None, new StringElement (s));

        public void SynchronousExecution() {

        public void AsynchronousExecution() {

        public void Timer() {

        public void MultipleTimers() {

        public void ClearOuput() {
            _outputSection.RemoveRange(0, _outputSection.Elements.Count);

        public void ClearEventQueue() {

        public override bool FinishedLaunching(UIApplication app, NSDictionary options) {

            _outputSection = new Section("Output") {


            var menu = new RootElement ("Jint and Jint.Ex iOS Demo") {
                new Section ("Menu"){
                    new StringElement ("Synchronous Execution", SynchronousExecution),
                    new StringElement ("Asynchronous Execution", AsynchronousExecution),
                    new StringElement ("Timer", Timer),
                    new StringElement ("Multiple Timer", MultipleTimers),
                    new StringElement ("Clear Output", ClearOuput),
                    new StringElement ("Clear Event Queue", ClearEventQueue),

            var dv = new DialogViewController (menu) {
                Autorotate = true
            navigation = new UINavigationController ();
            navigation.PushViewController (dv, true);               

            // On iOS5 we use the new window.RootViewController, on older versions, we add the subview
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.MakeKeyAndVisible ();
            if (UIDevice.CurrentDevice.CheckSystemVersion (5, 0))
                window.RootViewController = navigation; 
                window.AddSubview (navigation.View);

            return true;


No comments:

Post a Comment