Skip to content

Instantly share code, notes, and snippets.

@0xWDG
Created December 26, 2015 12:47
Show Gist options
  • Save 0xWDG/ac0e22450cdbd5ce0dc4 to your computer and use it in GitHub Desktop.
Save 0xWDG/ac0e22450cdbd5ce0dc4 to your computer and use it in GitHub Desktop.
Web view add console.log support
let jsContext = mainWebview.mainFrame.javaScriptContext
jsContext.evaluateScript("var console = {log: function () {var message = '';for (var i = 0; i < arguments.length; i++) {message += arguments[i] + ' '};console.print(message)},warn: function () {var message = '';for (var i = 0; i < arguments.length; i++) {message += arguments[i] + ' '};console.print(message)},error: function () {var message = '';for (var i = 0; i < arguments.length; i++){message += arguments[i] + ' '};console.print(message)}};")
let logFunction: @convention(block) (NSString!) -> Void = { (message:NSString!) in
print("JS: \(message)")
}
jsContext.objectForKeyedSubscript("console").setObject(unsafeBitCast(logFunction, AnyObject.self), forKeyedSubscript:"print")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment