Create WKWebView_logging.md
This commit is contained in:
parent
92c906adc4
commit
01b66fa9fb
1 changed files with 48 additions and 0 deletions
48
IOS/WKWebView_logging.md
Normal file
48
IOS/WKWebView_logging.md
Normal 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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
Loading…
Add table
Reference in a new issue