I've been using the next line in my constants to differentiate between devices and get back the number of the device. What's the appropriate way to identify iPhone 5 and still keep it in a one line format?
#define iPhoneType [[UIScreen mainScreen] scale]==2 || [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad ? @"4" : @"3"
Thanks
Edit: A lot of good answers but my goal is to keep it in a one line format for all devices.
Edit: Based on the comments, this question needs some clarification. Here are the requirements:
@"3"
, @"4"
, or @"5"
depending on the iOS device.@"5"
.@"4"
.@"3"
.According to your question I'm assuming you want to identify the hardware device, not the iOS version.
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 6.x Edition
BSD License, Use at your own risk
*/
#include <sys/sysctl.h>
NSString* getSysInfoByName(char* typeSpecifier) {
size_t size;
sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
char *answer = malloc(size);
sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
return results;
}
NSString* platform() {
return getSysInfoByName("hw.machine");
}
Import those functions in the .pch, then you are free to call this one liner:
BOOL isIphone5 = [platform() hasPrefix:@"iPhone5"];
It works for any device. See UIDevice-Hardware.m for a list of the strings returned.
Assuming the updated requirements are correct, the following should work:
#define iPhoneType (fabs((double)[UIScreen mainScreen].bounds.size.height - (double)568) < DBL_EPSILON) ? @"5" : ([UIScreen mainScreen].scale==2 || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"4" : @"3")
This will return @"5"
for the 4" screened iPhones and iPod touches. This will return @"4"
for all iPads and retina iPhones and iPod touches. And it will return @"3"
for non-retina iPhones and iPod touches.
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