Search code examples
iosobjective-calertpause

How to pause code between the display of 2 alerts


I have some iOS code where I need to be able to display 2 back to back alerts. When the user clicks "ok" on the first alert then I need to display the second alert. Because displaying an alert does not "pause" the code, my second alert tries to display at almost the same time as my first alert and things break (second alert does not show up).

What I need is a way to pause the code while displaying the first alert. Once the user clicks "ok" on the first alert then it can display the second alert.

What is the proper way to wait for first alert to finish before moving on to the second alert?

Here is my full code in case it helps. Each alert is triggered by a condition. If both conditions are true then I hit the back to back alert issue I have described.

    UIAlertController* alert;
    UIAlertAction* defaultAction;
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if(status != kCLAuthorizationStatusAuthorizedAlways) {
        alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                    message:@"Reminder will not work unless you enable 'Always' location."
                                             preferredStyle:UIAlertControllerStyleAlert];

        defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {}];


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

    }


    UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if(settings.types==0) {
        alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                    message:@"Reminder will not work unless you enable notifications."
                                             preferredStyle:UIAlertControllerStyleAlert];

        defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {}];

        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }

Solution

  • All you have to do is but the second alert in the handler for the first:

    UIAlertController* alert;
    UIAlertAction* defaultAction;
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if(status != kCLAuthorizationStatusAuthorizedAlways) {
        alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                    message:@"Reminder will not work unless you enable 'Always' location."
                                             preferredStyle:UIAlertControllerStyleAlert];
    
        defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
    
            UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    
            if(settings.types==0) {
    
                inner_alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                    message:@"Reminder will not work unless you enable notifications."
                                             preferredStyle:UIAlertControllerStyleAlert];
    
                inner_defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {}];
    
                [inner_alert addAction:inner_defaultAction];
                [self presentViewController:inner_alert animated:YES completion:nil];
            }
        }];
    
    
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    
    }
    

    This way when you hit "Ok" it will then call the code for the second alert.