27
votes

I want to conform to the UIAlertController used in iOS 8 since UIAlertView is now deprecated. Is there a way that I can use this without breaking support for iOS 7? Is there some kind of if condition I can do to check for iOS 8 otherwise do something else for iOS 7 support?

11
FYI - Since your Deployment Target is iOS 7 (or earlier), you can just use UIAlertView. This will keep your code a lot simpler. - rmaddy
I wasn't aware that Swift had a respondsToSelector... Also, the UIAlertView is screwed up in ios8, it doesn't scroll where as it does in ios 7, which is why I want to try to use both - Rocky Pulley
The scroll doesn't work on the UIAlertController either - jcesarmobile

11 Answers

99
votes

I think a much better way to check if a class exists (since iOS 4.2) is:

if([ClassToBeChecked class]) {

   // use it

} else {

  // use alternative

}

In your case, that would be:

if ([UIAlertController class]) {
   // use UIAlertController

} else {
  // use UIAlertView

}
12
votes

Objective C (as mentioned above)

if ([UIAlertController class]) {
    // use UIAlertController

} else {
    // use UIAlertView

}

Swift

if objc_getClass("UIAlertController") == nil  {
       // use UIAlertView 

} else {
  // use UIAlertController

}

Don't use if NSClassFromString("UIAlertController") == nil It is not working because the signature for this method is func NSClassFromString(_ aClassName: String!) -> AnyClass!

4
votes

Please see the answer of Erwan (below my answer) as I see it is the best.

--

You can check the iOS version to use appropriate control like this:

if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending)) {
    // use UIAlertView
}
else {
    // use UIAlertController
}
3
votes

As others have already mentioned - always check whether a feature exists. I believe the safest approach is following:

if (NSClassFromString(@"UIAlertController")) {
    // use UIAlertController
} else {
    // use UIAlertView
}

With the obvious risk of entering a class name with a typo. :)

From documentation of NClassFromString:

[Returns] The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

Availability iOS (2.0 and later)

2
votes

Solution for checking iOS version in Swift

switch (UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch)) {
    case .OrderedAscending:
       println("iOS < 8.0")

    case .OrderedSame, .OrderedDescending:
       println("iOS >= 8.0")
}

Con of this solution: it is simply bad practice to check against OS version numbers, whichever way you do it. One should never hard code dependencies in this way, always check for features, capabilities or the existence of a class. Consider this; Apple may release a backwards compatible version of a class, if they did then the code you suggest would never use it as your logic looks for an OS version number and NOT the existence of the class.

(Source of this information)

Solution for checking the class' existence in Swift

if (objc_getClass("UIAlertController") == nil) {
   // iOS 7
} else {
   // iOS 8+
}

Do not use if (NSClassFromString("UIAlertController") == nil) because it works correctly on the iOS simulator using iOS 7.1 and 8.2, but if you test on a real device using iOS 7.1, you will unfortunately notice that you will never pass through the else part of the code snippet.

1
votes
// Above ios 8.0
float os_version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (os_version >= 8.000000)
{
      //Use UIAlertController    
}
else
{
     //UIAlertView
}
1
votes

Create simple utility function to reduce code

CODE :

// pass minimum required iOS version
BOOL isOSSupported(NSString *minRequiredVersion)
{
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL isOSSupported = ([currSysVer compare:minRequiredVersion options:NSNumericSearch] != NSOrderedAscending) && 
                                  ![currSysVer isEqualToString:@"Unknown"];
    return isOSSupported;
}


USE :

if(isOSSupported("8.0")
{
// Code for iOS8 and above
}
else
{
// Code for iOS7 and below
}



Or Use system constant NSFoundationVersionNumber_iOS_7_1 as below
if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1)
{
   // Code for iOS8 and above
}
else
{
   // Code for iOS7 and below
}


for more options Link

1
votes

I have created very simple wrapper in Objective-C, that supports both - old iOS UIAlertView and new one UIAlertViewController

https://github.com/MartinPerry/UIAlert/

It also brings the new action blocks usage to old UIAlertView

Sample:

MyAlertMessage * a = [[MyAlertMessage alloc] initWithTitle:@"Hello" WithMessage:@"World"];

[a addButton:BUTTON_OK WithTitle:@"OK" WithAction:^(void *action) { 
  NSLog(@"Button OK at index 0 click"); 
}];

[a addButton:BUTTON_CANCEL WithTitle:@"Cancel" WithAction:^(void *action) {
  NSLog(@"Button Cancel at index 1 click"); 
}];

[a show];
1
votes

I have written one class that wrap the UIAlertView and use UIAlertController. For the programmer is transparently hence is sufficient import this classes in the project. The utility of this classes is when in a old project there are more UIAlertView to change. Link: https://github.com/kennymuse/UIAlertView

0
votes

Method one

by ios system version check

#define iOSVersionLessThan(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
// below ios8 ,create UIAlertView
if(iOSVersionLessThan(@"7.0")){
     // todo

// ios8 and above ,UIActionController avaliable
}else{
    // todo
}

Method two

by system feature detect

// create UIActionController 
if([UIActionController class]){
    // todo
// create UIAlertView
}else{
    // todo
}

But,there's a third lib named PSTAlertController that deal with backwards compatible to iOS 7 of UIActionSheet and UIAlertView.

ref to

0
votes

Try below code. It works fine for both iOS 8 and below version.

if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleCancel
                             handler:^(UIAlertAction *action)
                             {

                             }];
[alertVC addAction:cancelAction];

[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{

}];
}
else{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}