1

I have a Menu with a few buttons. Each button, represents an URL. On selecting one of the buttons I want to present a webView loading the said URL using .fullScreenCover(isPresented:)

 @State private var showWebPage = false
    
 @State private var urlToLoad = ""

...

View()
     .toolbar {
         ToolbarItem(placement: .navigationBarTrailing) {
                                                       Menu {
                                                          Button("FAQ", action: {
                                        presentWebView(for: "https://example.com/faqsLink")
                                    })
                                                          Button("Privacy Policy", action: {
                                        presentWebView(for: "https://example.com/privacyLink")
                                    })
                                                          Button("Terms and Conditions", action: {
                                        presentWebView(for: "https://example.com/termsLink")
                                    })
}
}
}
.fullScreenCover(isPresented: $showWebPage) {
                    WebView(url: URL(string: urlToLoad)!)
                }

private func presentWebView(for url: String) {
   urlToLoad = url
   showWebPage.toggle()
}

Everytime I try this, urlToLoad is still empty when I toggle showWebPage I feel it has to do with how @State works but can't figure it out, I'm still new to SwiftUI.

Mihai Fischer
  • 329
  • 2
  • 11

1 Answers1

0

with the hint from Delano I managed to come up with this:

 .fullScreenCover(isPresented: $showWebPage) {
                    WebView(url: URL(string: urlToLoad)!)
                        .withCloseButton(and: "FAQ")
                }
                .onChange(of: urlToLoad) { value in
                    log.info (value)
                    if !value.isEmpty {
                        showWebPage.toggle()
                    }
                }

and in the button action I just update the value of urlToLoad

Hope this helps out somebody, I spent more time on this than I ever thought a Menu needed.

Mihai Fischer
  • 329
  • 2
  • 11