iOS is getting fragmented. More devices to support with major iPhone hardware differences (0-2 cameras, no telephony - 3G - CDMA), screen resolutions (original, double, iPad) and especially more older operating system versions out in the wild.
To reach as many users as possible you have to check whether something exists before you try to use it. Customizing code based on iOS version number is possible, but there are other ways to do it.
Showing posts with label respondsToSelector. Show all posts
Showing posts with label respondsToSelector. Show all posts
Sunday, January 16, 2011
How to Check if iOS Supports Class, Method or Keyword
Labels:
Class,
conformsToProtocol,
fragmentation,
iOS4,
NSClassFromString,
NSDictionary,
NSString,
objectForKey,
protocol,
respondsToSelector,
selector
Monday, December 20, 2010
How to Detect iOS Version in Code
Bumbed into a case where code crashed iOS 4.0.2, but worked with iOS 4.2. Since crash happened because of missing NSString *const, couldn't use respondsToSelector to choose, which code to run. Decided to simply check what is current iOS version:
Always run code in real device! My development device iPhone4 with iOS 4.1 returns "4.1" as string, but that gives 4.0999999 as float value !!!
float version = [[[UIDevice currentDevice] systemVersion] floatValue];Please note how three part version number gets converted to float:
if (version > 4.0)
{
// iOS 4.2 specific code
}
else
{
// iOS 4.0.2 specific code
}
NSString *s = [[UIDevice currentDevice] systemVersion];I'm not really happy with the solution, but it works. Can live with that.
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
NSLog(@"Version: %@ and %f", s, version);
NSLog(@"Version: %@ and %f", @"3.1.3", [@"3.1.3" floatValue]);
Version: 4.0.2 and 4.000000
Version: 3.1.3 and 3.100000
Update:
Always run code in real device! My development device iPhone4 with iOS 4.1 returns "4.1" as string, but that gives 4.0999999 as float value !!!
Labels:
currentDevice,
float,
floatValue,
NSLog,
NSString,
respondsToSelector,
systemVersion,
UIDevice
Subscribe to:
Posts (Atom)