struct WebView: UIViewRepresentable {
@Binding var response: String
func makeUIView(context: Context) -> WKWebView {
let webConfiguration = WKWebViewConfiguration()
let wkController = WKUserContentController()
wkController.add(context.coordinator, name: "reCaptchaiOS")
webConfiguration.userContentController = wkController
.....
}
func updateUIView(_ webView: WKWebView, context: Context) {
....
}
}
func makeCoordinator() -> ContentController {
ContentController()// let handler be a coordinator
}
class ContentController: NSObject, WKScriptMessageHandler {
.....
var textResponse: String = ""
func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) {
if let args = message.body as? [String] {
switch args[0] {
.....
case "didSolve":
self.captchaDidSolve(response: args[1])
textResponse = args[1]
break
.....
default:
print("args[0]: \(args[0])")
break
....
}
func captchaDidSolve(response: String) {
print("response: \(response)")
}
我需要将ContentController类内部的响应传递给WebView结构的响应,以便在主contentView中使用它。
1条答案
按热度按时间4sup72z81#
您可以在ContentController中存储对WebView的引用,然后在ContentController中访问WebView的
response
。例如:现在我想起来了,虽然我不知道为什么
parent
实际上是一个引用,但它似乎是有效的。