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 具有完全相同的尺寸,因此解决方案无法区分这两者......

4

3 回答 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

于 2018-09-20T20:19:46.783 回答
10

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

根据此DeviceUtil GitHub 帖子,返回的 UIDevice hardwareString 值为:

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。

于 2018-09-13T18:50:22.397 回答
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"]

注意:- 小心,它仅适用于纵向

于 2018-09-20T13:41:00.487 回答