我正在尝试在 Swift 和 Javascript 之间与 SwiftUI 进行双向集成。
这是 WebKit 与 SwiftUI 的接口。
import SwiftUI
import WebKit
struct ggWebView : UIViewRepresentable {
let filePath: String
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.configuration.userContentController.add(self, name: "jsHandler")
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent(filePath)
uiView.loadFileURL(html, allowingReadAccessTo:bundleURL)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "jsHandler"{
print(message.body)
}
}
}
Run Code Online (Sandbox Code Playgroud)
userContentController.add(self, name: "jsHandler")self显示错误Argument type 'ggWebView' does not …