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 to the window until the action is invoked.
topWindow.hidden = YES; // if you want to hide the topwindow then use this
topWindow = nil; // if you want to remove the topwindow then use this
}]];
[topWindow makeKeyAndVisible];
[topWindow.rootViewController presentViewController:alert animated:YES completion:nil];
Swift3 and above
var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindow.Level.alert + 1
let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in
// continue your work
// important to hide the window after work completed.
// this also keeps a reference to the window until the action is invoked.
topWindow?.isHidden = true // if you want to hide the topwindow then use this
topWindow = nil // if you want to hide the topwindow then use this
})
topWindow?.makeKeyAndVisible()
topWindow?.rootViewController?.present(alert, animated: true, completion: nil)
Detail description: http://www.thecave.com/2015/09/28/how-to-present-an-alert-view-using-uialertcontroller-when-you-dont-have-a-view-controller/
How to present UIAlertView from appDelegate
Ok i finally got it, you need to find the active VC using this before trying to present your alertController :
let navigationController = application.windows[0].rootViewController as UINavigationController
let activeViewCont = navigationController.visibleViewController
activeViewCont.presentViewController(alertController, animated: true, completion: nil)
How do I add a UIAlertController in app delegate (obj-c)
Per Apple Documentation, UIAlertController
is just like any another view controller which needs a view controller as base to present on.
To present an UIAlertController
from AppDelegate
, you can create a temporary window with an empty view controller in it and present on top of it. Something like this:
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:nil];
This window will automatically be taken out of memory once alert is dismissed.
You can also present your alert on rootViewController
if it has been initialized and is added in view hierarchy.
I hope you got why you are getting this error now. Technically, AppDelegate
is not a view controller and so does not respond to presentViewController:animated:completion:
method.
UIAlertController not showing from AppDelegate
Alright, I solved this problem by creating a UIWindow
object with an empty view controller and presenting my alert in it. Apparently, once alert controller is dismissed, this temporary window also go away.
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:^{
[aBlockSelf alertPresented];
}];
Thanks to this SO thread where a WWDC lab session is referred :-)!
Present alert from AppDelegate when app is ready for presenting an alert
You need applicationDidBecomeActive
, but make sure it's called when you return from background also , so wrap code in once var if you don't like it to be called again
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
Presenting Alert Controller from the delegate
Use below line:
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
Instead of
vc.present(alertController, animated: true, completion: nil)
than problem is solved.
Example:
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
let alertController = UIAlertController(title: "Alert Title", message:
"Message", preferredStyle: .actionSheet)
let okAction = UIAlertAction(title: "Ok", style:.default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
})
Related Topics
iOS 11 Layout Guidance About Safe Area for iPhone X
How to Draw a Point Using Core Graphics
Combine Two String in Different Language Rtl & Ltr
Timestamp Function That Has Been Working Reliably Just Caused Exc_Bad_Instruction
Uiactionsheet Cancel Button Strange Behaviour
Overlay an Image Over Another Image in iOS
Get Instance of Viewcontroller from Appdelegate in Swift
Perform a Deeplink from Swiftui Widget on Tap
Prevent Dispatch_After() Background Task from Being Executed
iOS - Is Motion Activity Enabled in Settings > Privacy > Motion Activity