Online today imageNamed and imageWithContentsOfFile
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.

 Objective-C |  copy code |? 
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:

 Objective-C |  copy code |? 
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:

 Objective-C |  copy code |? 
1
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
2
if (version >= 3.0)
3
    {
4
    // iPhone 3.0 code here
5
    }

4 Responses to “To Be Compatible With OS 3.0”

  1. Kvinto Says:
    Thank you very much!

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

    This code is very useful for me.
  2. kooworx Says:
    I am glad that I can help. :)
  3. Cletus Degasperis Says:
    Consider & Hold your iPad for Free! -> http://bit.ly/cFBuis
  4. Stuart Radforth Says:
    Very handy snippet for a simple backward compatibility test, thanks!
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version>=4.0)

Leave a Reply