How to differentiate between iphone4 and iphone 3
You could check the scale of the screen.
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
//iPhone 4
}
For detecting retina display on all devices including the new iPad
+(BOOL)isRetinaDisplay {
// since we call this alot, cache it
static CGFloat scale = 0.0;
if (scale == 0.0) {
// NOTE: In order to detect the Retina display reliably on all iOS devices,
// you need to check if the device is running iOS4+ and if the
// [UIScreen mainScreen].scale property is equal to 2.0.
// You CANNOT assume a device is running iOS4+ if the scale property exists,
// as the iPad 3.2 also contains this property.
// On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0
// in 2x mode -- even though we know that device does not contain a Retina display.
// Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both
// 1x and 2x modes. You can test this yourself in the simulator.
// I test for the -displayLinkWithTarget:selector: method on the main screen
// which exists in iOS4.x but not iOS3.2, and then check the screen's scale:
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
scale = 2.0;
return YES;
} else {
scale = 1.0;
return NO;
}
}
return scale > 1.0;
}
Credit to: Adriano Paladini http://developer.appcelerator.com/question/133826/detecting-new-ipad-3-dpi-and-retina
- (NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine];
free(machine);
return platform;
}
- (NSString *) platformString
{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"Original iPhone";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3G[S]";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
return @"Unknown";
}
Despite what Apple's docs say, the UIScreen's scale property isn't just available in iOS4, it's also available in 3.2 on the iPad. This means it's probably an unreliable way to check which device you're on.
Instead, you should check if contentScaleFactor is available on your main window(or any UIView), and then check the scale value.
Checking the scale
property is not sufficient, as on iPad 3.2 in 2x mode, the scale
property exists and will return 2.0, but we know that device does NOT have a Retina display.
I've created at category on UIScreen
to do this. For a more detailed explanation, see my answer to Detect Retina Display. Here's the code:
@interface UIScreen(ZBScreenRetinaAdditions)
// Returns YES if this is a Retina display.
- (BOOL)zb_isRetina;
@end
@implementation UIScreen(ZBScreenRetinaAdditions)
- (BOOL)zb_isRetina {
return [self respondsToSelector:@selector(displayLinkWithTarget:selector:)] && (self.scale == 2.0);
}
@end
Usage example:
if ([UIScreen mainScreen] zb_isRetina) {
// Retina display
}