Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a JavaScript engine in an iOS application

I wonder if anyone has successfully ported a javascript engine/interpreter to iOS. I'm writing an iPhone game that I would like to use Javascript as the high-level scripting language (AI, gameplay, etc.), but to do that, I need to compile the JS engine into a static library and link it against my objectiveC program for iPhone OS. There are some candidate JS engine I'm looking at but I couldn't find any successful cases for doing that.

Here're the js engine I was hoping to use for iOS

  • google's V8 javascript engine
  • mozilla's SpiderMonkey

The alternative is to use UIWebView's Javascript callback interface, but that requires loading the entire UIWebView into memory and the experiences I heard is that it is usually slow in performance.

Appreciate if anyone had similar experiences of do this or know any references for that!

[UPDATED] as Kostis mentioned, Apple introduced JavascriptCore in WWDC 2013

like image 468
dennycd Avatar asked Mar 15 '11 20:03

dennycd


People also ask

Can you use JavaScript for iOS apps?

JavaScript frameworks are well-suited to mobile app development, as they can be used across a number of platforms, including iOS, Android, and Windows.

What is JavaScript in iOS?

JavaScript is a popular programming language used by most websites. On an iPhone, JavaScript should be turned on by default, but if it was disabled at some point, many websites will appear broken in the Safari browser.

Which JS engine is used by Safari?

JavaScriptCore is Apple's engine for its Safari browser. Other WebKit-based browsers also use it. KJS from KDE was the starting point for its development. Chakra is the engine of the Internet Explorer browser.


2 Answers

There are two projects you might be interested in:

  1. JavaScriptCore-iOS
  2. iMonkey

It is absolutely possible to build and ship a JavaScript engine with your iOS app, see http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time. Titanium does that, too: https://github.com/appcelerator/webkit_titanium/tree/master/Source/JavaScriptCore. Also see http://www.phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios.

I've also made a small app that shows how to use JSC on iOS: https://github.com/jfahrenkrug/AddressBookSpy

Enjoy.

like image 56
Johannes Fahrenkrug Avatar answered Sep 23 '22 12:09

Johannes Fahrenkrug


For future viewers, now there is the JavaScriptCore framework, introduced in the new iOS 7. It does magic! Wrapping, unwrapping values from/to JS/Objective-C, calling functions, callbacks, everything!

Unfortunately, documentation is really poor at the moment. You can find a presentation from the WWDC 2013 event and some more info in the header files (cmd+click on the header file name in Xcode). There are also some tutorial around the internets which just copy what the guy in the WWDC presentation does.

I've used it for one of my projects, it's really powerful. The only think I didn't like is that it passed objects from JS to ObjC by value, i.e. reference was lost. There might be a workaround for this, but I couldn't find anything without proper documentation.

Hope this will help someone :)

like image 35
Kostis Avatar answered Sep 23 '22 12:09

Kostis