How to detect an iPhone X

The iPhone X is like a “glimpse into the future”, according to Apple. By “the future” they mean “this is what three year old Samsung components could look like if they ran iOS”. iPhone X has been described as gimmicky and as “$1k for Face ID, with no other benefits”.

Overall it was not as warmly received as the 10 year anniversary device as it perhaps could have been.

But all that aside, as developers, we still need to support it and perhaps even give the device some unique treatment, be that with a dedicated Storyboard to make use of the larger screen, or to give attention to a feature other phones don’t have. Before we can do that though, we need to figure out a way to test if we’re actually dealing with an iPhone X device.

I know of no better way to do this than the trusted screen height check. Let’s see how this might work.

We know that the one thing that sets iPhone X apart from all its many friends is its screen height. To figure this out, we need a way to ask for a property that will always give us the portrait heigh, no matter if the user holds their device in portrait or landscape. I’ve recently used this trick when I determined the height of an iPad and load a different storyboard for different iPad sizes in this article.

The magic property to use is called fixedCoordinateSpace and can be found on the UIScreen class. Here’s how we can use it:

// figure out screen height (portrait)
int height = [UIScreen mainScreen].fixedCoordinateSpace.bounds.size.height;

Now all we need to know is the screen height of an iPhone X, and perhaps other supported devices, so we can make an educated decision about what device we’re working with. We can then use this information to give each iPhone model some unique treatment (for example, by loading dedicated storyboards).

Here’s what we know about the iPhone X from Apple’s Human Interface Guidelines:

Its screen height is 812 points, and although its pixel density is @3x (like the iPhone Plus models), 812 is the value we’re going to see returned from the above method.

Here’s what other iPhone models will return:

  • iPhone 8/7/6s/6 = 667
  • iPhone 8/7/6s/6 Plus = 736
  • iPhone 5s / SE = 568

Putting this together in a switch method, we can make decisions based on that:

- (NSString *)detectDevice {
    
    // figure out screen height (portrait)
    int height = [UIScreen mainScreen].fixedCoordinateSpace.bounds.size.height;
    
    // based on the height, give out an NSString for device type
    NSString *deviceModel;
    
    switch (height) {
        case 568:
            deviceModel = @"iPhone 5s or SE";
            break;
            
            case 667:
            deviceModel = @"iPhone 8/7/6s/6";
            break;
            
            case 736:
            deviceModel = @"iPhone 8/7/6s/6 Plus";
            break;
            
            case 812:
            deviceModel = @"iPhone X";
            break;
            
        default:
            deviceModel = @"Dunno. Maybe it's an Android...?";
            break;
    }
    return deviceModel;
}

We could go one step further and see if we’re perhaps dealing with an iPad, and figure out what type of iPad, but since I’ve covered that in a previous article I won’t repeat myself here. I’m sure you get the picture.

This method, or a decision process like this is best suited for the AppDelegate.m file or another central class. I hope this helps you get started on the journey of discovering The Future on iPhone X (queue spooky futuristic music).





Leave a Reply