Conditional support for iOS 6 features in iOS 5 app

How can you support iOS6 features in an application with the Minimal Deployment Target installed in iOS 5.0?

For example, if a user has iOS 5, will he see one UIActionSheet , if a user has iOS 6, will he see another UIActionSheet for iOS 6? How do you do this? I have Xcode 4.5 and want the application to run on iOS 5.

+7
source share
1 answer

You should always prefer to discover available methods / functions rather than iOS versions, and then provided that the method is available.

See Apple Documentation .

For example, in iOS 5, we will do something like this to display the modal view controller:

 [self presentModalViewController:viewController animated:YES]; 

In iOS 6, the presentModalViewController:animated: UIViewController deprecated, you should use presentViewController:animated:completion: in iOS 6, but how do you know when to use what?

You can determine the version of iOS and specify the if statement if you are using the old or the latest, but this is fragile, you will make a mistake, maybe the new OS in the future will have a new way to do this.

The correct way to handle this:

 if([self respondsToSelector:@selector(presentViewController:animated:completion:)]) [self presentViewController:viewController animated:YES completion:^{/* done */}]; else [self presentModalViewController:viewController animated:YES]; 

You can even argue that you should be more strict and do something like:

 if([self respondsToSelector:@selector(presentViewController:animated:completion:)]) [self presentViewController:viewController animated:YES completion:^{/* done */}]; else if([self respondsToSelector:@selector(presentViewController:animated:)]) [self presentModalViewController:viewController animated:YES]; else NSLog(@"Oooops, what system is this !!! - should never see this !"); 

I'm not sure about your UIActionSheet example, as far as I know, this is the same thing on iOS 5 and 6. You might be thinking of a UIActivityViewController for sharing, and you might need to go back to UIActionSheet if you're on iOS 5, so you can check that class is available, see here how to do it.

+19
source

All Articles