0

This issue is an initialization problem encountered in compiling code to transport variables between web views and views. I've asked 3 months ago(previous question was asked here.). I haven't been able to solve it yet. I gave up on solving and found for other alternatives, but wanted to know the cause or solution to the issue, so I re-identified the issue and recreated the question.

I'm implementing a sheet between a Web view and a View. The values ​​in the sheet are always changing. The content of the sheet called from the view must be shared by the web view (or reverse). There is no problem when mounting the web view directly inside the view. But the issue occurs when using with 'let' expressions. The following error message appears right before compilation:

"...Cannot use instance member '$activeSheet' within property initializer; property initializers run before 'self' is available..."

I haven't only used init(){...} in the view and but also used various initializing methods, but the initialization error message does not disappear. The most cause of this issue seems to be a problem with init, which sends @State from the view to web view and requests the value of @Binding down from the Web view to View again.

Is there any way?

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, the initialization error message occurs on $activeSheet.

Anyway, this Web view must be use instance member $activeSheet within property initializer through let webView....

I hope you to be understand my lacked question.

Young Lee
  • 1
  • 1
  • 2
    Put the webview in the body not in a variable. You can’t and shouldn’t have it in a variable. – lorem ipsum Feb 04 '23 at 15:08
  • @loremipsum Oh, I solved it. I fixed it as you advised ('Put the webview in the body') and it works fine. Thank you so much. By the way, the reason I put the Webview outside the body is because I referred to these links. [example 1](https://stackoverflow.com/questions/56760092/how-to-access-goback-and-goforward-via-uiviewrepresentable/56776133#56776133) and [2](https://stackoverflow.com/questions/56760092/how-to-access-goback-and-goforward-via-uiviewrepresentable/56776133#56776133) These example sources are known to work very well... – Young Lee Feb 04 '23 at 22:53

1 Answers1

0

I solved it through Lorem Ipsum advised ('Put the webview in the body') and it works fine.

struct ItlnrView: View {
    
    @State var activeSheet: **Bool** = false
    .....

    var body: some View {

        let webView = myWebView(web: nil, actSheet: $activeSheet, req: URLRequest(url: URL(string: "https://google.com")!))

        VStack {
            webView
        }
        .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
    }
    ....
    ....
}
Young Lee
  • 1
  • 1