11

Since I'm using iOS 13, each of my UIAlertController shows up for about half a second and disappears instantly before any user action. Any idea ?

As I use UIAlertController from different parts of my app, I use an extension that allows me to pop up both from classical views and collectionView (cell, header etc...)

public extension UIAlertController {
    func show() {
        let win = UIWindow(frame: UIScreen.main.bounds)
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        vc.view.tintColor = Theme.mainAccentColor
        win.rootViewController = vc
        win.windowLevel = UIWindow.Level.alert + 1
        win.makeKeyAndVisible()
        vc.present(self, animated: true, completion: nil)
    }
}

And here is an example of this extension use :

fileprivate func showMissingAlert() {
        let alert = UIAlertController(title: "blablabla", message: "blablablablabla blabla", preferredStyle: UIAlertController.Style.alert)
        alert.show()
        alert.view.tintColor = Theme.mainAccentColor
        let cancelAction = UIAlertAction(title: "OK, blabla", style: .default, handler: {(alert: UIAlertAction!) in print("ok, leave")})
        alert.addAction(cancelAction)
    }

And further in my code :

showMissingAlert()

Before iOS 13, every UIAlert worked fine... Since I moved to iOS 13, and even iOS 13.1, it became a big mess... :(

  • Any idea about what could cause this?

  • And how to prevent using UIAlert as subliminal message :) ?

1
  • But making a second window in this way was always wrong; now the framework has caught up with you.
    – matt
    Feb 3, 2020 at 3:41

4 Answers 4

12

I've got exactly the same problem, and fixed it by holding the window in which the alert is being presented in a strong variable.

You can hold a window for presenting alerts in you AppDelegate, for example, and use it in your UIAlertController extension.

//In app delegate
let alertWindow: UIWindow = {
    let win = UIWindow(frame: UIScreen.main.bounds)
    win.windowLevel = UIWindow.Level.alert + 1
    return win
}()

Then, in your extension:

public extension UIAlertController {
    func show() {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        vc.view.tintColor = Theme.mainAccentColor
        appDelegate.alertWindow.rootViewController = vc
        appDelegate.alertWindow.makeKeyAndVisible()
        vc.present(self, animated: true, completion: nil)
    }
}

You will also need to make sure your alert window is removed from view when your alert is dismissed, otherwise your app will become unresponsive, as all taps will be handled by the (invisible) alert window, that's still on top of everything. I do this by adding this code to the handlers of all actions in the alert:

(UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true
14
  • 1
    This is not going to work well when your app supports scenes in iOS 13.
    – rmaddy
    Sep 27, 2019 at 14:47
  • Wahou, great ! It works well. But once closed, none of my buttons, textfields, tabbar or navigation bar are enabled anymore... Did you also have this problem ?
    – Creanomy
    Sep 27, 2019 at 15:03
  • @Creanomy you will need to make sure the window is removed from the view when the alert view is dismissed. I’ll edit my answer later with how I did it.
    – pepsy
    Sep 27, 2019 at 15:06
  • 1
    @user_Dennis_Mostajo are you having the same problem described in the question? The alert appearing for a split second then disappearing? This will present the alert on a window with a window level of (alert)+1, which will probably be the topmost window in your application.
    – pepsy
    Oct 4, 2019 at 19:12
  • 2
    @user_Dennis_Mostajo Are you sure you're holding a strong reference to your alert window somewhere safe? In the link you posted, you're creating it inside your showAlertGlobally func.
    – pepsy
    Oct 4, 2019 at 20:00
2

You can try this solution also. It is working form me.

write below method in your class.

func presentViewController(alertController: UIAlertController, completion: (() -> Void)? = nil) {
        if var topController = UIApplication.shared.keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }

            DispatchQueue.main.async {
                topController.present(alertController, animated: true, completion: completion)
            }
        }
    }

Then call it from your code as below

let alertController = UIAlertController(title: "Discard Photo?",
                                                message: "Your photo will not be attached",
                                                preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Keep Photo", style: .default, handler: nil))
        alertController.addAction(UIAlertAction(title: "Discard", style: .default) { (_) -> Void in
            self.PhotoStack.deletePhoto(at: index)
            self.cameraBtn.isEnabled = true
        })

        self.presentViewController(alertController: alertController)
1
  • @Skywalker, My Guess is sometimes you cant find the main window. Check the view controller hierarchy when it doesn't work. Feb 19, 2021 at 9:52
1

Based on pepsy answer. If you don't want to care about alertWindow.isHidden = true stuff, you can do something like this:

class AlertHandler {
    private static let alertWindow: UIWindow = {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.windowLevel = UIWindow.Level.alert + 1
        return window
    }()

    private var alertController: UIAlertController

    init(title: String?,
         message: String?) {
        alertController = UIAlertController(title: title,
                                            message: message,
                                            preferredStyle: .alert)
    }

    func addAction(title: String?,
                   style: UIAlertAction.Style,
                   handler: ((UIAlertAction) -> Void)? = nil) {
        let action = UIAlertAction(title: title,
                                   style: style) { action in
                                    handler?(action)
                                    AlertHandler.alertWindow.isHidden = true
        }
        alertController.addAction(action)
    }

    func present() {
        AlertHandler.alertWindow.rootViewController = UIViewController()
        AlertHandler.alertWindow.makeKeyAndVisible()
        AlertHandler.alertWindow.rootViewController?.present(alertController,
                                                             animated: true,
                                                             completion: nil)
    }
}
1

Before you considered to make alertController in window, please check your animation or other UI things animated together.

e.g, if you do self.dismiss(VC) and self.present(alertController) together, it makes problem

Best way to develop is not ignore irregular ui events, but check any other things first which has some problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.