Javascript console.log() in an iOS UIWebView

前端 未结 7 972
深忆病人
深忆病人 2020-11-28 01:10

When writing a iPhone / iPad app with a UIWebView, the console isn\'t visible. this excellent answer shows how to trap errors, but I would like to use the console.log() as w

7条回答
  •  自闭症患者
    2020-11-28 01:49

    Starting from iOS7, you can use native Javascript bridge. Something as simple as following

     #import 
    
    JSContext *ctx = [webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    ctx[@"console"][@"log"] = ^(JSValue * msg) {
    NSLog(@"JavaScript %@ log message: %@", [JSContext currentContext], msg);
        };
    

提交回复
热议问题