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)
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 :-)!
Simple App Delegate method to show an UIAlertController (in Swift)
Try using
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
All you need is a viewController
object to present the AlertController from.
In Swift 4:
self.window?.rootViewController?.present(importAlert, 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.
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
Iphone:Daily Local Notifications
Amazon Aws iOS Sdk: How to List All File Names in a Folder
Negative Number Modulo in Swift
How to Create a Hex Color String Uicolor Initializer in Swift
Install Apps on Device Without Itunes
Can't Pass Data Correctly to Modal Presentation Using Foreach and Coredata in Swiftui
Firebase Cloud Messaging Doesn't Create Push Notifications But Gets Information
Expand Uilabel Inside Uitableview with "More" Button Like Instagram
Launchscreen.Xib Not Displaying My Custom Font
Swiftui - How to Initialize an Observedobject Using an Environmentobject as a Parameter
How to Make Post Nsurlrequest with 2 Parameters
Prevent Uialertcontroller to Dismiss
What Does the Text Inside Parentheses in @Interface and @Implementation Directives Mean
How to Convert a Nib-Based Project to a Storyboard-Based
Copy Nsattributedstring in Uipasteboard
Why Is Uitextfield.Text an Optional
You Are Not Authorised to Use This Service Itunes App Upload Error