Hey. I would like to include UIAlertController in my SwiftUI project because I have more customization options with UIAlertController than with swiftui alert. I've already partially done that with this code:
struct CustomAlertManager {
static let shared = CustomAlertManager()
private init() {}
func showAlert() {
let alert = UIAlertController(title: "Achtung", message: "Sie werden weiter geleitet", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default) { action in
}
alert.addAction(okAction)
if let controller = UIApplication.shared.rootViewController {
controller.present(alert, animated: true)
}
}
}
My UIApplication extension:
extension UIApplication {
var rootViewController: UIViewController? {
let scene = self.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
return scene?.keyWindow?.rootViewController
}
}
But now to my problem: This works pretty much fine but when I want to present an UIAlertController inside of a sheet the alert is not displayed (maybe its is unter the sheet). Note: No, the controller (UIApplication.shared.rootViewController) is not empty, so the .present(_:) method will be called 👍. Best regards!