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?
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"
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.
#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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With