0

I'm implementing a sheet between a web view and a view. The values ​​in the sheet are always changing. The value of the sheet called by the view must be shared by the web view (or reverse). There is no problem when mounting the web view directly inside the view. The problem arises when mediating with 'let' expressions.

struct ItlnrView: View {
    
    @State var activeSheet: **Bool** = false
    .....
    let webView = myWebView(web: nil, actSheet: **$activeSheet**, req: URLRequest(url: URL(string: "https://google.com")!)).  <--- problem

    var body: some View {
        VStack {
            webView
            // myWebView(web: nil, actSheet: $activeSheet, req: URLRequest(url: URL(string: "https://google.com")!)) <--- no problem

        }
        .sheet(isPresented: $activeSheet) {
           // code
        }
     }
}


struct myWebView: UIViewRepresentable {
    
    let request: URLRequest
    var webview: WKWebView?
    
    @Binding var activeSheet: Bool

    init(web: WKWebView?, actSheet: **Binding<Bool>**, req: URLRequest) {
        self.webview = WKWebView()
        self._activeSheet = actSheet
        self.request = req
    }
    ....
    ....
}

If I don't use let webView and run myWebView(web:,actSheet:,req:) in V/HStack directly, the sheet value is compatible/shared properly. But I must use/call by let webView because I have to use the 'go back' and 'reload' functions of web view. So whenever I use let webView, @State and @Binding then are incompatible.

In other words, $activeSheet requires as a Binding value in let webView, but @State value is appropriate when coding myWebView(web:,actSheet:,req:) in Stack directly. I hope you to be understand my lacked question.

TylerP
  • 9,600
  • 4
  • 39
  • 43
Young Lee
  • 1
  • 1
  • finally found a solution after 3 months. I suggested an alternative in the post below 3 months ago, but [this way](https://stackoverflow.com/questions/75345921/initializing-issue-between-binding-in-webview-and-state-in-view/75348951#75348951) don't need any alternative. – Young Lee Feb 04 '23 at 23:06

1 Answers1

0

It seems to be a bug that the same @State type and @Binding type are not compatible in the let webview environment. I solved myself the problem that @State and @Binding are not compatible when calling let webView. As follows:

struct ItlnrView: View {
    
    @State var activeSheet: Bool = false
    @State var urlToOpenInSheet: String = ""  <--- add
    .....
    //let webView = myWebView(web: nil, actSheet: **$activeSheet**, req: URLRequest(url: URL(string: "https://google.com")!))  <--- not required

    var body: some View {
        VStack {
            // webView  <--- not required
            myWebView(web: nil, actSheet: $activeSheet, **urlToSheet: $urlToOpenInSheet**, req: URLRequest(url: URL(string: "https://google.com")!))

        }
        .sheet(isPresented: $activeSheet) {
           // code
        }
     }
}


struct myWebView: UIViewRepresentable {
    
    let request: URLRequest
    var webview: WKWebView?
    
    @Binding var activeSheet: Bool
    @Binding var urlToOpenInSheet: String <---- add

    init(web: WKWebView?, actSheet: Binding<Bool>, **urlToSheet: Binding<String>**, req: URLRequest) {
        self.webview = WKWebView()
        self._activeSheet = actSheet
        elf._urlToOpenInSheet = urlToSheet  <---- add 
        self.request = req
    }
    ....
    ....
    func updateUIView(_ uiView: WKWebView, context: Context) {
        // add follow
        // 'uiView.load(request)' will replace 'let webView'(that was in View)
        if urlToOpenInSheet == "" {
            DispatchQueue.main.async {
                uiView.load(request)
            }
        }
    }
}

As a result, urlToOpenInSheet brings two webview environments.

Young Lee
  • 1
  • 1