tags:

views:

1252

answers:

2

I would like to detect iPhone OS version in my app. I have tried using detection code but was advised to use macros. Does someone experience, can you post sample code, and what library to use.

+5  A: 

I am not certain what macros you are being advised to use. I always thought that the standard way to find the version number so that it will be compatiable with future and previous versions was to use

NSString* versionNumber = [[UIDevice currentDevice] systemVersion]

which gives it as a NSString such as @"2.0" or @"2.2.1"

There are the Version constants that describe the version of Foundation classes being used, with NSFoundationVersionNumber but I am uncertain of how reliable this will be in older and future code.

Brandon Bodnár
Beat me to the answer :P To add, the macro's that they are most likely referring to is pre-processor macro's (#define). This isn't how it works but I can see how people can assume that the version would be defined in such a way.
Jay
The preprocessor macros would let you do conditional compiling based on the iPhone OS version you're targeting, but not detect the iPhone OS version the application is running on. As an example: #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_2_0
Brad Larson
+1  A: 

Look in Availability.h, specifically the statements:

#define __IPHONE_2_0     20000
#define __IPHONE_2_1     20100
#define __IPHONE_2_2     20200
#define __IPHONE_3_0     30000
#define __IPHONE_NA      99999

And don't forget to read the giant header comment. The preprocessor macros are definitely the safest way to section out your code by os version.

CajunLuke
I believe those are for writing libraries for iPhone apps but not for the apps themselves. But that probably is what the mike was alluding to. But I am fairly certain that this is for compilation and not for runtime version detection.
Brandon Bodnár
Yes - it would be compile-time only.
CajunLuke