¿Cómo agrego un UIAlertController en el delegado de la aplicación (obj-c)?

Este es el código que estaba usando antes de que UIAlertView estuviera en desuso:

//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] == NSOrdenetworkingDescending; //default code updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrdenetworkingDescending; //force update //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrdenetworkingDescending; //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 shanetworkingApplication] openURL:[NSURL URLWithString:@"PLIST_URL_HERE"]]; exit(0); 

Estoy intentando actualizarlo para usar UIAlertController en su lugar. aquí está el código que estoy usando para eso

 //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] == NSOrdenetworkingDescending; //default code updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrdenetworkingDescending; //force update //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrdenetworkingDescending; //force no update if (updateAvailable) { UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"UPDATE AVAILABLE" message:@"Click OK to update the application to the latest version" prefernetworkingStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [[UIApplication shanetworkingApplication] 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]; } } 

El problema es este código:

[self presentViewController: alerta animada: SÍ finalización: nil];

me da este error:

/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15: No visible @interface para 'AppDelegate' declara el selector 'presentViewController: animated: completion:'

Puedo poner ese código en viewDidLoad pero no quiero que aparezca la alerta cada vez que los usuarios vayan a TableView. Solo quiero que se cargue SÓLO cuando la aplicación esté abierta.

¿Cómo voy a hacer esto?

Según la documentation de Apple , UIAlertController es como cualquier otro controller de vista que necesita un controller de vista como base para presentar.

Para presentar un UIAlertController desde AppDelegate , puede crear una window temporal con un controller de vista vacío en él y presente en la parte superior. Algo como esto:

 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]; 

Esta window se eliminará automáticamente de la memory una vez que se descarte la alerta.

También puede presentar su alerta en rootViewController si se ha inicializado y se agrega en la jerarquía de vistas.

Espero que sepas por qué estás recibiendo este error ahora. Técnicamente, AppDelegate no es un controller de vista y por lo tanto no responde a presentViewController:animated:completion: method.