检测设备是否为iPhone X. [英] Detect if the device is iPhone X

查看:129
本文介绍了检测设备是否为iPhone X.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用使用 UINavigationBar 的自定义高度,这会导致新iPhone X出现问题。



如果应用程序在iPhone X上运行,是否有人已经知道如何可靠以编程方式检测(在Objective-C中)?



编辑:



当然可以检查屏幕的大小,但是,我想知道是否有一些内置方法,如 TARGET_OS_IPHONE 检测iOS ...

  if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds] .size;
if(screenSize.height == 812)
NSLog(@iPhone X);
}

编辑2:



我不认为,我的问题是链接问题的重复。当然,有一些方法可以测量当前设备的不同属性,并使用结果来决定使用哪个设备。然而,这不是我的问题的实际问题,因为我在第一次编辑中试图强调。



实际问题是:是否可以直接检测当前设备是否是iPhone X(例如,通过某些SDK功能)或者我是否必须使用间接测量



到目前为止给出的答案,我认为答案是不,没有直接的方法。测量是方式去。

解决方案

根据您的问题,答案是否,没有直接的方法,有关更多信息,您可以从中获取信息



swift 3及以上

 如果UIDevice()。userInterfaceIdiom == .phone {
切换UIScreen.main.nativeBounds.height {
case 1136:
print(iPhone 5或5S或5C)
案例1334:
打印(iPhone 6 / 6S / 7/8)
案例1920,2208:
打印(iPhone 6 + / 6S + / 7 + / 8 +)
案例2436:
打印(iPhone X)
默认值:
打印(未知)
}
}

目标C

  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

switch((int)[[UIScreen mainScreen] nativeBounds] .size。身高){

案例1136:
printf(iPhone 5或5S或5C);
休息;
case 1334:
printf(iPhone 6 / 6S / 7/8);
休息;
case 1920,2208:
printf(iPhone 6 + / 6S + / 7 + / 8 +);
休息;
案例2436:
printf(iPhone X);
休息;
默认值:
printf(unknown);
}
}

Xamarin.iOS

  if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
{
if((UIScreen。 MainScreen.Bounds.Height * UIScreen.MainScreen.Scale)== 1136)
{
Console.WriteLine(iPhone 5或5S或5C);
}
//等...
}

基于在你的问题上如下



或使用screenSize.height作为浮动 812.0f 不是int 812

  if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
CGSize screenSize = [ [UIScreen mainScreen] bounds] .size;
if(screenSize.height == 812.0f)
NSLog(@iPhone X);
}

了解更多信息这里



更新



请勿使用 userInterfaceIdiom 属性识别设备类型,如Apple文档所述,


对于通用应用程序,您可以使用此属性来定制应用程序的行为特定类型的设备。例如,iPhone和iPad设备具有不同的屏幕尺寸,因此您可能希望根据当前设备的类型创建不同的视图和控件。


也就是说,此属性仅用于标识正在运行的应用程序的视图样式。但是,iPhone应用程序(不是通用)可以通过App store安装在iPad设备上,在这种情况下,userInterfaceIdiom也会返回UIUserInterfaceIdiomPhone。



右边方法是通过 uname 获取机器名称,检查此主题了解详情。


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".

解决方案

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.

这篇关于检测设备是否为iPhone X.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆