javascript - How to log errors to XCode with JavaScriptCore? -
i have project uses javascriptcore communicate between webview , objective c. works well, except seem unable log runtime errors xcode console.
i have following:
- (void)createwebviewcontext { //find context self.webviewcontext = [self.webview valueforkeypath:@"documentview.webview.mainframe.javascriptcontext"]; //register js error handler self.webviewcontext.exceptionhandler = ^(jscontext *c, jsvalue *e) { dispatch_async(dispatch_get_main_queue(), ^{ nslog(@"javascript error: %@. stack: %@", e, [e valueforproperty:@"stack"]); }); }; //attach logger id logger = ^(nsstring *logmessage) { nslog(@"%@", logmessage); }; self.webviewcontext[@"console"][@"log"] = logger; self.webviewcontext[@"console"][@"info"] = logger; self.webviewcontext[@"console"][@"error"] = logger; self.webviewcontext[@"console"][@"err"] = logger; self.webviewcontext[@"console"][@"warn"] = logger; }
this code seem work - if console.log
or console.err
something, output in xcode. problem not errors thrown javascript (such syntaxerrors, typeerrors or similar). these kinds of errors still show in safari inspector, not in xcode.
any way achieve this?
hm, try check webviewcontext.exception
property, maybe work.
additionally make sure catch errors in window object. like:
[self.webviewcontext evaluatescript:@"window.onerror = function(msg) { console.err(msg)}"]
after errors should forwarded console.err handler
Comments
Post a Comment