Created
May 27, 2019 15:17
-
-
Save BrunoMiguens/7a5a804c82aac21050f403534eea2270 to your computer and use it in GitHub Desktop.
[Medium] Swift: Using Javascript library - Generate JSContext
This file contains 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
import JavaScriptCore | |
func generateJSContext() -> JSContext? { | |
let context = JSContext() | |
context?.evaluateScript("var console = { log: function(message) { _consoleLog(message) } }") | |
let consoleLog: (String) -> Void = { message in | |
print("JS Log: \(message)") | |
} | |
context?.setObject(consoleLog, forKeyedSubscript: "_consoleLog" as NSCopying & NSObjectProtocol) | |
context?.exceptionHandler = { context, exception in | |
print("JS Error: \(exception.debugDescription)") | |
} | |
return context | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment