Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 different BSVs in same View #10

Open
FsxShader2012 opened this issue Dec 12, 2020 · 2 comments
Open

2 different BSVs in same View #10

FsxShader2012 opened this issue Dec 12, 2020 · 2 comments

Comments

@FsxShader2012
Copy link

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 :)

@legolasW
Copy link

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.

@Sam-Spencer
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants