如何通过编程检测iPhone XS或iPhone X?

7
我的一个应用连接到一个网页应用服务,向用户提供设备特定的新闻。为了适应最新的iPhone版本,我需要通过编程区分iPhone XS和iPhone X。如何实现? [[UIScreen mainScreen] bounds].size 一直是区分不同设备的好起点。然而,iPhone XS和iPhone X具有相同的屏幕尺寸:1125 x 2436。因此,在这种情况下使用 [[UIScreen mainScreen] bounds].size 是行不通的。
有没有其他方法来检测最新的iPhone版本?
编辑: 这不是问题“如何检测iPhone X”的重复,因为所有讨论的答案/解决方案都使用屏幕尺寸来指定设备类型。正如上面所述,iPhone X和iPhone XS具有完全相同的尺寸,因此这些解决方案无法区分这两个设备...

通常,您喜欢使用UIDevice:https://dev59.com/questions/g18e5IYBdhLWcg3wJnyl,对吧?但我还不知道返回的值是什么。 - Larme
来自链接的重复问题的许多答案将帮助您区分任何设备。 - Cœur
请查看以下链接:https://dev59.com/h1YO5IYBdhLWcg3wMO1y#52821290 - ale_stro
3个回答

22

您可以使用UIScreen.main.nativeBounds来确定。

 if UIDevice().userInterfaceIdiom == .phone {
        switch UIScreen.main.nativeBounds.height {
        case 1136:
            print("IPHONE 5,5S,5C")
        case 1334:
            print("IPHONE 6,7,8 IPHONE 6S,7S,8S ")
        case 1920, 2208:
            print("IPHONE 6PLUS, 6SPLUS, 7PLUS, 8PLUS")
        case 2436:
            print("IPHONE X, IPHONE XS, IPHONE 11 PRO")
        case 2688:
            print("IPHONE XS MAX, IPHONE 11 PRO MAX")
        case 1792:
            print("IPHONE XR, IPHONE 11")
        default:
            print("UNDETERMINED")
        }
    }

.nativeBounds不会因为屏幕方向的改变而改变。 https://developer.apple.com/documentation/uikit/uiscreen/1617810-nativebounds


XR比XS大,你确定XR的高度是1792吗? - Coder221
1
是的,你说得对,但这就是nativeBounds方法返回的XR值。 - Umair
这个在横屏模式下能用吗? - M Afham
是的,它也适用于横屏模式。 - Umair

10

我使用DeviceUtil来确定iOS设备的型号。

根据这篇DeviceUtil GitHub帖子,返回的UIDevice硬件字符串值为:

iPhone11,2 = iPhone XS
iPhone11,4 = iPhone XS Max
iPhone11,8 = iPhone XR

有趣的是,Xcode 10 GM模拟器的 [UIScreen mainScreen].bounds.size 在iPhone X、XS、XS Max和R设备上返回375 x 812。我原以为XS Max和XR会返回414 x 896。


我发现模拟器报告这些尺寸是因为我的应用程序针对iOS 10或更高版本。要启用全屏分辨率,我的应用程序必须针对iOS 11或更高版本。 - lifjoy
您的应用程序必须针对iOS 10或更高版本,并且必须使用启动屏幕文件而不是启动图像源。 - cmii
1
你关于启动图的说法是正确的。但我认为需要iOS 11。在developer.apple.com/iphone上,有一段话说:“如果您的项目基本SDK设置为iOS 11或更高版本,并且您有一个启动故事板或iPhone X启动图像,则您的应用程序将在iPhone X、iPhone XS、iPhone XS Max和iPhone XR上以全屏显示模式运行”。我的应用程序针对iOS 10,并且具有适当的XS Max启动图像,但仍然在XS Max上缩放。 - lifjoy
我正在使用最新的Xcode,iOS 10目标, 开启全屏演示模式,[UIScreen mainScreen].bounds.size 可以返回所有设备正确的大小。 - cmii
谢谢,@cmii。你说得太对了。事实证明我搞砸了新的UILaunchImages字典,以支持{414, 896},LaunchImage-iPhone6_5-Port。模拟器现在返回了预期的屏幕尺寸。 - lifjoy

3

#import <sys/utsname.h> // 在您的头文件或实现文件中导入它。

+(NSString*)deviceName
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *machineName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];


    NSDictionary *commonNamesDictionary =
    @{
      @"i386":     @"i386 Simulator",
      @"x86_64":   @"x86_64 Simulator",

      @"iPhone1,1":    @"iPhone",
      @"iPhone1,2":    @"iPhone 3G",
      @"iPhone2,1":    @"iPhone 3GS",
      @"iPhone3,1":    @"iPhone 4",
      @"iPhone3,2":    @"iPhone 4",
      @"iPhone3,3":    @"iPhone 4",
      @"iPhone4,1":    @"iPhone 4S",
      @"iPhone5,1":    @"iPhone 5",
      @"iPhone5,2":    @"iPhone 5",
      @"iPhone5,3":    @"iPhone 5c",
      @"iPhone5,4":    @"iPhone 5c",
      @"iPhone6,1":    @"iPhone 5s",
      @"iPhone6,2":    @"iPhone 5s",

      @"iPhone7,1":    @"iPhone 6+",
      @"iPhone7,2":    @"iPhone 6",

      @"iPhone8,1":    @"iPhone 6S",
      @"iPhone8,2":    @"iPhone 6S+",
      @"iPhone8,4":    @"iPhone SE",
      @"iPhone9,1":    @"iPhone 7",
      @"iPhone9,2":    @"iPhone 7+",
      @"iPhone9,3":    @"iPhone 7",
      @"iPhone9,4":    @"iPhone 7+",
      @"iPhone10,1":    @"iPhone 8",
      @"iPhone10,2":    @"iPhone 8+",

      @"iPhone10,3":    @"iPhone X",
      @"iPhone11,2":    @"iPhone XS",
      @"iPhone11,4":    @"iPhone XS Max",
      @"iPhone11,8":    @"iPhone XR",

      @"iPad1,1":  @"iPad",
      @"iPad2,1":  @"iPad 2",
      @"iPad2,2":  @"iPad 2",
      @"iPad2,3":  @"iPad 2",
      @"iPad2,4":  @"iPad 2",
      @"iPad2,5":  @"iPad Mini 1G",
      @"iPad2,6":  @"iPad Mini 1G",
      @"iPad2,7":  @"iPad Mini 1G",
      @"iPad3,1":  @"iPad 3",
      @"iPad3,2":  @"iPad 3",
      @"iPad3,3":  @"iPad 3",
      @"iPad3,4":  @"iPad 4",
      @"iPad3,5":  @"iPad 4",
      @"iPad3,6":  @"iPad 4",

      @"iPad4,1":  @"iPad Air",
      @"iPad4,2":  @"iPad Air",
      @"iPad4,3":  @"iPad Air",

      @"iPad5,3":  @"iPad Air 2",
      @"iPad5,4":  @"iPad Air 2",

      @"iPad4,4":  @"iPad Mini 2G",
      @"iPad4,5":  @"iPad Mini 2G",
      @"iPad4,6":  @"iPad Mini 2G",

      @"iPad4,7":  @"iPad Mini 3G",
      @"iPad4,8":  @"iPad Mini 3G",
      @"iPad4,9":  @"iPad Mini 3G",

      @"iPod1,1":  @"iPod 1st Gen",
      @"iPod2,1":  @"iPod 2nd Gen",
      @"iPod3,1":  @"iPod 3rd Gen",
      @"iPod4,1":  @"iPod 4th Gen",
      @"iPod5,1":  @"iPod 5th Gen",
      @"iPod7,1":  @"iPod 6th Gen",
      };

    NSString *deviceName = commonNamesDictionary[machineName];

    if (deviceName == nil) {
        deviceName = machineName;
    }

    return deviceName;
}

它会为您返回设备型号,包括XS、XR和XS Max。
    #define IS_IPHONE        (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_IPHONE_4      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0)
    #define IS_IPHONE_5      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
    #define IS_IPHONE_6      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
    #define IS_IPHONE_6PLUS  (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
    #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
    #define IS_IPHONE_X      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)
#define IS_IPHONE_X      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)

    #define IS_IPHONE_XS      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)
    #define IS_IPHONE_X_MAX      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 896.0)
    #define IS_RETINA        ([[UIScreen mainScreen] scale] >= 2.0) // 3.0 for iPhone X, 2.0 for others

    #define IS_IPAD_DEVICE   [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"]

注意:请小心,此功能仅在竖屏方向下正常工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接