swift 如何在结构体内部的类中传递字符串?

bf1o4zei  于 2023-01-08  发布在  Swift
关注(0)|答案(1)|浏览(133)
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中使用它。

4sup72z8

4sup72z81#

您可以在ContentController中存储对WebView的引用,然后在ContentController中访问WebView的response。例如:

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(parent: self)// let handler be a coordinator
    }
}

    
class ContentController: NSObject, WKScriptMessageHandler {
    let parent: WebView
    
    init(parent: WebView) {
        self.parent = parent
    }
    
    // .....
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)  {
        if let args = message.body as? [String] {
            switch args[0] {
                // .....
            case "didSolve":
                self.captchaDidSolve(response: args[1])
                break
                
                // .....
            default:
                print("args[0]: \(args[0])")
                break
                
                // ....
            }
        }
    }
    
    func captchaDidSolve(response: String) {
        parent.response = response
    }
}

现在我想起来了,虽然我不知道为什么parent实际上是一个引用,但它似乎是有效的。

相关问题