Created
November 17, 2017 00:35
-
-
Save jfahrenkrug/2a503734a8b08d24acd5d1b49f095105 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private override init() { | |
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!) | |
// The Swift closure needs @convention(block) because JSContext's setObject:forKeyedSubscript: method | |
// expects an Objective-C compatible block in this instance. | |
// For more information check out https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-ID350 | |
let nativeLog: @convention(block) (String) -> Void = { message in | |
NSLog("JS Log: \(message)") | |
} | |
// Create a new JavaScript context that will contain the state of our evaluated JS code. | |
self.context = JSContext(virtualMachine: self.vm) | |
// Register our native logging function in the JS context | |
self.context.setObject(nativeLog, forKeyedSubscript: "nativeLog" as NSString) | |
// Evaluate the JS code that defines the functions to be used later on. | |
self.context.evaluateScript(jsCode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment