Friday, June 27, 2014

How to redirect JavaScript console.log() to Xcode's debug output

On iOS >= 7.0 it is pretty easy thanks to brilliant (and undocumented as well) framework:
@import JavaScriptCore;

...

// get UIWebView's JavaScript context
JSContext *ctx = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

// 'listen' to console.log()
ctx[@"console"][@"log"] = ^(NSString *message) { 
    NSLog(@"Javascript's console.log() :\n%@\n\n", message); 
};

No comments:

Post a Comment