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)")
}
I need to pass the response that is inside the class ContentController to the response of the structure WebView to use it in a main contentView.