Present UIAlertController from AppDelegate [duplicate]

You can use this code as well if you want to launch it from didFinishLaunchingWithOptions.Hope this helps. dispatch_async(dispatch_get_main_queue(), { let importantAlert: UIAlertController = UIAlertController(title: “Action Sheet”, message: “Hello I was presented from appdelegate ;)”, preferredStyle: .ActionSheet) self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil) }) And up-to-date: DispatchQueue.main.async { let alert = UIAlertController(title: “Hello!”, message: “Greetings from AppDelegate.”, … Read more

Presenting a view controller modally from an action sheet’s delegate in iOS8 – iOS11

Update: As of iOS 9 SDK, UIActionSheet is deprecated, so do not expect a fix regarding this issue. It is best to start using UIAlertController when possible. The problem seems to come from Apple’s switch to using UIAlertController internally to implement the functionality of alert views and action sheets. The issue is seen mostly on … Read more

Prevent UIAlertController to dismiss

EDIT: Updated for Swift 5 EDIT: Updated to include @skywalker’s feedback So I actually got this to work. In short, it involves adding a long-press gesture recognizer to the UIAlertController that triggers before the dismissal occurs. First, create lazily loaded computed variables in your view controller for your UIAlertController and the UIAlertAction you want to … Read more

How to change the background color of the UIAlertController?

You have to step some views deeper: let subview = actionController.view.subviews.first! as UIView let alertContentView = subview.subviews.first! as UIView alertContentView.backgroundColor = UIColor.blackColor() And maybe you want to keep original corner radius: alertContentView.layer.cornerRadius = 5; Sorry for the “Swifting” but i’m not familiar with Objective-C. I hope that’s similar. Of course it’s also important to change … Read more

How to show UIAlertController from Appdelegate

try this Objective-C UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; topWindow.rootViewController = [UIViewController new]; topWindow.windowLevel = UIWindowLevelAlert + 1; UIAlertController* alert = [UIAlertController alertControllerWithTitle:@”APNS” message:@”received Notification” preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@”OK”,@”confirm”) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { // continue your work // important to hide the window after work completed. // this also keeps a reference … Read more

How to dismiss UIAlertController when tap outside the UIAlertController?

Add a separate cancel action with style UIAlertActionStyleCancel. So that when user taps outside, you would get the callback. Obj-c UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@”Alert Title” message:@”A Message” preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@”Cancel” style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // Called when user taps outside }]]; Swift 5.0 let alertController = UIAlertController(title: “Alert Title”, message: “A Message”, preferredStyle: … Read more

AlertController is not in the window hierarchy

If you’re instancing your UIAlertController from a modal controller, you need to do it in viewDidAppear, not in viewDidLoad or you’ll get an error. Here’s my code (Swift 4): override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let alertController = UIAlertController(title: “Foo”, message: “Bar”, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: “OK”, style: .cancel, handler: nil)) present(alertController, animated: true, completion: … Read more

UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

The detection pattern is identical to the Objective-C style. You need to detect whether the current active runtime has the ability to instantiate this class if objc_getClass(“UIAlertController”) != nil { println(“UIAlertController can be instantiated”) //make and use a UIAlertController } else { println(“UIAlertController can NOT be instantiated”) //make and use a UIAlertView } Don’t try … Read more