fuzzygoat
fuzzygoat

Reputation: 26223

Checking for feature / class on OS version?

I am developing an application that I want to run on both iOS4 and iOS5 but for users of iOS5 I want to use an iOS5 feature as part of the interface (iOS4 users will get something less exciting). My question is what is the procedure for checking the availability of a particular OS on a device. My understanding is that I don't check the OS version but rather the availability of a particular class, can anyone help me out of the best way to do this ...

Upvotes: 0

Views: 400

Answers (4)

karlbecker_com
karlbecker_com

Reputation: 959

If you're trying to get the UIStepper to gracefully degrade in 4.x, you cannot only use

if( NSClassFromString(@"UIStepper") )

Instead, you must also check for a UIStepper-specific selector having a response. As Harry Wood suggested in the comment under Bo A, a good way to do it is:

if( NSClassFromString(@"UIStepper") && [theStepper respondsToSelector:@selector(setValue:)] )

This solved the issue of my app crashing under iOS 4.x.

Harry Wood helped me solve the issue I was seeing, and I would like him to get the credit.

Upvotes: 2

Bo A
Bo A

Reputation: 3154

Isn't iOS 5 under NDA?

Anyway, to check if a feature exists then try this:

if (NSClassFromString(@"UIStepper")) {
    //enter code here
} else {
    //enter code here
}

Customise to your needs.

EDIT: iOS 5 is now released so I can now add "UIStepper" to my code.

Upvotes: 4

Filip Radelic
Filip Radelic

Reputation: 26683

Class stepperClass = NSClassFromString(@"UIStepper");
if (stepperClass) {
    // class is available, use it
} else {
    // class not available, don't use it or use something else
}

Upvotes: 2

brain
brain

Reputation: 5546

iOS5 is under NDA so i wouldnt mention any new classes that may or may not exist. However the following code should do what you want. It's lifted from the docs.

if ([UINewClass class]) {
    // Create an instance of the class and use it.

} else {
    // Alternate code path to follow when the
    // class is not available.

}

This uses Weak Linking and therefore requires that the new class (UINewClass) to be in the SDK you are using to compile. It is a relatively new feature introduced in iOS 4.2 and might not be supported by all the frameworks. A workaround is to use the older style (from the same link as above):

Class cls = NSClassFromString (@"UINewClass");
if (cls) {
    // Create an instance of the class and use it.
} else {
    // Alternate code path to follow when the
    // class is not available.
}

Upvotes: 2

Related Questions