BetterSafariView icon indicating copy to clipboard operation
BetterSafariView copied to clipboard

2 different BSVs in same View

Open FsxShader2012 opened this issue 4 years ago • 2 comments

I have 2 Buttons in the same View. Each of them should open a BSV with given URLs slnURL[0] and slnURL[1]:

Button("Show details") {
                        showSafari.toggle()
                    }
                    .safariView(isPresented: $showSafari) {
                        SafariView(
                            url: URL(string: slnURL[0])!,
                            configuration: SafariView.Configuration(
                                entersReaderIfAvailable: false,
                                barCollapsingEnabled: true
                            )
                        )
                        .preferredBarAccentColor(.clear)
                        .preferredControlAccentColor(.accentColor)
                        .dismissButtonStyle(.close)
                    }
Button("Show details") {
                        showSafari.toggle()
                    }
                    .safariView(isPresented: $showSafari) {
                        SafariView(
                            url: URL(string: slnURL[1])!,
                            configuration: SafariView.Configuration(
                                entersReaderIfAvailable: false,
                                barCollapsingEnabled: true
                            )
                        )
                        .preferredBarAccentColor(.clear)
                        .preferredControlAccentColor(.accentColor)
                        .dismissButtonStyle(.close)
                    }

When running the App both buttons open the URL from the first Button in the View.

E.g.: First Button URL: google.com, Second Button URL: amazon.com -> Opens google.com from both Buttons First Button URL: amazon.com, Second Button URL: google.com -> Opens amazon.com from both Buttons

Is this expected behavior? Am I doing something wrong?

Thanks for your help in advance :)

FsxShader2012 avatar Dec 12 '20 13:12 FsxShader2012

I found a solution. The problems lies in the way BetterSafariView init the url. Once the view is visible, the url is initialized and there is no way to change it. As a result, it can only point to one url in a single view.

The solution is to put it into a separate view. Thus, you can create as many url points to different website as you wish.

legolasW avatar Jan 28 '21 20:01 legolasW

You can create an @Binding value for your URL and change it prior to calling showSafari.toggle(). This should cause SwiftUI to redraw the view with your updated URL.

Sam-Spencer avatar Feb 24 '21 21:02 Sam-Spencer