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

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

问题描述

我的 iOS 应用对 UINavigationBar 使用了自定义高度,这导致了新 iPhone X 上的一些问题.

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

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

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {CGSize screenSize = [[UIScreen mainScreen] bounds].size;如果(屏幕尺寸.高度 == 812)NSLog(@"iPhone X");}

编辑 2:

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

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

根据目前给出的答案,我假设答案是不,没有直接的方法.测量是要走的路".

解决方案

根据您的问题,答案是否定的.没有直接的方法.如需更多信息,您可以在此处获取信息:

  • 来自

    Swift 3 及更高版本:

    if UIDevice().userInterfaceIdiom == .phone {切换 UIScreen.main.nativeBounds.height {案例1136:打印(iPhone 5 或 5S 或 5C")案例1334:打印(iPhone 6/6S/7/8")案例 1920、2208:打印(iPhone 6+/6S+/7+/8+")案例2436:打印(iPhone X/XS/11 Pro")案例2688:打印(iPhone XS Max/11 Pro Max")案例1792:打印(iPhone XR/11")默认:打印(未知")}}

    目标 C:

    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {案例1136:printf(iPhone 5 或 5S 或 5C");休息;案例1334:printf(iPhone 6/6S/7/8");休息;案例 1920:案例2208:printf(iPhone 6+/6S+/7+/8+");休息;案例2436:printf(iPhone X/XS/11 Pro");休息;案例2688:printf(iPhone XS Max/11 Pro Max");休息;案例1792:printf(iPhone XR/11");休息;默认:printf(未知");休息;}}

    Xamarin.iOS:

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {如果((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale)== 1136){Console.WriteLine(iPhone 5 or 5S or 5C");} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {Console.WriteLine(iPhone 6/6S/7/8");} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {Console.WriteLine(iPhone 6+/6S+/7+/8+");} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {Console.WriteLine(iPhone X, XS, 11 Pro");} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {Console.WriteLine(iPhone XS Max, 11 Pro Max");} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {Console.WriteLine(iPhone XR, 11");} 别的 {Console.WriteLine(未知");}}

    根据您的问题如下:

    或者使用 screenSize.height 作为 float 812.0f 而不是 int 812.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {CGSize screenSize = [[UIScreen mainScreen] bounds].size;//iPhone X、XS 上的 812.0//iPhone XS Max、XR 上的 896.0.如果(屏幕尺寸.高度 >= 812.0f)NSLog(@iPhone X");}

    有关更多信息,您可以参考 iOS 人机界面指南中的以下页面:

    斯威夫特:

    topNotch检测:

    <块引用>

    如果有人考虑使用缺口来检测 iPhoneX,请注意横向"所有 iPhone 都一样.

    var hasTopNotch: Bool {如果#available(iOS 13.0,*){返回 UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.safeAreaInsets.top ??0 >20}别的{返回 UIApplication.shared.delegate?.window??.safeAreaInsets.top ??0 >20}返回假}

    目标 C:

    - (BOOL)hasTopNotch {如果(@available(iOS 13.0,*)){return [self keyWindow].safeAreaInsets.top >20.0;}别的{return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top >20.0;}返回否;}- (UIWindow*)keyWindow {UIWindow *foundWindow = nil;NSArray *windows = [[UIApplication sharedApplication]windows];for (UIWindow *window in windows) {如果(window.isKeyWindow){发现窗口 = 窗口;休息;}}返回发现窗口;}

    更新:

    不要使用 userInterfaceIdiom 属性来标识设备类型,因为 userInterfaceIdiom 的文档 解释说:

    <块引用>

    对于通用应用程序,您可以使用此属性为特定类型的设备定制应用程序的行为.例如,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 here:

    and

    The iPhone X height is 2436 px

    From Device Screen Sizes and resolutions:

    From Device Screen Sizes and Orientations:

    Swift 3 and later:

    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/XS/11 Pro")
            
            case 2688:
                print("iPhone XS Max/11 Pro Max")
            
            case 1792:
                print("iPhone XR/ 11 ")
            
            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:
                case 2208:
                    printf("iPhone 6+/6S+/7+/8+");
                    break;
    
               case 2436:
                    printf("iPhone X/XS/11 Pro");
                     break;
    
                case 2688:
                    printf("iPhone XS Max/11 Pro Max");
                     break;
    
                case 1792:
                    printf("iPhone XR/ 11 ");
                     break;
    
                default:
                    printf("Unknown");
                    break;
            }
        }
    

    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");
        } else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {
            Console.WriteLine("iPhone 6/6S/7/8");
        } else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {
            Console.WriteLine("iPhone 6+/6S+/7+/8+");
        } else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {
            Console.WriteLine("iPhone X, XS, 11 Pro");
        } else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {
            Console.WriteLine("iPhone XS Max, 11 Pro Max");
        } else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {
            Console.WriteLine("iPhone XR, 11");
        } else {
            Console.WriteLine("Unknown");
        }
    }
    

    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;
            // 812.0 on iPhone X, XS
            // 896.0 on iPhone XS Max, XR.
    
        if (screenSize.height >= 812.0f)
            NSLog(@"iPhone X");
        }
    

    For more information you can refer the following page in iOS Human Interface Guidelines:

    Swift:

    Detect with topNotch:

    If anyone considering using notch to detect iPhoneX, mind that on "landscape" its same for all iPhones.

    var hasTopNotch: Bool {
        if #available(iOS 13.0,  *) {
            return UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.safeAreaInsets.top ?? 0 > 20
        }else{
         return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
        }
    
        return false
    }
    

    Objective-C:

    - (BOOL)hasTopNotch {
       if (@available(iOS 13.0, *)) {
           return [self keyWindow].safeAreaInsets.top > 20.0;
       }else{
           return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
       }
       return  NO;
    }
    
    - (UIWindow*)keyWindow {
        UIWindow        *foundWindow = nil;
        NSArray         *windows = [[UIApplication sharedApplication]windows];
        for (UIWindow   *window in windows) {
            if (window.isKeyWindow) {
                foundWindow = window;
                break;
            }
        }
        return foundWindow;
    }
    

    UPDATE:

    Do not use the userInterfaceIdiom property to identify the device type, as the documentation for userInterfaceIdiom 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 the following for details:

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

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