简体   繁体   English

如何在app delegate(obj-c)中添加UIAlertController

[英]How do I add a UIAlertController in app delegate (obj-c)

This is the code I was using prior to UIAlertView being deprecated: 这是我在弃用UIAlertView之前使用的代码:

//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. 我正在尝试更新它以使用UIAlertController。 heres the code I'm using for that 下面是我正在使用的代码

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

problem is this code: 问题是这段代码:

[self presentViewController:alert animated:YES completion:nil]; [self presentViewController:alert animated:YES completion:nil];

gives me this error: 给我这个错误:

/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15: No visible @interface for 'AppDelegate' declares the selector 'presentViewController:animated:completion:' /Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15:'AppDelegate'没有可见的@interface声明选择器'presentViewController:animated:completion:'

I can put that code in viewDidLoad but I don't want the alert popping up every time the users go to the TableView. 可以将该代码放在viewDidLoad但我不希望每次用户转到TableView时都会弹出警报。 I just want it to load ONLY when the app is opened. 我只想在应用程序打开时加载它。

how do I go about doing this? 我该怎么做呢?

Per Apple Documentation , UIAlertController is just like any another view controller which needs a view controller as base to present on. 根据Apple文档UIAlertController就像任何另一个需要视图控制器作为基础的视图控制器一样。

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. 要从AppDelegate呈现UIAlertController ,您可以创建一个临时窗口,其中包含一个空视图控制器并显示在其上。 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. 如果已经初始化并添加到视图层次结构中,您还可以在rootViewController上显示警报。

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. 从技术上讲, AppDelegate不是视图控制器,因此不响应presentViewController:animated:completion:方法。

  let navigationController = application.windows[0].rootViewController as UINavigationController

            let activeViewCont = navigationController.visibleViewController

            activeViewCont.presentViewController(alertController, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM