swift – iOS SwiftUI SafariView Bug – Why is the primary URL not loading correctly?

0
29
swift – iOS SwiftUI SafariView Bug – Why is the primary URL not loading correctly?


I’m constructing a SwiftUI software with an inventory of merchandise, the place every product has an related URL. When the consumer clicks the “Data” button of a product, a SafariView ought to open to show the corresponding URL. Nevertheless, I’m encountering a problem the place the SafariView fails to load the right URL the primary time a product is chosen. After deciding on a distinct product, the performance begins working as anticipated for all subsequent choices.

My variables:

@StateObject personal var dataProvider = DataProvider()
// For INFO-Button:
@State personal var safariURL: URL? = nil
@State personal var showSafariView: Bool = false
 ForEach(dataProvider.merchandise) { product in
                            
                            VStack(alignment: .main) {

// "Data" Button
                                    Button(motion: {
                                        
                                        if let urlString = product.productUrl, let url = URL(string: urlString) {
                                            safariURL = url
                                            print("Safari URL set: (url); (String(describing: safariURL))")
                                            showSafariView = true
                                        } else {
                                            print("Err. 1: No legitimate URL out there.")
                                            safariURL = nil
                                            showSafariView = false
                                        }
                                    }) {
                                        HStack {
                                            Textual content("INFO")
                                        }
                                        .body(width: 120, top: 10)
                                        .padding()
                                    }
                                    .background(Colour.blueGradient)
                                    .foregroundColor(.white)
                                    // SafariView Overlay
                                    .sheet(isPresented: $showSafariView) {
                                        // Test, whether or not the URL was set
                                        if let safariURL = safariURL {
                                            SafariView(url: safariURL)
                                        } else {
                                            Textual content("Err. 2: No legitimate URL to show.")
                                            Textual content("Product URL: (String(describing: product.productUrl))")
                                            Textual content("safariURL: (String(describing: safariURL))")
                                        }
                                    }
struct SafariView: View {
    var url: URL
    
    var physique: some View {
        SafariViewController(url: url)
            .edgesIgnoringSafeArea(.all)
    }
}

struct SafariViewController: UIViewControllerRepresentable {
    var url: URL
    
    func makeUIViewController(context: Context) -> SFSafariViewController {
        let safariVC = SFSafariViewController(url: url)
        return safariVC
    }
    
    func updateUIViewController(_ uiViewController: SFSafariViewController, context: Context) {
        // possibly I've to repair sth. right here?
    }
}

It really works completely in Preview (xCode 16.1). Within the Simulator (and TestFlight on iPhone), nevertheless, the next occurs:

Shall we say I get 3 merchandise listed:

  1. www.youtube.com
  2. no url
  3. www.google.com

I click on on the primary product (or every other product):

I click on on the primary/similar product once more (irrespective of what number of instances):

  • The identical factor occurs each time.

Now I click on on one other product, e.g., the third one:

If I now return and click on on the primary product once more:

If I click on on a product with no URL (e.g., the second):

  • Console: “Err. 1: No legitimate URL out there.”

So, this difficulty solely occurs with the very first product I choose, no matter which product it’s. The identical occurs once I begin with the third product or the 2nd. It doesn’t appear to be associated to the product or the URL itself; it simply doesn’t appear to “get it” the primary time.


I attempted so many issues, e.g. to pressure set the URL, I attempted to place a delay in opening the SafariView,…. I requested chatGPT like 100 instances…

I simply need the safariView to open with the right URL instantly.
In the event you want additional data or code associated to my downside, simply let me know.

LEAVE A REPLY

Please enter your comment!
Please enter your name here