Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI.
This library manages items that are about to be presented in a queue and displays them on a first-in, first-out basis.
I have also tried to control the dialogs that the system displays, such as Push Notifications, App Tracking Transparency, and Location, but it has proven difficult.
The purpose of creating this library was to make it easier to solve the following problems.
[Presentation] Attempt to present <UIAlertController> on <ViewController> which is already presenting <UIViewController>.
In addition, I felt it was necessary to have a mechanism that takes into account SwiftUI as well as UIKit.
- iOS 13+
- Swift 5
Before | After |
---|---|
- File > Swift Packages > Add Package Dependency...
- Add
https://github.com/srea/UIPresentCoordinator
pod 'UIPresentCoordinator'
// Suspend queue processing.
UIPresentCoordinator.shared.suspend()
// Resume queue processing.
UIPresentCoordinator.shared.resume()
Set the class for automatic interrupt control.
UIPresentCoordinator.shared.interruptSuppressionTargets = [
FirebaseInAppMessagingInterruptSuppression(),
SystemAlertInterruptSuppression(),
UserDefineInterruptSuppression.init(objects: [CustomDialogViewController.self])
]
// Display without interruption
let alert = UIAlertController(...)
...
present(alert, animated: true, completion: nil)
// Display without interruption
let alert = UIAlertController(...)
...
presentQueue(alert, animated: true, completion: nil)
struct DebugView: View {
@State private var isPresented = false
var body: some View {
Button("Show Alert", action: {
isPresented = true
})
.alert(isPresented: $isPresented) {
Alert(title: Text("Alert"))
}
}
}
struct DebugView: View {
private let presentCoordinator: UIPresentable = UIPresentCoordinator.shared
@ObservedObject private var alertTask = Task<Alert>()
var body: some View {
Button("Show Alert", action: {
presentCoordinator.enqueue(.alert(alertTask.content({
Alert(title: Text("Alert"))
})))
})
.alert(isPresented: $alertTask.isPresented) {
presentCoordinator.dequeue()
}
}
}
Your ideas, bug fixes, improvements, etc. are all welcome. 🐢
UIPresentCoordinator is licensed under the MIT license. See LICENSE for more info.