ITZone

Integrate javascript code on ios project

1. Introduction

The unfinished research had to write a report, ?, confess that if you want to write well, you have to be born like this for 2 months so it is weak, you are reading it and try to bear it, it's not my fault : v. After a period of self-app making, I found that the 1 app uploading me takes a lot of time affecting the process of distributing and earning money from product. So I want to find a way to update the app and limit the new version of app len appstore -> I am researching integrating javascript into ios and then modifying some parts of the app with javascript to make it easier Easy to update the code.

2. JavaScripCore Framework

Introduced by Apple from WWDC 2013, javascriptcore lets you integrate the script into the app, previously with only C api for Mac and now javascriptcore build on top both on mac and ios, making programming simple and fewer errors.

To reflect component in javascript Apple defines the following basic classes:

JSVirtualMachine : js code is run on the machine that tells js to be defined by class. JSVirtualMachine itself does not support multithread, if you want to run multithreading, you must create multiple JSVirtualMachine. Each JSVirtualMachine Object has its own set of heap and barbage collector, and there is no other JSVirtualMachine contact.

JSContext : Corresponding to the Global object in javascript, unlike JSVirtualMachine, you can freely pass objects between JSContext.

JSValue : All values ​​obtained from JSContext are JSValue . JSValue encapsulates all Javascript data types. Each JSValue is tied to the JSContext object.

3. Practice

To use apple code in javascript you have 2 ways:

In the javascript file you can use the following:

4. Conclusion

It seems that running code on javascript is delayed a bit on processing time. You can test by project example in this article. http://www.mediafire.com/download/ztgly24x1aucd6i/JavascriptCoreTest.zip Refer to: https://www.raywenderlich.com/124075/javascriptcore-tutorial

ITZone via Viblo

Share the news now