52 lines
1.3 KiB
Markdown
52 lines
1.3 KiB
Markdown
Complete example, because I couldn't find one.
|
|
No prior IOS experience
|
|
|
|
|
|
```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)
|
|
|
|
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)")
|
|
}
|
|
}
|
|
}
|
|
|
|
```
|