Detect iPhone6 ​​and iPhone6 ​​Plus using macros

I am trying to determine if the current device is iPhone5, iPhone6 ​​or iPhone 6 Plus.

In my application, I already use this macro to detect an iPhone 5 that works great.

#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON) 

Similarly, I use this macro to detect the iPhone 6 and iPhone 6 Plus.

 #define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON) #define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON) 

The IS_IPHONE_5 macro works as expected in any orientation.

My problem is that the macros IS_IPHONE_6 and IS_IPHONE_6_PLUS do not return true when the device is held in LANDSCAPE. However, they work as expected while the device is held in PORTRAIT. What gives?

Also, if someone has the best recommendation for finding iPhone5, 6 and 6 Plus, please share.

+6
source share
4 answers

It is tested and designed for any combination of iOS system versions and SDK versions.

 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0) #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) 

Note. If iPhone 6 is in zoomed mode, the user interface is an enlarged version of iPhone 5. This is reflected in the macros.

Use: http://pastie.org/9687735

+8
source

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) { // for the 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 ...

+3
source

Take a look at this answer: iOS - How to get the device brand and model? she does not use macros, but does the job. I saw some similar macro issues for detecting iPhone 6 and iPhone 6 plus. So it would be better to try the answer from @Ohhmee

EDIT: Of course, maybe a solution that detects it with macros. But I don’t know this, and I can’t find a solution, so I propose a different approach.

+1
source

Your macros are funny and seem to indicate a fear of floating point arithmetic.

 #define IS_IPHONE_5 ([UIScreen mainScreen].bounds.size.height == 568) #define IS_IPHONE_6 ([UIScreen mainScreen].bounds.size.height == 667) #define IS_IPHONE_6_PLUS ([UIScreen mainScreen].bounds.size.height == 736) 

will work just as well (or just as bad). Most likely, they will work just as badly.

If you're interested in the features of the iPhone 6 or 6+, check the features, not the screen size. It is likely that Apple will soon have a 4-inch phone with all the features of the iPhone 6. Or a cheap 6c with a large screen, but without the features of the iPhone 6.

For user interface etc. just grab the mainScreen borders and place your views accordingly. At this point, hardcoding fixed sizes is absolutely ridiculous and will bite you. You should be able to run, for example, a 12-inch split-screen iPad without any problems, and heaven knows what screen size will be.

0
source

Source: https://habr.com/ru/post/976120/


All Articles