BetterSafariView
BetterSafariView copied to clipboard
2 different BSVs in same View
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 :)
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.
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.