4

As demonstrated in the sample code below. The popover appears correctly when run without declaring the openURL variable. Uncomment the lines and it appears in the incorrect spot and refuses to be dismissed. I could really use a fix/work-around...

struct Popover: View {
    @State var isShowingPopover = false
//    @Environment(\.openURL) var openURL

    var body: some View {
        Text("Content")
        .toolbar {
            ToolbarItemGroup(placement: .primaryAction) {
                Button("Popover") {
                    isShowingPopover.toggle()
                }
                .popover(isPresented: $isShowingPopover) {
                    Button(action:{
//                        openURL(URL(string: "https://cnn.com")!)
                    }){
                        Label("Open CNN", systemImage: "hand.thumbsup")
                    }
                        .padding()
                }
            }
        }
    }
}


struct Popover_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
            Text("Sidebar")
            Popover()
        }
    }
}
Rumbles
  • 806
  • 1
  • 8
  • 15
  • If you are targeting iOS 14 and higher, you could try `Link` instead of the button with `openURL`. – koen Dec 10 '20 at 20:43
  • Great idea, but putting the URL Link in a Menu doesn't appear to work either! – Rumbles Dec 10 '20 at 21:31
  • I take that back... didn't wok in simulator but does on device... – Rumbles Dec 10 '20 at 22:38
  • Not sure what you mean by the menu, I was referring to [this](https://www.hackingwithswift.com/quick-start/swiftui/how-to-open-web-links-in-safari). – koen Dec 10 '20 at 23:18

1 Answers1

1

It appears toolbar defect... using navigationBarItems works fine.

Tested with Xcode 12.1 / iOS 14.1

struct PopoverView: View {
    @State var isShowingPopover = false
    @Environment(\.openURL) var openURL

    var body: some View {
        Text("Content")
            .navigationBarItems(trailing:
                 Button("Popover") {
                      isShowingPopover.toggle()
                 }
                 .popover(isPresented: $isShowingPopover) {
                      Button(action:{
                            openURL(URL(string: "https://cnn.com")!)
                      }){
                            Label("Open CNN", systemImage: "hand.thumbsup")
                      }
                            .padding()
                 })
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690