Reputation: 63
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)
}
Upvotes: 1
Views: 1098
Reputation: 12154
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
.
Upvotes: 5
Reputation: 1489
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)
}
Upvotes: -1