18

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

Community
  • 1
  • 1
Tung Do
  • 1,423
  • 3
  • 13
  • 27
  • 7
    Both 4.1 and 4.2 are ≥ 2.2. – kennytm Nov 18 '10 at 07:34
  • 5
    #if/#endif are *compile-time* directives. All they do is compare against your build settings to see what OS version you are targeting. But the same code will be output regardless of what OS you run on. You want a *run-time* check. – benzado Nov 18 '10 at 08:15
  • Try this one : http://jayprakashdubey.blogspot.in/2014/07/check-device-os-version.html – Jayprakash Dubey Jul 30 '14 at 10:13

6 Answers6

27

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.

Moshe Gottlieb
  • 3,963
  • 25
  • 41
21
float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Bourne
  • 10,094
  • 5
  • 24
  • 51
6

Or, even better:

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

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

if( systemVersion > 4.1 ){...}

Henry Cooke
  • 2,563
  • 2
  • 23
  • 22
5

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

walter
  • 364
  • 3
  • 6
3

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.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
1

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

Macarse
  • 91,829
  • 44
  • 175
  • 230
aba
  • 11
  • 1