Show alert in AppDelegate in Swift [duplicate] Show alert in AppDelegate in Swift [duplicate] ios ios

Show alert in AppDelegate in Swift [duplicate]


This is what i'm using now to do that.

var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {                    UIAlertAction in                    NSLog("OK Pressed")                }var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {                    UIAlertAction in                    NSLog("Cancel Pressed")                }alertController.addAction(okAction)alertController.addAction(cancelAction)self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)


Swift 5:

let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertController.Style.alert)        // add an action (button)alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))       // show the alertself.window?.rootViewController?.present(alert, animated: true, completion: nil)


As per Jorge's answer, updated for Swift 4

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {        UIAlertAction in        NSLog("OK Pressed")    }let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel) {        UIAlertAction in        NSLog("Cancel Pressed")    }alertController.addAction(okAction)alertController.addAction(cancelAction)self.window?.rootViewController?.present(alertController, animated: true, completion: nil)