티스토리 뷰

728x90

출처

Code

    func initWebView(activityIndicator: UIActivityIndicatorView) {
        // 생략...
        contentController.add(self, name: "native_console_log")
        // 생략...
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if(message.name == "native_console_log") {
            native_console_log(didReceive: message)
        }
    }

    func native_console_log(didReceive message: WKScriptMessage) {
        print(NSString(string: "console.log: \(message.body)"))
    }
 
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        #if CONSOLE_LOG
            let js = "var console = { log: function(message) { webkit.messageHandlers.native_console_log.postMessage(JSON.stringify(message)) } }"
            webView.evaluateJavaScript(js)
        #endif
    }

빌드 옵션 추가

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함