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.

bfe81039d7d298f2fd73864f498597c1fb7db06a

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

22b93101efaab438028d9a93d331d1c65f845a1b

84b80828fd2c49e8f71a29d2f979b59495979779

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

f21e66958dfdb51f9f7d52523f688771f53b3c1d

3. Practice

  • To use javascript code you do the following:
    • JSContext initialization
    • Javascript Load Code from String file
    • Get Object from javascriptCode
    • Run object with callWithArguments function

To use apple code in javascript you have 2 ways:

  • With block: with objC, the visibility is visible in the code with swift code you have to declare @objc for swift block
  • With class: Use JSExportProtocol Declare a protocol extend from JSExportProtocol

In the javascript file you can use the following:

  • Note that the function has 2 or more paramter. Since javascript does not have a reminding parameter, you append the method name in the swift twoParametersExample (hex: String, hax: String) to a method with the name twoParametersExampleHax in js (this is not available on raywenderlich: v)

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