3

why UIAlertController in iOS7 received nil value while need to presented but worked in iOS8 was good, may i know that is because iOS7 not support the UIAlertController class ?

UIAlertController *view=[UIAlertController
                        alertControllerWithTitle:@"Hello"
                        message:nil
                        preferredStyle:UIAlertControllerStyleAlert];

 [self presentViewController:view animated:NO completion:nil];
Deeper
  • 129
  • 1
  • 13
  • UIAlertController was introduced for iOS 8. It wont work in iOS7. For iOS7 you should use UIAlertView. – Sreejith Nov 03 '14 at 08:09
  • http://stackoverflow.com/questions/25111011/uialertview-uialertcontroller-ios-7-and-ios-8-compatibility – Sreejith Nov 03 '14 at 08:09
  • I have created a simple wrapper class that can be used with both. It mimics the UIAlertController. https://github.com/Reggian/RAAlertController – reggian Jan 30 '15 at 14:49

3 Answers3

4

These class is only available in iOS8 and later. See the class reference

Jens
  • 67,715
  • 15
  • 98
  • 113
3

In order to display AlertView in both iOS 8 and lower versions you can use the following code:

if ([self isiOS8OrAbove]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         [self.navigationController popViewControllerAnimated:YES];
                                                     }];

    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
} else {
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles: nil];
    [alertView show];
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)isiOS8OrAbove {
    NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"8.0"
                                                                       options: NSNumericSearch];
    return (order == NSOrderedSame || order == NSOrderedDescending);
}
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
0

You might be using os version less then iOS 8.

If you compile your code with Xcode 6 and use a device with iOS version < 8.0 then you will face this issue. I would suggest the following

- (void)showXYZAlert
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        // Handle UIAlertController
        [self showXYZAlertController];
    }
    else
    {
        // Handle UIAlertView
        [self showXYZAlertControllerView];
    }
}
BLC
  • 2,240
  • 25
  • 27