开发者

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 devic开发者_StackOverflow中文版e. 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 ...


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.


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.
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜