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

how to use it inside inside a VStack loop #13

Open
gurugeek opened this issue Feb 4, 2021 · 4 comments
Open

how to use it inside inside a VStack loop #13

gurugeek opened this issue Feb 4, 2021 · 4 comments

Comments

@gurugeek
Copy link

gurugeek commented Feb 4, 2021

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 #1 (comment)

@legolasW
Copy link

legolasW commented Feb 4, 2021

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.

@gurugeek
Copy link
Author

gurugeek commented Feb 4, 2021

@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 ?                            

@legolasW
Copy link

legolasW commented Feb 5, 2021

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".

@aokj4ck
Copy link

aokj4ck commented Jan 1, 2024

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 #48

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