Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to detect hardware type, iPhone4 or iPhone5?

I am setting up an application where I want to make a number of changes to the view layout based on the type of iPhone in use. Specifically I want to know if the vertical screen resolution is 1136 (iPhone5) or 960 (iPhone4). One of the things I want to do is adjust the height of my UITableViewCells in a UITableView so that no partial cells are displayed when the app is run on either phone.

My question is: what is the best way to detect the phone hardware type in use so that I can make the appropriate changes to my view layout?

like image 295
fuzzygoat Avatar asked Oct 24 '12 09:10

fuzzygoat


3 Answers

I dont have iphone 5 but on the other phone this code worked great:

NSMutableDictionary *devices = [[NSMutableDictionary alloc] init];
[devices setObject:@"simulator"                     forKey:@"i386"];
[devices setObject:@"iPod Touch"                    forKey:@"iPod1,1"];
[devices setObject:@"iPod Touch Second Generation"  forKey:@"iPod2,1"];
[devices setObject:@"iPod Touch Third Generation"   forKey:@"iPod3,1"];
[devices setObject:@"iPod Touch Fourth Generation"  forKey:@"iPod4,1"];
[devices setObject:@"iPhone"                        forKey:@"iPhone1,1"];
[devices setObject:@"iPhone 3G"                     forKey:@"iPhone1,2"];
[devices setObject:@"iPhone 3GS"                    forKey:@"iPhone2,1"];
[devices setObject:@"iPad"                          forKey:@"iPad1,1"];
[devices setObject:@"iPad 2"                        forKey:@"iPad2,1"];
[devices setObject:@"iPhone 4"                      forKey:@"iPhone3,1"];
[devices setObject:@"iPhone 4S"                     forKey:@"iPhone4"];
[devices setObject:@"iPhone 5"                      forKey:@"iPhone5"];

- (NSString *) getDeviceModel
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [devices objectForKey:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]];
}

Remember to import:

#import "sys/utsname.h"
like image 193
edzio27 Avatar answered Nov 04 '22 08:11

edzio27


You should really use the autolayout features to make your views adapt to whatever format the screen is.

However, if you only want to know if the device is an iPhone 5 (ie has 1136px in height) you could use:

[ [ UIScreen mainScreen ] bounds ].size.height

Could ofcourse be turned into a macro.

like image 8
Johan André Avatar answered Nov 04 '22 08:11

Johan André


#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

define this macro in some constant.h file and use it any where to check

if(IS_IPHONE5)
{
// iphone 5
}
else
{
// earlier
}
like image 2
Saad Avatar answered Nov 04 '22 09:11

Saad