BetterSafariView
BetterSafariView copied to clipboard
how to use it inside inside a VStack loop
Hello and thank you for your package :) My problem is that inside a list of articles (VStack loop) if I use the current README example I get 2 instances/URLs (belonging to 2 different articles) opens every time I click a button.
I now tried:
List(fetch.Novitadss) { Novitads in
VStack(alignment: .leading) {
Button(action: {
self.selectedURL = URL(string: Novitads.link)
}) {
Text(Novitads.title)
.platformFontNegative()
.italic()
}
.safariView(item: $selectedURL) {selectedURL in
SafariView(url: selectedURL)
}
but I get again 2 browsers opens at every click (just with the same link now). Thanks in advance for your help.
Originally posted by @gurugeek in https://github.com/stleamist/BetterSafariView/issues/1#issuecomment-772965135
Because the BetterSafariView init the url on launch. There is no way to change it dynamically as far as I can tell. You need to extract the Safari View into subview, and it will work as expected.
@legolasW thank you for the reply. I think I am missing something: the example at #1 would work with a list of links. In my sample code I sorted the first issue (using the README example in loop of articles will open 2 URLS different URLS in Safari if in lieu of passing a loop "in"
```
.safariView(item: $selectedURL) {selectedURL in
SafariView(url: selectedURL)
}
and I could just use selectURL I think it would open just 1. Another thing which makes me thing is why it opens 2 safari instances one after the other? If was because of the loop I have many more articles than 2. Just trying to figure this out. Having another view would solve this but that would require the user to have 2 clicks before reaching the final link ?
Umm... no, user does not need to click twice. By extract it to subview, I mean creating a new view that accepts url as variable. And then just passed the url to the subview.
Use it like SomeSafariView(url: "your url address here")
Define it like view struct PersonalLinkView: View { var url: String }
Extract it to subview in SwiftUI is merely a way of organizing the code. It should not cause issues like "Having another view would solve this but that would require the user to have 2 clicks".
~I'm also encountering a similar issue and BetterSafariView is opened twice on the same URL. I'm using Xcode 15.1 targeting iOS 17.2. I have a .safariView modifier that creates a SafariView with a given URL from an enum's field. Here's a demo video and sample project showing the behavior~ EDIT: Moved to https://github.com/stleamist/BetterSafariView/issues/48