본문 바로가기

Mobile/iOS

[iOS] Javascript console.log 찍는 방법

Cordova를 이용해 개발 중 Web에 console.log() 의 경우 Android에서는 디버그로 볼 수 있지만 iOS 에서 보이지 않는 현상이 발생

하기와 같이 코드를 추가해 주면 xcode console에서 해당 로그를 볼 수 있음


#import <JavaScriptCore/JavaScriptCore.h>


- (void)webViewDidFinishLoad:(UIWebView*)theWebView

{

    NSLog(@"Finished load of: %@", theWebView.request.URL);

    

    JSContext *ctx = [theWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    ctx[@"console"][@"log"] = ^(JSValue * msg) {

        NSLog(@"JavaScript %@", msg);

    };

}