Struggling to figure out how to call a Javascript Function with a callback from Swift, but it's not working as expected.
Here's what I have:
Javascript:
global.calculateWithCb = function(temp,cb){
cb(5 * temp)
}
global.calculate = function(temp) {
return 5 * temp
}
Swift:
let context = JSContext()
let scriptURL = NSURL(fileURLWithPath: String("XXX/bundle.js"))
let script : String = try String(contentsOfURL:scriptURL, encoding: NSUTF8StringEncoding)
var jsSource: String! = script
jsSource = "var window = this; \(jsSource)"
context.evaluateScript(jsSource)
let calculate = context.objectForKeyedSubscript("calculate")
let output = calculate.callWithArguments([40]).toNumber()
Although the calculate function is functioning correctly, I'm stuck on how to pass a block as a callback argument. I've searched extensively online for a solution but haven't found any helpful hints. Can anyone provide guidance on how to achieve this?