Do not use screen size for this, it is better to use the hardware model. We get more and more screen sizes every year, the smaller the screen size of the hard code in the code, the better for your future.
You need a helper function to get the name of the machine. I use dispatch_once to not query the system multiple times for data that will not change.
NSString* machineName() { static NSString* name = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ struct utsname systemInfo; uname(&systemInfo); name = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; }); return name; }
Then define a few macros as needed:
#define IS_IPHONE_6 [machineName() isEqualToString:@"iPhone7,2"] #define IS_IPHONE_6_PLUS [machineName() isEqualToString:@"iPhone7,1"]
For some models it is more difficult:
#define IS_IPHONE_5s [machineName() hasPrefix:@"iPhone6,"]
Finally, use macros in your code:
if (IS_IPHONE_6) {
Note. This is the answer to your question (detecting models with macros), but you are doing it wrong IMHO. You should use auto-detection and size classes if you do not support really old versions of iOS ...
source share