1

I tried to create a pop-up alert message in my app delegate but it's not showing up at all. The program is written is Swift 4.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
shim
  • 9,289
  • 12
  • 69
  • 108
TFLOW9Z1
  • 63
  • 2
  • 9
  • See [this](https://stackoverflow.com/a/36156077/7576100) – Jack Oct 30 '17 at 06:02
  • Possible duplicate of [How to show UIAlertController from Appdelegate](https://stackoverflow.com/questions/36155769/how-to-show-uialertcontroller-from-appdelegate) – shim May 31 '18 at 13:52

2 Answers2

5

According to the documentation for didFinishLaunching:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

So I don't think you can show an alert in didFinishLaunchingWithOptions.

Try to move your code to applicationDidBecomeActive.

shim
  • 9,289
  • 12
  • 69
  • 108
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • yeah moved my pop-up message in appdelgate swift file to func applicationDidBecomeActive. The pop-up started working with no problem. Thanks for catching the error... – TFLOW9Z1 Oct 30 '17 at 19:08
-1

If there’s one rule to remember in iOS native programming, it is this: UI components can only be properly manipulated on main thread. Keep this in mind and I’m sure it will spare you from headaches in the future.

 let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

    let actionYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
        print("Handler YES")
    })

    let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
        print("Handler CANCEL")
    })

    alert.addAction(actionYes)
    alert.addAction(actionCancel)

    DispatchQueue.main.async {
        self.window?.rootViewController?.present(alert, animated: true, completion: nil)
    }
MrWaqasAhmed
  • 1,479
  • 12
  • 12