- Fire-and-forget way of showing system alerts in SwiftUI
- Will not fail if screen is already presenting a modal (🫤)
- Dipping into UIKit for creating a custom alert UIWindow
- Windowing logic adapted from https://www.fivestars.blog/articles/swiftui-windows/ (without going to the trouble of creating a custom Scene Delegate)
struct ContentView: View {
@StateObject var errorService = ErrorStateService()
var body: some View {
NavigationStack {
Button("Do a task") {
do {
try runTask()
} catch {
errorService.insert(.localizedError(title: nil, error: error))
}
}
}
.alertPresentationWindow(service: errorService)
}
}