Detect if the device is iPhone X
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 (eg 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".
Based on your question, the answer is No, there are no direct methods, for more information you can get the information from here1 and here2
if you want to detect the iphone X height use 2436px
Device Screen Sizes and Orientations
swift 3 and above
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")
case 1334:
print("iPhone 6/6S/7/8")
case 1920, 2208:
print("iPhone 6+/6S+/7+/8+")
case 2436:
print("iPhone X")
default:
print("unknown")
}
}
objective C
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {
case 1136:
printf("iPhone 5 or 5S or 5C");
break;
case 1334:
printf("iPhone 6/6S/7/8");
break;
case 1920, 2208:
printf("iPhone 6+/6S+/7+/8+");
break;
case 2436:
printf("iPhone X");
break;
default:
printf("unknown");
}
}
Xamarin.iOS
if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
{
if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1136)
{
Console.WriteLine("iPhone 5 or 5S or 5C");
}
//etc...
}
based on your question as follow
or use screenSize.height as float 812.0f
not int 812
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812.0f)
NSLog(@"iPhone X");
}
for more information you can get here
UPDATE
DO NOT use the userInterfaceIdiom
property to identify the device type, as the apple documentation explains,
For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.
That is, this property is just used to identify the running app's view style. However, the iPhone app (not the universal) could be installed in iPad device via App store, in that case, the userInterfaceIdiom will return the UIUserInterfaceIdiomPhone, too.
The right way is to get the machine name via uname
, check this thread for detail.
Another possibility, which works currently because the iPhone X is the only one with a notch at the top. That is what I am really detecting here:
BOOL iPhoneX = NO;
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.top > 0.0) {
iPhoneX = YES;
}
}
And of course, you might need to check the left and right safe area insets if you are in landscape orientation.
Edit: _window is the UIWindow of the AppDelegate, where this check is done in application didFinishLaunchingWithOptions.
You can do like this to detect iPhone X device according to dimension.
Swift
if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {
//iPhone X
}
Objective - C
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone && UIScreen.mainScreen.nativeBounds.size.height == 2436) {
//iPhone X
}
But ,
This is not sufficient way. What if Apple announced next iPhone with same dimension of iPhone X. so the best way is to use Hardware string to detect the device.
For newer device Hardware string is as below.
iPhone 8 - iPhone10,1 or iPhone 10,4
iPhone 8 Plus - iPhone10,2 or iPhone 10,5
iPhone X - iPhone10,3 or iPhone10,6
链接地址: http://www.djcxy.com/p/19240.html上一篇: 如何在2个网站之间共享表格
下一篇: 检测设备是否为iPhone X