Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting iPhone6 & iPhone6 Plus using macros

I am attempting to detect whether the current device is iPhone5, iPhone6 or iPhone 6 Plus.

In my app I am already using this macro to detect iPhone 5 which works perfectly.

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

Similarly, I use this macro to detect 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 IS_IPHONE_6 and IS_IPHONE_6_PLUS macros do not return true when the device is held in LANDSCAPE. However they do work as expect while the device is held in PORTRAIT. What gives?

Also if anyone has a better recommendation to detect iPhone5, 6 and 6 Plus please share.

like image 305
leejona Avatar asked Oct 01 '14 18:10

leejona


People also ask

How can I detect my iPhone?

You can use Find My iPhone on iCloud.com to find the approximate location of your iPhone, iPad, iPod touch, Mac, Apple Watch, AirPods, and Beats product if Find My is set up and the device is online. To sign in to Find My iPhone, go to icloud.com/find.

Can iPhone 6 be tracked?

There is also the possibility to search your Apple iPhone 6 from a Mac, iPad or iPhone through the “Find My iPhone” app, the steps are the same as from the browser, you must log in to your iCloud account, select the lost device and you can perform the actions of locating, sounding the device and deleting all your data.

How can I check original iPhone 6 06?

To find out the IMEI number of your iPhone by entering *#06# on the keypad. Alternatively, you can also find the IMEI number from Settings->About->General. Plus, it will also be printed on the back of the box. (Check out this Apple Support Page to know the location of IMEI number for various models).


3 Answers

This 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 UI is a zoomed up version of iPhone 5. This is reflected in the macros.

Usage: http://pastie.org/9687735

like image 182
hfossli Avatar answered Oct 19 '22 00:10

hfossli


Don't use the screen size for this, it's better to use the hardware model. We are getting more and more screen sizes every year, the less you hard-code screen dimensions in your code the better for your future self.

You need a helper function to get the machine name. I'm using dispatch_once to avoid querying the system multiple times for data that won't 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 is trickier:

#define IS_IPHONE_5s     [machineName() hasPrefix:@"iPhone6,"]

Finally, use the macros in your code:

if (IS_IPHONE_6) {
// for the 6
}

Note: This answer your question (detect models with macros) but you're doing it wrong IMHO. You should use autolayout and size classes, unless you support really old iOS versions...

like image 28
djromero Avatar answered Oct 18 '22 23:10

djromero


Take a look at this answer: iOS - How to get device make and model? it doesn't use macros but it does the job. I've seen some similar problems with macros to detect iPhone 6 and iPhone 6 plus. So it would be a better idea to try out the answer from @Ohhmee

EDIT: Of course there probably is a solution detecting it with macros. But I don't know that and I can't find a solution so i'm suggesting a different approach.

like image 1
Bas Avatar answered Oct 19 '22 01:10

Bas