Change UIAlertView After IOS 8

On September 18, 2015, in iPhone iPad Object-C, by James Liu

UIAlertView is deprecated in iOS 8 and later. Therefore, we need to use the latest API to show alert view. To make the code more compatible, we can check the system version and implement the alert view basing the iOS version. Here is the example source code I am using:

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Network Error" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:
                                        ^(UIAlertAction *action) {
                                            NSLog(@"action is triggered");
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:
                NSLog(@"Alert Error Display");
    } else {
        //use old alertview
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Network Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

Leave a Reply

WordPress Themes

Free WordPress Themes