My iOS app uses a custom height for the UINavigationBar
which leads to some problems on the new iPhone X.
Does someone already know how to reliable detect programmatically (in Objective-C) if an app is running on iPhone X?
EDIT:
Of course checking the size of the screen is possible, however, I wonder if there is some "build in" method like TARGET_OS_IPHONE
to detect iOS...
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812)
NSLog(@"iPhone X");
}
EDIT 2:
I do not think, that my question is a duplicate of the linked question. Of course, there are methods to "measure" different properties of the current device and to use the results to decide which device is used. However, this was not the actual point of my question as I tried to emphasize in my first edit.
The actual question is: "Is it possible to directly detect if the current device is an iPhone X (e.g. by some SDK feature) or do I have to use indirect measurements"?
By the answers given so far, I assume that the answer is "No, there is no direct methods. Measurements are the way to go".
For those getting 2001px instead of 2436px for the native bounds height (like me), it is because you built your app with an older SDK, before iOS 11 (Xcode 8 instead of Xcode 9). With an older SDK, iOS will display the apps "black boxed" on the iPhone X instead of extending the screen edge-to-edge, beyond the top "sensor notch". This reduces the screen size which is why that property returns 2001 instead of 2436.
The simplest solution is to just check for both sizes if you are only interested in device detection. I used this method for detecting FaceID while building with an older Xcode SDK which doesn't have the ENUM value specifying the biometric type. In this situation, device detection using screen height seemed like the best way to know whether the device had FaceID vs TouchID without having to update Xcode.
Do NOT use screen pixel size as other solutions have suggested, this is bad as it can result in false positives for future devices; will not work if UIWindow hasn't yet rendered (AppDelegate), won't work in landscape apps, and can fail on simulator if scale is set.
I've, instead, made a macro for this purpose, it's very easy to use and relies on hardware flags to prevent the aforementioned issues.
Edit: Updated to support iPhoneX, iPhone XS, iPhoneXR, iPhoneXS Max
To Use:
Yup, really.
Macro:
Just copy paste this anywhere, I prefer the very bottom of my .h file after
@end
I rely on the Status Bar Frame height to detect if it's an iPhone X :
This is for application un portrait. You could also check the size according to the device orientation. Also, on other iPhones, the Status Bar may be hidden, so the frame height is
0
. On iPhone X, the Status Bar is never hidden.There are several reasons to want to know what the device is.
You can check the device height (and width). This is useful for layout, but you usually don't want to do that if you want to know the exact device.
For layout purposes, you can also use
UIView.safeAreaInsets
.If you want to display the device name, for example, to be included in a email for diagnostic purposes, after retrieving the device model using
sysctl ()
, you can use the equivalent of this to figure the name:I was using Peter Kreinz's code (because it was clean and did what I needed) but then I realized it works just when the device is on portrait (since top padding will be on top, obviously) So I created an extension to handle all the orientations with its respective paddings, without relaying on the screen size:
And on your call site you just:
SWIFT 4+ Answer
iPhone X, XR, XS, XSMAX:
Note: Need real device for test
Reference