Jul 26
While, as we all know, OS 3.0 has been installed or upgraded on most of the iPhones, but, there are still a lot of iPod Touch not upgraded. So, by now, we still need to develop our apps based on the OS 2.2+ then try to make it compatible with OS 3.0.
When we test our apps on OS 2.2+ and OS 3.0, we found out that some thing different in the two platforms, so if your want the same code to be built for different OS, here is the trick to do it.
| 01 | - (void)viewDidLoad {
|
| 02 | |
| 03 | #ifdef __IPHONE_3_0
|
| 04 | NSInteger offset = -40;
|
| 05 | #else
|
| 06 | NSInteger offset = 0;
|
| 07 | #endif
|
| 08 | |
| 09 | /* Your code here */
|
| 10 | |
| 11 | [super viewDidLoad];
|
| 12 | } |
You can notice that we used pre-processor directives for the conditional compilation. But there is still a problem: we have to build two binaries for the different OS. What if we want to build only one binary that can be compatible with both platforms?
This code will do it for you:
| 01 | - (void)viewDidLoad {
|
| 02 |
|
| 03 | NSString *version = [[UIDevice currentDevice] systemVersion];
|
| 04 |
|
| 05 | int offset = -40;
|
| 06 |
|
| 07 | if ([version isEqualToString:@"2.2"] || [version isEqualToString:@"2.2.1"]) {
|
| 08 | offset = 0;
|
| 09 | }
|
| 10 | |
| 11 | /* Your code here */
|
| 12 |
|
| 13 | [super viewDidLoad];
|
| 14 | } |
This code will detect the
systemVersion of the OS, then we can get one binary to run on different OS.
For another usage, you can use this:
| 1 | float version = [[[UIDevice currentDevice] systemVersion] floatValue];
|
| 2 | if (version >= 3.0)
|
| 3 | {
|
| 4 | // iPhone 3.0 code here
|
| 5 | } |
Leave a Reply
September 4th, 2009 at 10:46 am Thank you very much!
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
This code is very useful for me.
September 15th, 2009 at 8:41 am I am glad that I can help. :)
April 6th, 2010 at 1:38 pm Consider & Hold your iPad for Free! -> http://bit.ly/cFBuis
August 24th, 2010 at 8:42 am Very handy snippet for a simple backward compatibility test, thanks!
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version>=4.0)