How to add UIAlertController to application delegate (obj-c)

This is the code I used before the UIAlertView was deprecated:

//OLD UIALERTVIEW - (void)applicationDidBecomeActive:(UIApplication *)application { // 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. // check to see if newer version exists to alert the user BOOL updateAvailable = NO; NSDictionary *updateDictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"PLIST_URL_HERE"]]; NSArray *items = [updateDictionary objectForKey:@"items"]; NSDictionary *itemDict = [items lastObject]; NSDictionary *metaData = [itemDict objectForKey:@"metadata"]; NSString *newversion = [metaData valueForKey:@"bundle-version"]; NSString *currentversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; //updateAvailable = [newversion compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //default code updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force update //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force no update if (updateAvailable) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UPDATE AVAILABLE" message:@"Click OK to update the application to the latest version" delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"OK", nil]; [alert show]; } } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != [alertView cancelButtonIndex]) { // point to the pList on dropbox to install the [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"PLIST_URL_HERE"]]; exit(0); 

I am trying to update it to use UIAlertController instead. heres the code i use for this

 //NEW UIACTIONCONTROLLER - (void)applicationDidBecomeActive:(UIApplication *)application { // 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. // check to see if newer version exists to alert the user BOOL updateAvailable = NO; NSDictionary *updateDictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"PLIST_URL_HERE"]]; NSArray *items = [updateDictionary objectForKey:@"items"]; NSDictionary *itemDict = [items lastObject]; NSDictionary *metaData = [itemDict objectForKey:@"metadata"]; NSString *newversion = [metaData valueForKey:@"bundle-version"]; NSString *currentversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; //updateAvailable = [newversion compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //default code updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force update //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force no update if (updateAvailable) { UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"UPDATE AVAILABLE" message:@"Click OK to update the application to the latest version" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"PLIST_URL_HERE"]]; exit(0); }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:ok]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil]; } } 

The problem is this code:

[self presentViewController: warning animated: YES termination: zero];

gives me this error:

/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15: No visible @interface for 'AppDelegate' declares a selector 'presentViewController: animated: completion:'

I can put this code in viewDidLoad , but I don’t want the warning to appear every time users go to TableView. I just want it to load ONLY when the application opens.

How can i do this?

+2
ios objective-c uialertcontroller
source share
1 answer

Per Apple Documentation , UIAlertController similar to any other view controller that needs a view controller as the basis for the view.

To present a UIAlertController from AppDelegate , you can create a temporary window with an empty view controller in it and be present on top of it. Something like that:

 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 be automatically erased after the warning is rejected.

You can also submit your warning to rootViewController if it has been initialized and added to the view hierarchy.

I hope you understand why you are getting this error now. Technically, AppDelegate not a view controller and therefore does not respond to the presentViewController:animated:completion: method.

+8
source share

All Articles