SwiftUI: Alert that does not close when clicking on button

274 Views Asked by At

I'm new to SwiftUI and I'm trying to display an alert that does only close on some conditions but not on every click on its buttons.
Is that possible or does it strike against the concept of alerts?

My code is like this:

.alert("title", isPresented: $presented) {
    Button(action: {
        NSWorkspace.shared.open(URL(string: "https://myurl")!)
    }) {
        Text("Download ...")
    }
} message: {
    Text("message")
}

But when I click the button presented gets set to false. Can I prevent this somehow? Or intercept when it gets set to false so I can check if it should actualle stay true and change it again?

1

There are 1 best solutions below

0
On

Seems to me that it goes against the concept of alerts. As the documentation says:

All actions in an alert dismiss the alert after the action runs.

My solution is using the .sheet() View modifier instead. Styling it the same way as an alert looks takes some time, though.