Create WKWebView_logging.md

This commit is contained in:
Sander Hautvast 2024-02-21 14:05:19 +01:00 committed by GitHub
parent 92c906adc4
commit 01b66fa9fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

48
IOS/WKWebView_logging.md Normal file
View file

@ -0,0 +1,48 @@
```swift
import SwiftUI
import WebKit
import JavaScriptCore
struct ContentView: View {
var body: some View {
WebView()
}
}
#Preview {
ContentView()
}
struct WebView: UIViewRepresentable {
let webView: WKWebView
init() {
webView = WKWebView(frame: .zero)
}
func makeUIView(context: Context) -> WKWebView {
let source = "function captureLog(msg) { window.webkit.messageHandlers.logHandler.postMessage(msg); } window.console.log = captureLog;"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
webView.configuration.userContentController.addUserScript(script)
// register the bridge script that listens for the output
webView.configuration.userContentController.add(Controller(), name: "logHandler")
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
webView.load(URLRequest(url: URL(string: "http://localhost:8000/demo")!))
}
}
class Controller: NSObject, WKScriptMessageHandler{
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "logHandler" {
print("LOG: \(message.body)")
}
}
}
```