开发者

How to detect current iOS is 4.1 or 4.2? [duplicate]

This question already has answers here: Closed 1开发者_StackOverflow1 years ago.

Possible Duplicate:

Check iPhone iOS Version

I tried with this code:

#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_2_2)
    NSLog(@"iPhone 4.2");
#endif

but when I run it again on simulator 4.1, I still get the log "iPhone 4.2" in console.

Thanks for helping me.

Tung Do


You should probably avoid asking about the system version altogether.

A better design would ask about a specific feature.

For instance: if (NSClassFromString(@"UIPrintInfo")) would tell you if the current device supports the printing API,available in 4.2 or higher.

That way you can plan your code to use a feature if it's available, on not based on the OS version.


float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 


Or, even better:

CGFloat systemVersion = [[[ UIDevice currentDevice ] systemVersion ] floatValue ];

Then, you can do a simple equality check, e.g.

if( systemVersion > 4.1 ){...}


You can do this either at compile time or at runtime.

For compile time, you use the so-called availability macros:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > 40000 // __IPHONE_4_0
    NSLog( @"After Version 4.0" ); 
#else
    NSLog( @"Version 4.0 or earlier" ); 
#endif

However, the problem with compile-time is that you have to maintain multiple binaries. If you want this binary to run on pre-4.x, then you should weak link to the iOS libraries.

if(kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iPhoneOS_4_0)
{
    NSLog( @"After Version 4.0" ); 
}
else
{
    NSLog( @"Version 4.0 or earlier" ); 
}

There's a good tutorial here: http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html


Rule 1: Do not depend on version or device type to determine if a feature is available. Different devices (iPhone, iPod Touch and iPad) can get the same feature but at different versions of the OS. Link weakly against frameworks and missing classes will be nil. Or use NSClassFromString() function that also returns nil if a class do not exist. Also use -[NSObject respondsToSelector:] to query if a method exists or not.

Rule 2: Apple discourages from using the defined constants (such as __IPHONE_2_2) when checking for versions, instead use their numerical values as such:

#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 40200)
   NSLog(@"BUILT against iPhone 4.2 or later");
#endif

But do take notice using #if compile-time directive will only check what version of the SDK you build against not which version of the OS will later run on.


try this one:

NSString * v = [[[UIDevice currentDevice] systemVersion] substringToIndex:0];

more reference: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜