Javascript console.log() in an iOS UIWebView

前端 未结 7 982
深忆病人
深忆病人 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:54

    NativeBridge is very helpful for communicating from a UIWebView to Objective-C. You can use it to pass console logs and call Objective-C functions.

    https://github.com/ochameau/NativeBridge

    console = new Object();
    console.log = function(log) {
        NativeBridge.call("logToConsole", [log]);
    };
    console.debug = console.log;
    console.info = console.log;
    console.warn = console.log;
    console.error = console.log;
    
    window.onerror = function(error, url, line) {
        console.log('ERROR: '+error+' URL:'+url+' L:'+line);
    };
    

    The advantage of this technique is that things like newlines in log messages are preserved.

提交回复
热议问题