UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility ios ios

UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility


The detection pattern is identical to the Objective-C style.

You need to detect whether the current active runtime has the ability to instantiate this class

if objc_getClass("UIAlertController") != nil {     println("UIAlertController can be instantiated")      //make and use a UIAlertController } else {      println("UIAlertController can NOT be instantiated")      //make and use a UIAlertView}

Don't try and work out this based on the OS version. You need to detect abilities NOT OS.

EDIT

The original detector for this answer NSClassFromString("UIAlertController") fails under -O optimisation so its been changed to the current version which does work for Release builds

EDIT 2

NSClassFromString is working at all optimisations in Xcode 6.3/Swift 1.2


For non-swift code, pure objective-C do this

if ([UIAlertController class])    {        // use UIAlertController        UIAlertController *alert= [UIAlertController                                      alertControllerWithTitle:@"Enter Folder Name"                                      message:@"Keep it short and sweet"                                      preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault                                                   handler:^(UIAlertAction * action){                                                       //Do Some action here                                                       UITextField *textField = alert.textFields[0];                                                       NSLog(@"text was %@", textField.text);                                                   }];        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault                                                       handler:^(UIAlertAction * action) {                                                           NSLog(@"cancel btn");                                                           [alert dismissViewControllerAnimated:YES completion:nil];                                                       }];        [alert addAction:ok];        [alert addAction:cancel];        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {            textField.placeholder = @"folder name";            textField.keyboardType = UIKeyboardTypeDefault;        }];        [self presentViewController:alert animated:YES completion:nil];    }    else    {        // use UIAlertView        UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"                                                         message:@"Keep it short and sweet"                                                        delegate:self                                               cancelButtonTitle:@"Cancel"                                               otherButtonTitles:@"OK", nil];        dialog.alertViewStyle = UIAlertViewStylePlainTextInput;        dialog.tag = 400;        [dialog show];    }


I was annoyed that I kept having to write out both situations, so I wrote a compatible UIAlertController that works for iOS 7 as well so I just threw it up on GitHub. I did my best to replicate the (much better) methods of adding buttons and actions of the UIAlertController. Works with both Objective-C and Swift. I'm posting this as I found this question when searching on Google and figured it could be helpful for others.

https://github.com/BayPhillips/compatible-alert-controller