使用 iOS 确定设备(iPhone、iPod Touch)

Determine device (iPhone, iPod Touch) with iOS

提问人:Rob 提问时间:1/16/2009 最后编辑:CœurRob 更新时间:2/18/2023 访问量:198402

问:

有没有办法确定运行应用程序的设备。如果可能的话,我想区分 和 。iPhoneiPod Touch

iOS Objective-C iPhone iPod-Touch

评论

1赞 12/3/2009
UIDeviceHardware 类是否使用未记录且被禁止的库?
4赞 Lawrence 12/10/2009
不,它们被记录在案。developer.apple.com/iphone/library/documentation/System/......
0赞 clearlight 1/2/2017
包含此答案的线程中有一些非常好的解决方案
0赞 bitops 12/3/2021
这个问题在 12 年前就被问过了,现在仍在更新中。UIKit 开发团队和产品人员 - 也许这表明您应该添加一个 API?

答:

287赞 Adam Rosenfield 1/16/2009 #1

您可以像这样使用 UIDevice 类:

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])
    // it's an iPhone

评论

4赞 Adam Rosenfield 9/15/2010
@jeeva:该链接中的代码是对通用应用程序(分别为 iPhone 和 iPad 编译的应用程序)的编译时检查。此代码是运行时检查。还要记住,这个问题和答案早在iPad出现之前就已经写好了。
0赞 9/16/2010
@Adam我同意你的看法..你很久以前就写了一个答案,这是很好的工作......我告诉过,您可以使用UIUserInterfaceIdiomPad,而不是使用模型,它仅用于此目的......
0赞 Anil Sivadas 10/29/2010
@jeeva,我认为 3.0 操作系统不支持
1赞 snibbe 6/19/2011
我是否可以建议取消将其标记为最佳答案,并将下面的答案标记为最佳答案,并带有完整的 UIDeviceHardware 类代码,请问?
1赞 mklement0 7/9/2012
@AnilSivadas:在仍然支持 iOS < 3.2 的应用程序中可以安全使用:它是一个宏,专门编写为在此类较旧的 iOS 版本上默认使用。UI_USER_INTERFACE_IDIOM()UIDevice.hUIUserInterfaceIdiomPhone
122赞 Dutchie432 10/14/2009 #2

请随意使用这个类(gist @ github)

代码已删除并重新定位到

https://gist.github.com/1323251

更新 (01/14/11)

显然,这段代码现在已经过时了,但它肯定可以使用此线程上提供的代码进行更新,其中包括具有更新模型的类似代码。感谢您对此线程的支持。Brian Robbins

评论

3赞 winsmith 1/14/2011
@Dave:代码不会中断,它只是返回一个稍微不那么漂亮的字符串。你有更好的方法来获取设备模型吗?通过型号,我指的不仅仅是“iPhone”或“iPod”,而是特定版本。
7赞 Ben G 3/3/2011
我不明白的一件事是,为什么你不把它们定义为类方法,因为你没有维护任何状态。只需将 - (NSString *) 替换为 + (NSString *),即可在使用它时为自己节省一个分配/释放。
1赞 Goles 9/19/2011
此行:NSString *platform = [NSString stringWithCString:machine];,应为 NSString *platform = [NSString stringWithUTF8String:machine];因为 stringWithCString 已弃用。
1赞 DougW 10/16/2011
@Oliver - 已添加。它是“iPhone4,1”
1赞 Goles 10/22/2011
我的建议是将这段非常简单的代码作为 github 中的要点添加,或者作为一个非常小的开源项目。由于 Stack Overflow 不适合这种开发;)
10赞 snibbe 7/24/2010 #3

以下是新模型的次要更新:

- (NSString *) platformString{
    NSString *platform = [self platform];
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPod1,1"])   return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])   return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])   return @"iPod Touch 3G";
    if ([platform isEqualToString:@"i386"])   return @"iPhone Simulator";
    return platform;
}
279赞 18 revs, 12 users 33%Brian Robbins #4

这是对上述答案的更新。UIDeviceHardware.m

- (NSString *)platformString
{
    NSString *platform = [self platform];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPod7,1"])      return @"iPod Touch 6G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad mini 4 (WiFi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad mini 4 (Cellular)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7 inch (Cellular)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9 inch (Cellular)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}

评论

5赞 jakev 2/13/2012
对不起,这个函数不会因为第一行而导致无限循环吗?谁是这里的“自我”?
0赞 Matthias Bauch 2/14/2012
是的,我回滚了该更改
4赞 Stunner 3/26/2012
为什么叫iPad-3G?这是新iPad的正式名称吗?我认为 iPad 4G 或 iPad 3 更有意义。
4赞 Fritz Anderson 2/1/2014
级联比较产生的名称是 。定义一个没有代码依赖关系的名称是 。NSStringNSStringNSDictionary.plist
6赞 jeremywhuff 11/5/2014
仅供参考 - 似乎此处保留了更新的列表 - theiphonewiki.com/wiki/Models
12赞 UIBuilder 10/18/2010 #5

更实用

#include <sys/types.h>
#include <sys/sysctl.h>

@interface UIDevice(Hardware)

- (NSString *) platform;

- (BOOL)hasRetinaDisplay;

- (BOOL)hasMultitasking;

- (BOOL)hasCamera;

@end

@implementation UIDevice(Hardware)

- (NSString *) platform{
    int mib[2];
size_t len;
char *machine;

mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);

    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
return platform;
}

- (BOOL)hasRetinaDisplay {
    NSString *platform = [self platform];
    BOOL ret = YES;
    if ([platform isEqualToString:@"iPhone1,1"]) {
        ret = NO;
    }
    else
        if ([platform isEqualToString:@"iPhone1,2"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPhone2,1"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPod1,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod2,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod3,1"])      ret = NO;
    return ret;
}

- (BOOL)hasMultitasking {
    if ([self respondsToSelector:@selector(isMultitaskingSupported)]) {
        return [self isMultitaskingSupported];
    }
    return NO;
}

- (BOOL)hasCamera {
   BOOL ret = NO;
   // check camera availability
   return ret;
}

@end

您可以使用以下命令读取属性

NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO");

评论

4赞 Todd Lehman 6/1/2012
您可以简单地检查是否为 2(适用于 iOS 5+),而不是复杂的 if/else 树。[[UIScreen mainScreen] scale]
0赞 Jonny 3/13/2013
不包括所需的“hasVibration”功能。
1赞 Di Wu 3/20/2011 #6

Dutchie432并提供了很好的解决方案。但仍然缺少一个型号,即Verizon iPhone 4。这是缺失的一行。Brian Robbins

if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4"; //Verizon

评论

1赞 makdad 5/27/2011
有趣的是,现在似乎有iPhone3,3?
0赞 Di Wu 6/1/2011
@makdad是的。实际上,一些Verizon iPhone 4竟然是3,3。
1赞 stoutyhk 10/12/2011
有人知道iPhone 4S的标识符吗?
8赞 Jason Fuerstenberg 11/6/2011 #7

只需将iPhone 4S设备代码添加到此线程中...

iPhone 4S 将返回字符串 @“iPhone4,1”。

评论

0赞 Besi 1/4/2012
我已将其添加到最“升级”的帖子中
6赞 Rodrigo 12/13/2011 #8

这个代码怎么样,如果发布了新版本,您将使用最后知道的设备进行标识符

- (NSString *)getModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);                              
    if ([sDeviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([sDeviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([sDeviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([sDeviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([sDeviceModel isEqual:@"iPhone3,1"]) return @"iPhone3GS";  //iPhone 4 - AT&T
    if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone3GS";  //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([sDeviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([sDeviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([sDeviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([sDeviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([sDeviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([sDeviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([sDeviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([sDeviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([sDeviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[sDeviceModel componentsSeparatedByString:@","] objectAtIndex:0];

//If a newer version exist
    if ([aux rangeOfString:@"iPhone"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4"
        if (version >= 4) return @"iPhone4s";

    }
    if ([aux rangeOfString:@"iPod"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version >=4) return @"iPod4thGen";
    }
    if ([aux rangeOfString:@"iPad"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version ==1) return @"iPad3G";
        if (version >=2) return @"iPad2";
    }
    //If none was found, send the original string
    return sDeviceModel;
}

评论

1赞 Oliver 12/13/2011
我不喜欢你的所作所为。它返回未知设备的错误引用。好主意,但实施不好。
0赞 Rodrigo 12/14/2011
@Oliver 你有什么建议?你必须想象一下,如果发布了一个新设备,你不能更改代码,对我来说,这会导致一个错误。
1赞 joe 10/19/2012
如何识别iPhone 5 / iPod 5?@“iPhone5,1” @“iPod5,1” 是正确的使用方式吗?
5赞 benvolioT 1/13/2012 #9

根据上面非常好的答案,这是我想出的。这与@Rodrigo的回答非常相似,但解决了@Oliver对该答案的评论的担忧。这还提供了在输出字符串中包含模型字符串的选项。

+ (NSString *) deviceModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);      

    return deviceModel;
}

+ (NSString *) deviceName {
    NSString *deviceModel = [DeviceGateway deviceModel];                    

    if ([deviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([deviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([deviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([deviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([deviceModel isEqual:@"iPhone3,1"]) return @"iPhone4";    //iPhone 4 - AT&T
    if ([deviceModel isEqual:@"iPhone3,2"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([deviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([deviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([deviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([deviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([deviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([deviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([deviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([deviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([deviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([deviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([deviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[deviceModel componentsSeparatedByString:@","] objectAtIndex:0];

    //If a newer version exists
    if ([aux rangeOfString:@"iPhone"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4";
        if (version == 4) return @"iPhone4s";
        return @"Newer iPhone";
    }
    if ([aux rangeOfString:@"iPod"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version == 4) return @"iPod4thGen";
        return @"Newer iPod";
    }
    if ([aux rangeOfString:@"iPad"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version == 1) return @"iPad3G";
        if (version == 2) return @"iPad2";
        return @"Newer iPad";
    }

    //If none was found, send the original string
    return deviceModel;
}

+ (NSString *) deviceNameWithDeviceModel:(BOOL)shouldIncludeDeviceModel {
    if (shouldIncludeDeviceModel) {
        return [NSString stringWithFormat:@"%@ (%@)", [DeviceGateway deviceName], [DeviceGateway deviceModel]];
    }

    return [DeviceGateway deviceName];
}

评论

0赞 fvisticot 1/18/2012
我们可以将它用于 AppStore 应用程序吗?此代码是否使用私有 API (sysctlbyname) ?Tx 为您提供帮助
1赞 benvolioT 1/22/2012
此处不使用私有 API。这已通过 XCode 中的自动验证。
0赞 Nizzy 12/11/2012
你从哪里获得 DeviceGateway?
0赞 benvolioT 12/13/2012
DeviceGateway 是显示其静态方法的类的名称。因此,您询问的行只是在引用的代码中调用了另一个静态方法。随意命名任何您喜欢的类。
27赞 Gurpreet Singh 6/19/2012 #10
if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) {
    //Device is ipad 
}else{
    //Device is iphone
}

评论

6赞 stephenmuss 7/5/2012
如果你看一下Rob提到的问题,“如果可能的话,我想区分iPhone和iPod Touch”。您的解决方案无法解决此问题。
37赞 Tomasz 12/3/2012 #11

我在我的应用程序中使用它。截至2012年12月。

- (NSString *) platformString {
    // Gets a string with the device model
    size_t size;  
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);  
    char *machine = malloc(size);  
    sysctlbyname("hw.machine", machine, &size, NULL, 0);  
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];  
    free(machine); 

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";    
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";

    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch (1 Gen)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch (2 Gen)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch (3 Gen)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4 Gen)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";

    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";

    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}  

评论

8赞 Ans 5/30/2013
它在 sysctlbyname(“hw.machine”, NULL, &size, NULL, 0) 行抛出警告,指出“函数'sysctlbyname'的隐式声明在 C99 中无效”;
0赞 Brabbeldas 3/12/2015
要解决“函数的隐式声明”。' 警告 您需要添加以下内容:#import <sys/sysctl.h>
2赞 Arash Zeinoddini 12/21/2012 #12
- (BOOL)deviceiPhoneOriPod
  {
    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound)
      return YES;
    else
      return NO;
  }
6赞 ICL1901 1/13/2013 #13

添加到 Arash 的代码中,我不关心我的应用程序我正在使用什么型号,我只想知道什么样的设备,因此,我可以按如下方式进行测试:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            NSLog(@"I'm definitely an iPad");
    } else {
    NSString *deviceType = [UIDevice currentDevice].model;
                if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound)
                {
                    NSLog(@"I must be an iPhone");

                } else {
                    NSLog(@"I think I'm an iPod");

                }
}
1赞 QED 4/22/2013 #14

我喜欢 Erica Sadun 的东西。她包括AppleTV设备和其他你可能没有想到的设备。

https://github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.h

4赞 Bhavesh Nayi 5/22/2013 #15
NSString *deviceType = [UIDevice currentDevice].model;
3赞 Dan Loughney 9/18/2013 #16

我更进一步,将大的“isEqualToString”块转换为设备类型、代次和逗号后的其他限定符的位掩码分类(我称之为子代)。它被包装在一个具有单例调用 SGPlatform 的类中,这避免了大量重复的字符串操作。代码可在 https://github.com/danloughney/spookyGroup

该类允许您执行如下操作:

if ([SGPlatform iPad] && [SGPlatform generation] > 3) {
    // set for high performance
}

switch ([SGPlatform deviceMask]) {
case DEVICE_IPHONE:
    break;
case DEVICE_IPAD:
    break;
case DEVICE_IPAD_MINI:
    break;
}

设备的分类采用 platformBits 方法。该线程的读者应该非常熟悉该方法。我按设备类型和世代对设备进行了分类,因为我最感兴趣的是整体性能,但可以调整来源以提供您感兴趣的任何分类、视网膜屏幕、网络功能等。

0赞 Ortwin Gentz 10/12/2013 #17

我想补充一点,为了检索设备的正面和外壳颜色,有一个私有 API:

UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString([device.systemVersion hasPrefix:@"7"] ? @"_deviceInfoForKey:" :  @"deviceInfoForKey:");
if ([device respondsToSelector:selector]) {
    NSLog(@"DeviceColor: %@ DeviceEnclosureColor: %@", [device performSelector:selector withObject:@"DeviceColor"], [device performSelector:selector withObject:@"DeviceEnclosureColor"]);
}

我已经写了一篇关于此的博客,并提供了一个示例应用程序:

http://www.futuretap.com/blog/device-colors/

3赞 Jayprakash Dubey #18

下面提到的代码片段应该会有所帮助:

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   // iPhone device
 }
 else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
   // iPad device
 }
 else {
  // Other device i.e. iPod
 }
0赞 GJDK 11/14/2014 #19
NSString *deviceType = [[UIDevice currentDevice] systemName];

我可以保证上述建议将在 iOS 7 及更高版本中运行。我相信它也可以在 iOS 6 中使用。但我不确定。

14赞 Gerard G 12/24/2014 #20

更新了 和 的平台字符串:iPad Air 2iPad mini 3

- (NSString *)platformString
{
    NSString *platform = [self platform];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";

    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";

    if ([platform isEqualToString:@"iPad4,7"])      return @"iPad mini 3 (WiFi)";
    if ([platform isEqualToString:@"iPad4,8"])      return @"iPad mini 3 (Cellular)";
    if ([platform isEqualToString:@"iPad4,9"])      return @"iPad mini 3 (China Model)";

    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro";

    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}
0赞 Maxim Pavlov 1/19/2015 #21

您可以在 GitHub 上查看 GBDeviceInfo,也可以通过 CocoaPods 获得。它提供了简单的 API,用于检测各种属性,并支持所有最新设备:

  • 设备系列

[GBDeviceInfo deviceDetails].family == GBDeviceFamilyiPhone;

  • 设备型号

[GBDeviceInfo deviceDetails].model == GBDeviceModeliPhone6.

有关详细信息,请参阅自述文件

1赞 Inder Kumar Rathore 4/13/2015 #22

可能的

[[UIDevice currentDevice] model];

iPod touchiPhoneiPhone SimulatoriPadiPad Simulator

如果您想知道哪个硬件正在损坏,例如,等,下面是该代码iOSiPhone3iPhone4iPhone5


注意:下面的代码可能不包含所有设备的字符串,我和其他人在 GitHub 上维护相同的代码,所以请从那里获取最新代码

Objective-CGitHub/DeviceUtil

斯威夫特GitHub/DeviceGuru


#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString*)hardwareDescription {
    NSString *hardware = [self hardwareString];
    if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G";
    if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G";
    if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
    if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI";
    //there are lots of other strings too, checkout the github repo
    //link is given at the top of this answer

    if ([hardware isEqualToString:@"i386"]) return @"Simulator";
    if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";

    return nil;
}

- (NSString*)hardwareString {
    size_t size = 100;
    char *hw_machine = malloc(size);
    int name[] = {CTL_HW,HW_MACHINE};
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    free(hw_machine);
    return hardware;
}
4赞 MD SHAHIDUL ISLAM 4/25/2015 #23
#import <sys/utsname.h> 

NSString *iPhone = [self iPhoneVersion]; 

-(NSString*)iPhoneVersion
{
   NSString *platform;
   struct utsname systemInfo;
   uname(&systemInfo);
   platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

   if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
   if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
   if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
   if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
   if ([platform isEqualToString:@"iPhone3,3"])    return @"VerizoniPhone 4";
   if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
   if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
   if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
   if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
   if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
   if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
   if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
   if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
   if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
   if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
   if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
   if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
   if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
   if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
   if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
   if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
   if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
   if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
   if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
   if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
   if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
   if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
   if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
   if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
   if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air";
   if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini 2G (WiFi)";
   if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini 2G (Cellular)";
   if ([platform isEqualToString:@"iPad4,6"])      return @"iPad Mini 2G";
   if ([platform isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (WiFi)";
   if ([platform isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
   if ([platform isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
   if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
   if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
   if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
   if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3";
   if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3 (2013)";
   if ([platform isEqualToString:@"i386"])         return @"Simulator";
   if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
   return platform;
}
10赞 user4657588 6/5/2015 #24

我知道已经勾选了答案,但为了将来参考,您可以随时使用设备屏幕尺寸来确定它是哪个设备:

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

    CGSize result = [[UIScreen mainScreen] bounds].size;

    if (result.height == 480) {
        // 3.5 inch display - iPhone 4S and below
        NSLog(@"Device is an iPhone 4S or below");
    }

    else if (result.height == 568) {
        // 4 inch display - iPhone 5
        NSLog(@"Device is an iPhone 5/S/C");
    }

    else if (result.height == 667) {
        // 4.7 inch display - iPhone 6
        NSLog(@"Device is an iPhone 6");
    }

    else if (result.height == 736) {
        // 5.5 inch display - iPhone 6 Plus
        NSLog(@"Device is an iPhone 6 Plus");
    }
} 

else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
       // iPad 9.7 or 7.9 inch display.
       NSLog(@"Device is an iPad.");
}
5赞 Sebyddd 9/7/2015 #25

有一个非常成熟的库(由我负责),称为 SDVersion。 您可以检查正在运行的设备型号、屏幕尺寸和许多其他参数。它还支持 OSX。

例:

      // Check for device model
      if ([SDVersion deviceVersion] == iPhone6)
           NSLog(@"You got the iPhone 6. Sweet 🍭!");
      else if ([SDVersion deviceVersion] == iPhone6Plus)
           NSLog(@"iPhone 6 Plus? Bigger is better!");
      else if ([SDVersion deviceVersion] == iPadAir2)
           NSLog(@"You own an iPad Air 2 🌀!");

      // Check for device screen size
      if ([SDVersion deviceSize] == Screen4inch)
           NSLog(@"Your screen is 4 inches");

评论

0赞 11/5/2015
如何在 Swift 中使用?
34赞 D1mers0n 4/19/2016 #26

最新更新 - 2023 年 2 月:新增 iPhone 14、iPhone 14 Plus、iPhone 14 Pro、iPhone 14 Pro Max、iPad(第 10 代)、iPad Pro 11 英寸(第 4 代)、 iPad Pro 12,9 英寸(第 6 代)、Apple Watch Series 8、Apple Watch Ultra、Apple Watch SE(第 2 代)。

已删除不支持 iOS 10 的设备


迅速

(在 Swift 5、Swift 4、Swift 3 上测试)

下面的函数返回一个包含当前设备名称的字符串

func userDeviceName() -> String {
    let platform: String = {
        var size = 0
        sysctlbyname("hw.machine", nil, &size, nil, 0)
        var machine = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname("hw.machine", &machine, &size, nil, 0)
        return String(cString: machine)
    }()

    let deviceMap = [

        // iPhone
        "iPhone5,1": "iPhone 5 (GSM)",
        "iPhone5,2": "iPhone 5 (GSM+CDMA)",
        "iPhone5,3": "iPhone 5c (GSM)",
        "iPhone5,4": "iPhone 5c (GSM+CDMA)",
        "iPhone6,1": "iPhone 5s (GSM)",
        "iPhone6,2": "iPhone 5s (GSM+CDMA)",
        "iPhone7,2": "iPhone 6",
        "iPhone7,1": "iPhone 6 Plus",
        "iPhone8,1": "iPhone 6s",
        "iPhone8,2": "iPhone 6s Plus",
        "iPhone8,4": "iPhone SE",
        "iPhone9,1": "iPhone 7 (GSM+CDMA)",
        "iPhone9,3": "iPhone 7 (GSM)",
        "iPhone9,2": "iPhone 7 Plus (GSM+CDMA)",
        "iPhone9,4": "iPhone 7 Plus (GSM)",
        "iPhone10,1": "iPhone 8 (GSM+CDMA)",
        "iPhone10,4": "iPhone 8 (GSM)",
        "iPhone10,2": "iPhone 8 Plus (GSM+CDMA)",
        "iPhone10,5": "iPhone 8 Plus (GSM)",
        "iPhone10,3": "iPhone X (GSM+CDMA)",
        "iPhone10,6": "iPhone X (GSM)",
        "iPhone11,2": "iPhone XS",
        "iPhone11,6": "iPhone XS Max",
        "iPhone11,8": "iPhone XR",
        "iPhone12,1": "iPhone 11",
        "iPhone12,3": "iPhone 11 Pro",
        "iPhone12,5": "iPhone 11 Pro Max",
        "iPhone12,8": "iPhone SE (2nd generation)",
        "iPhone13,1": "iPhone 12 mini",
        "iPhone13,2": "iPhone 12",
        "iPhone13,3": "iPhone 12 Pro",
        "iPhone13,4": "iPhone 12 Pro Max",
        "iPhone14,4": "iPhone 13 mini",
        "iPhone14,5": "iPhone 13",
        "iPhone14,2": "iPhone 13 Pro",
        "iPhone14,3": "iPhone 13 Pro Max",
        "iPhone14,6": "iPhone SE (3rd generation) (2022)",
        "iPhone14,7": "iPhone 14",
        "iPhone14,8": "iPhone 14 Plus",
        "iPhone15,2": "iPhone 14 Pro",
        "iPhone15,3": "iPhone 14 Pro Max",

        // iPod touch
        "iPod7,1": "iPod Touch (6th generation)",
        "iPod9,1": "iPod Touch (7th generation) (2019)",

        // iPad
        "iPad3,4": "iPad (4th generation) (Wi-Fi)",
        "iPad3,5": "iPad (4th generation) (GSM)",
        "iPad3,6": "iPad (4th generation) (GSM+CDMA)",
        "iPad6,11": "iPad (5th generation) 9.7\" (2017) (Wi-Fi)",
        "iPad6,12": "iPad (5th generation) 9.7\" (2017) (Cellular)",
        "iPad7,5": "iPad (6th generation) 9.7\" (2018) (Wi-Fi)",
        "iPad7,6": "iPad (6th generation) 9.7\" (2018) (Cellular)",
        "iPad7,11": "iPad (7th generation) 10.2\" (2019) (Wi-Fi)",
        "iPad7,12": "iPad (7th generation) 10.2\" (2019) (Cellular)",
        "iPad11,6": "iPad (8th generation) 10.2\" (2020) (Wi-Fi)",
        "iPad11,7": "iPad (8th generation) 10.2\" (2020) (Cellular)",
        "iPad12,1": "iPad (9th generation) 10.2\" (2021) (Wi-Fi)",
        "iPad12,2": "iPad (9th generation) 10.2\" (2021) (Cellular)",
        "iPad13,18": "iPad (10th generation) 10.9\" (2022) (Wi-Fi)",
        "iPad13,19": "iPad (10th generation) 10.9\" (2022) (Cellular)",
        
        // iPad Air
        "iPad4,1": "iPad Air (Wi-Fi)",
        "iPad4,2": "iPad Air (Cellular)",
        "iPad4,3": "iPad Air (China)",
        "iPad5,3": "iPad Air 2 (Wi-Fi)",
        "iPad5,4": "iPad Air 2 (Cellular)",
        "iPad11,3": "iPad Air (3rd generation) (2019) (Wi-Fi)",
        "iPad11,4": "iPad Air (3rd generation) (2019) (Cellular)",
        "iPad13,1": "iPad Air (4th generation) (2020) (Wi-Fi)",
        "iPad13,2": "iPad Air (4th generation) (2020) (Cellular)",
        "iPad13,16": "iPad Air (5th generation) (2022) (Wi-Fi)",
        "iPad13,17": "iPad Air (5th generation) (2022) (Cellular)",
        
        // iPad Pro
        "iPad6,3": "iPad Pro 9.7\" (Wi-Fi)",
        "iPad6,4": "iPad Pro 9.7\" (Cellular)",
        "iPad6,7": "iPad Pro 12.9\" (Wi-Fi)",
        "iPad6,8": "iPad Pro 12.9\" (Cellular)",
        "iPad7,1": "iPad Pro 12.9\" (2nd generation) (Wi-Fi)",
        "iPad7,2": "iPad Pro 12.9\" (2nd generation) (Cellular)",
        "iPad7,3": "iPad Pro 10.5\" (Wi-Fi)",
        "iPad7,4": "iPad Pro 10.5\" (Cellular)",
        "iPad8,1": "iPad Pro 11\" (2018) (Wi-Fi)",
        "iPad8,2": "iPad Pro 11\" (2018) (Wi-Fi, 1TB)",
        "iPad8,3": "iPad Pro 11\" (2018) (Cellular)",
        "iPad8,4": "iPad Pro 11\" (2018) (Cellular 1TB)",
        "iPad8,5": "iPad Pro 12.9\" (3rd generation) (2018) (Wi-Fi)",
        "iPad8,6": "iPad Pro 12.9\" (3rd generation) (2018) (Wi-Fi, 1TB)",
        "iPad8,7": "iPad Pro 12.9\" (3rd generation) (2018) (Cellular)",
        "iPad8,8": "iPad Pro 12.9\" (3rd generation) (2018) (Cellular, 1TB)",
        "iPad8,9": "iPad Pro 11\" (2nd generation) (2020) (Wi-Fi)",
        "iPad8,10": "iPad Pro 11\" (2nd generation) (2020) (Cellular)",
        "iPad8,11": "iPad Pro 12.9\" (4th generation) (2020) (Wi-Fi)",
        "iPad8,12": "iPad Pro 12.9\" (4th generation) (2020) (Cellular)",
        "iPad13,4": "iPad Pro 11\" (3nd generation) (2021) (Wi-Fi)",
        "iPad13,5": "iPad Pro 11\" (3nd generation) (2021) (Cellular US)",
        "iPad13,6": "iPad Pro 11\" (3nd generation) (2021) (Cellular Global)",
        "iPad13,7": "iPad Pro 11\" (3nd generation) (2021) (Cellular China)",
        "iPad13,8": "iPad Pro 12.9\" (5th generation) (2021) (Wi-Fi)",
        "iPad13,9": "iPad Pro 12.9\" (5th generation) (2021) (Cellular US)",
        "iPad13,10": "iPad Pro 12.9\" (5th generation) (2021) (Cellular Global)",
        "iPad13,11": "iPad Pro 12.9\" (5th generation) (2021) (Cellular China)",
        "iPad14,3": "iPad Pro 11\" (4th generation) (2022) (Wi-Fi)",
        "iPad14,4": "iPad Pro 11\" (4th generation) (2022) (Cellular)",
        "iPad14,5": "iPad Pro 12.9\" (6th generation) (2022) (Wi-Fi)",
        "iPad14,6": "iPad Pro 12.9\" (6th generation) (2022) (Cellular)",
        
        // iPad mini
        "iPad4,4": "iPad mini 2 (Wi-Fi)",
        "iPad4,5": "iPad mini 2 (Cellular)",
        "iPad4,6": "iPad mini 2 (China)",
        "iPad4,7": "iPad mini 3 (Wi-Fi)",
        "iPad4,8": "iPad mini 3 (Cellular)",
        "iPad4,9": "iPad mini 3 (China)",
        "iPad5,1": "iPad mini 4 (Wi-Fi)",
        "iPad5,2": "iPad mini 4 (Cellular)",
        "iPad11,1": "iPad mini (5th generation) (2019) (Wi-Fi)",
        "iPad11,2": "iPad mini (5th generation) (2019) (Cellular)",
        "iPad14,1": "iPad mini (6th generation) (2021) (Wi-Fi)",
        "iPad14,2": "iPad mini (6th generation) (2021) (Cellular)",

        // Apple TV
        "AppleTV2,1": "Apple TV 2G",
        "AppleTV3,1": "Apple TV 3",
        "AppleTV3,2": "Apple TV 3 (2013)",
        "AppleTV5,3": "Apple TV 4",
        "AppleTV6,2": "Apple TV 4K",
        "AppleTV11,1": "Apple TV 4K (2nd generation)",

        // Apple Watch
        "Watch1,1": "Apple Watch (1st generation) (38mm)",
        "Watch1,2": "Apple Watch (1st generation) (42mm)",
        "Watch2,6": "Apple Watch Series 1 (38mm)",
        "Watch2,7": "Apple Watch Series 1 (42mm)",
        "Watch2,3": "Apple Watch Series 2 (38mm)",
        "Watch2,4": "Apple Watch Series 2 (42mm)",
        "Watch3,1": "Apple Watch Series 3 (38mm Cellular)",
        "Watch3,2": "Apple Watch Series 3 (42mm Cellular)",
        "Watch3,3": "Apple Watch Series 3 (38mm)",
        "Watch3,4": "Apple Watch Series 3 (42mm)",
        "Watch4,1": "Apple Watch Series 4 (40mm)",
        "Watch4,2": "Apple Watch Series 4 (44mm)",
        "Watch4,3": "Apple Watch Series 4 (40mm Cellular)",
        "Watch4,4": "Apple Watch Series 4 (44mm Cellular)",
        "Watch5,1": "Apple Watch Series 5 (40mm)",
        "Watch5,2": "Apple Watch Series 5 (44mm)",
        "Watch5,3": "Apple Watch Series 5 (40mm Cellular)",
        "Watch5,4": "Apple Watch Series 5 (44mm Cellular)",
        "Watch6,1": "Apple Watch Series 6 (40mm)",
        "Watch6,2": "Apple Watch Series 6 (44mm)",
        "Watch6,3": "Apple Watch Series 6 (40mm Cellular)",
        "Watch6,4": "Apple Watch Series 6 (44mm Cellular)",
        "Watch5,9": "Apple Watch SE (40mm)",
        "Watch5,10": "Apple Watch SE (44mm)",
        "Watch5,11": "Apple Watch SE (40mm Cellular)",
        "Watch5,12": "Apple Watch SE (44mm Cellular)",
        "Watch6,6": "Apple Watch Series 7 (41mm)",
        "Watch6,7": "Apple Watch Series 7 (45mm)",
        "Watch6,8": "Apple Watch Series 7 (41mm Cellular)",
        "Watch6,9": "Apple Watch Series 7 (45mm Cellular)",
        "Watch6,10": "Apple Watch SE (2nd gen) (40mm)",
        "Watch6,11": "Apple Watch SE (2nd gen) (44mm)",
        "Watch6,12": "Apple Watch SE (2nd gen) (40mm Cellular)",
        "Watch6,13": "Apple Watch SE (2nd gen) (44mm Cellular)",
        "Watch6,14": "Apple Watch Series 8 (41mm)",
        "Watch6,15": "Apple Watch Series 8 (45mm)",
        "Watch6,16": "Apple Watch Series 8 (41mm Cellular)",
        "Watch6,17": "Apple Watch Series 8 (45mm Cellular)",
        "Watch6,18": "Apple Watch Ultra",

        // iMac
        "iMac21,1": "iMac 24\" (M1, 2021)",
        "iMac21,2": "iMac 24\" (M1, 2021)",
        // Mac mini
        "Macmini9,1": "Mac mini (M1, 2020)",
        // MacBook Air
        "MacBookAir10,1": "MacBook Air (M1, Late 2020)",
        // MacBook Pro
        "MacBookPro17,1": "MacBook Pro 13\"  (M1, 2020)",
        "MacBookPro18,3": "MacBook Pro 14\" (M1 Pro, 2021)",
        "MacBookPro18,4": "MacBook Pro 14\" (M1 Max, 2021)",
        "MacBookPro18,1": "MacBook Pro 16\" (M1 Pro, 2021)",
        "MacBookPro18,2": "MacBook Pro 16\" (M1 Max, 2021)",

        // Simulator
        "i386": "Simulator",
        "x86_64": "Simulator",
    ]

    return deviceMap[platform] ?? platform
}

您可以使用以下代码对其进行测试:

print("Current device is: ", self.userDeviceName())

Objective-C语言

我还在 Objective-C 代码中添加了新设备

不要忘记导入 sys/sysctl.h

 #import <sys/sysctl.h>

- (NSString *) userDeviceName {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];

free(machine);

NSDictionary *deviceMap = @{
    
    // iPhone
    @"iPhone5,1": @"iPhone 5 (GSM)",
    @"iPhone5,2": @"iPhone 5 (GSM+CDMA)",
    @"iPhone5,3": @"iPhone 5c (GSM)",
    @"iPhone5,4": @"iPhone 5c (GSM+CDMA)",
    @"iPhone6,1": @"iPhone 5s (GSM)",
    @"iPhone6,2": @"iPhone 5s (GSM+CDMA)",
    @"iPhone7,2": @"iPhone 6",
    @"iPhone7,1": @"iPhone 6 Plus",
    @"iPhone8,1": @"iPhone 6s",
    @"iPhone8,2": @"iPhone 6s Plus",
    @"iPhone8,4": @"iPhone SE",
    @"iPhone9,1": @"iPhone 7 (GSM+CDMA)",
    @"iPhone9,3": @"iPhone 7 (GSM)",
    @"iPhone9,2": @"iPhone 7 Plus (GSM+CDMA)",
    @"iPhone9,4": @"iPhone 7 Plus (GSM)",
    @"iPhone10,1": @"iPhone 8 (GSM+CDMA)",
    @"iPhone10,4": @"iPhone 8 (GSM)",
    @"iPhone10,2": @"iPhone 8 Plus (GSM+CDMA)",
    @"iPhone10,5": @"iPhone 8 Plus (GSM)",
    @"iPhone10,3": @"iPhone X (GSM+CDMA)",
    @"iPhone10,6": @"iPhone X (GSM)",
    @"iPhone11,2": @"iPhone XS",
    @"iPhone11,6": @"iPhone XS Max",
    @"iPhone11,8": @"iPhone XR",
    @"iPhone12,1": @"iPhone 11",
    @"iPhone12,3": @"iPhone 11 Pro",
    @"iPhone12,5": @"iPhone 11 Pro Max",
    @"iPhone12,8": @"iPhone SE (2nd generation) (2020)",
    @"iPhone13,1": @"iPhone 12 mini",
    @"iPhone13,2": @"iPhone 12",
    @"iPhone13,3": @"iPhone 12 Pro",
    @"iPhone13,4": @"iPhone 12 Pro Max",
    @"iPhone14,4": @"iPhone 13 mini",
    @"iPhone14,5": @"iPhone 13",
    @"iPhone14,2": @"iPhone 13 Pro",
    @"iPhone14,3": @"iPhone 13 Pro Max",
    @"iPhone14,6": @"iPhone SE (3rd generation) (2022)",
    @"iPhone14,7": @"iPhone 14",
    @"iPhone14,8": @"iPhone 14 Plus",
    @"iPhone15,2": @"iPhone 14 Pro",
    @"iPhone15,3": @"iPhone 14 Pro Max",

    // iPod touch
    @"iPod7,1": @"iPod Touch (6th generation)",
    @"iPod9,1": @"iPod Touch (7th generation) (2019)",

    // iPad
    @"iPad3,4": @"iPad (4th generation) (Wi-Fi)",
    @"iPad3,5": @"iPad (4th generation) (GSM)",
    @"iPad3,6": @"iPad (4th generation) (GSM+CDMA)",
    @"iPad6,11": @"iPad (5th generation) 9.7\" (2017) (Wi-Fi)",
    @"iPad6,12": @"iPad (5th generation) 9.7\" (2017) (Cellular)",
    @"iPad7,5": @"iPad (6th generation) 9.7\" (2018) (Wi-Fi)",
    @"iPad7,6": @"iPad (6th generation) 9.7\" (2018) (Cellular)",
    @"iPad7,11": @"iPad (7th generation) 10.2\" (2019) (Wi-Fi)",
    @"iPad7,12": @"iPad (7th generation) 10.2\" (2019) (Cellular)",
    @"iPad11,6": @"iPad (8th generation) 10.2\" (2020) (Wi-Fi)",
    @"iPad11,7": @"iPad (8th generation) 10.2\" (2020) (Cellular)",
    @"iPad12,1": @"iPad (9th generation) 10.2\" (2021) (Wi-Fi)",
    @"iPad12,2": @"iPad (9th generation) 10.2\" (2021) (Cellular)",
    @"iPad13,18": @"iPad (10th generation) 10.9\" (2022) (Wi-Fi)",
    @"iPad13,19": @"iPad (10th generation) 10.9\" (2022) (Cellular)",
    
    // iPad Air
    @"iPad4,1": @"iPad Air (Wi-Fi)",
    @"iPad4,2": @"iPad Air (Cellular)",
    @"iPad4,3": @"iPad Air (China)",
    @"iPad5,3": @"iPad Air 2 (Wi-Fi)",
    @"iPad5,4": @"iPad Air 2 (Cellular)",
    @"iPad11,3": @"iPad Air (3rd generation) (2019) (Wi-Fi)",
    @"iPad11,4": @"iPad Air (3rd generation) (2019) (Cellular)",
    @"iPad13,1": @"iPad Air (4th generation) (2020) (Wi-Fi)",
    @"iPad13,2": @"iPad Air (4th generation) (2020) (Cellular)",
    @"iPad13,16": @"iPad Air (5th generation) (2022) (Wi-Fi)",
    @"iPad13,17": @"iPad Air (5th generation) (2022) (Cellular)",
    
    // iPad Pro
    @"iPad6,3": @"iPad Pro 9.7\" (Wi-Fi)",
    @"iPad6,4": @"iPad Pro 9.7\" (Cellular)",
    @"iPad6,7": @"iPad Pro 12.9\" (Wi-Fi)",
    @"iPad6,8": @"iPad Pro 12.9\" (Cellular)",
    @"iPad7,1": @"iPad Pro 12.9\" (2nd generation) (Wi-Fi)",
    @"iPad7,2": @"iPad Pro 12.9\" (2nd generation) (Cellular)",
    @"iPad7,3": @"iPad Pro 10.5\" (Wi-Fi)",
    @"iPad7,4": @"iPad Pro 10.5\" (Cellular)",
    @"iPad8,1": @"iPad Pro 11\" (2018) (Wi-Fi)",
    @"iPad8,2": @"iPad Pro 11\" (2018) (Wi-Fi, 1TB)",
    @"iPad8,3": @"iPad Pro 11\" (2018) (Cellular)",
    @"iPad8,4": @"iPad Pro 11\" (2018) (Cellular 1TB)",
    @"iPad8,5": @"iPad Pro 12.9\" (3rd generation) (2018) (Wi-Fi)",
    @"iPad8,6": @"iPad Pro 12.9\" (3rd generation) (2018) (Wi-Fi, 1TB)",
    @"iPad8,7": @"iPad Pro 12.9\" (3rd generation) (2018) (Cellular)",
    @"iPad8,8": @"iPad Pro 12.9\" (3rd generation) (2018) (Cellular, 1TB)",
    @"iPad8,9": @"iPad Pro 11\" (2nd generation) (2020) (Wi-Fi)",
    @"iPad8,10": @"iPad Pro 11\" (2nd generation) (2020) (Cellular)",
    @"iPad8,11": @"iPad Pro 12.9\" (4th generation) (2020) (Wi-Fi)",
    @"iPad8,12": @"iPad Pro 12.9\" (4th generation) (2020) (Cellular)",
    @"iPad13,4": @"iPad Pro 11\" (3nd generation) (2021) (Wi-Fi)",
    @"iPad13,5": @"iPad Pro 11\" (3nd generation) (2021) (Cellular US)",
    @"iPad13,6": @"iPad Pro 11\" (3nd generation) (2021) (Cellular Global)",
    @"iPad13,7": @"iPad Pro 11\" (3nd generation) (2021) (Cellular China)",
    @"iPad13,8": @"iPad Pro 12.9\" (5th generation) (2021) (Wi-Fi)",
    @"iPad13,9": @"iPad Pro 12.9\" (5th generation) (2021) (Cellular US)",
    @"iPad13,10": @"iPad Pro 12.9\" (5th generation) (2021) (Cellular Global)",
    @"iPad13,11": @"iPad Pro 12.9\" (5th generation) (2021) (Cellular China)",
    @"iPad14,3": @"iPad Pro 11\" (4th generation) (2022) (Wi-Fi)",
    @"iPad14,4": @"iPad Pro 11\" (4th generation) (2022) (Cellular)",
    @"iPad14,5": @"iPad Pro 12.9\" (6th generation) (2022) (Wi-Fi)",
    @"iPad14,6": @"iPad Pro 12.9\" (6th generation) (2022) (Cellular)",
    
    // iPad mini
    @"iPad4,4": @"iPad mini 2 (Wi-Fi)",
    @"iPad4,5": @"iPad mini 2 (Cellular)",
    @"iPad4,6": @"iPad mini 2 (China)",
    @"iPad4,7": @"iPad mini 3 (Wi-Fi)",
    @"iPad4,8": @"iPad mini 3 (Cellular)",
    @"iPad4,9": @"iPad mini 3 (China)",
    @"iPad5,1": @"iPad mini 4 (Wi-Fi)",
    @"iPad5,2": @"iPad mini 4 (Cellular)",
    @"iPad11,1": @"iPad mini (5th generation) (2019) (Wi-Fi)",
    @"iPad11,2": @"iPad mini (5th generation) (2019) (Cellular)",
    @"iPad14,1": @"iPad mini (6th generation) (2021) (Wi-Fi)",
    @"iPad14,2": @"iPad mini (6th generation) (2021) (Cellular)",

    // Apple TV
    @"AppleTV2,1": @"Apple TV 2G",
    @"AppleTV3,1": @"Apple TV 3",
    @"AppleTV3,2": @"Apple TV 3 (2013)",
    @"AppleTV5,3": @"Apple TV 4",
    @"AppleTV6,2": @"Apple TV 4K",
    @"AppleTV11,1": @"Apple TV 4K (2nd generation)",

    // Apple Watch
    @"Watch1,1": @"Apple Watch (1st generation) (38mm)",
    @"Watch1,2": @"Apple Watch (1st generation) (42mm)",
    @"Watch2,6": @"Apple Watch Series 1 (38mm)",
    @"Watch2,7": @"Apple Watch Series 1 (42mm)",
    @"Watch2,3": @"Apple Watch Series 2 (38mm)",
    @"Watch2,4": @"Apple Watch Series 2 (42mm)",
    @"Watch3,1": @"Apple Watch Series 3 (38mm Cellular)",
    @"Watch3,2": @"Apple Watch Series 3 (42mm Cellular)",
    @"Watch3,3": @"Apple Watch Series 3 (38mm)",
    @"Watch3,4": @"Apple Watch Series 3 (42mm)",
    @"Watch4,1": @"Apple Watch Series 4 (40mm)",
    @"Watch4,2": @"Apple Watch Series 4 (44mm)",
    @"Watch4,3": @"Apple Watch Series 4 (40mm Cellular)",
    @"Watch4,4": @"Apple Watch Series 4 (44mm Cellular)",
    @"Watch5,1": @"Apple Watch Series 5 (40mm)",
    @"Watch5,2": @"Apple Watch Series 5 (44mm)",
    @"Watch5,3": @"Apple Watch Series 5 (40mm Cellular)",
    @"Watch5,4": @"Apple Watch Series 5 (44mm Cellular)",
    @"Watch6,1": @"Apple Watch Series 6 (40mm)",
    @"Watch6,2": @"Apple Watch Series 6 (44mm)",
    @"Watch6,3": @"Apple Watch Series 6 (40mm Cellular)",
    @"Watch6,4": @"Apple Watch Series 6 (44mm Cellular)",
    @"Watch5,9": @"Apple Watch SE (40mm)",
    @"Watch5,10": @"Apple Watch SE (44mm)",
    @"Watch5,11": @"Apple Watch SE (40mm Cellular)",
    @"Watch5,12": @"Apple Watch SE (44mm Cellular)",
    @"Watch6,6": @"Apple Watch Series 7 (41mm)",
    @"Watch6,7": @"Apple Watch Series 7 (45mm)",
    @"Watch6,8": @"Apple Watch Series 7 (41mm Cellular)",
    @"Watch6,9": @"Apple Watch Series 7 (45mm Cellular)",
    @"Watch6,10": @"Apple Watch SE (2nd gen) (40mm)",
    @"Watch6,11": @"Apple Watch SE (2nd gen) (44mm)",
    @"Watch6,12": @"Apple Watch SE (2nd gen) (40mm Cellular)",
    @"Watch6,13": @"Apple Watch SE (2nd gen) (44mm Cellular)",
    @"Watch6,14": @"Apple Watch Series 8 (41mm)",
    @"Watch6,15": @"Apple Watch Series 8 (45mm)",
    @"Watch6,16": @"Apple Watch Series 8 (41mm Cellular)",
    @"Watch6,17": @"Apple Watch Series 8 (45mm Cellular)",
    @"Watch6,18": @"Apple Watch Ultra",

    // iMac
    @"iMac21,1": @"iMac 24\" (M1, 2021)",
    @"iMac21,2": @"iMac 24\" (M1, 2021)",
    // Mac mini
    @"Macmini9,1": @"Mac mini (M1, 2020)",
    // MacBook Air
    @"MacBookAir10,1": @"MacBook Air (M1, Late 2020)",
    // MacBook Pro
    @"MacBookPro17,1": @"MacBook Pro 13\"  (M1, 2020)",
    @"MacBookPro18,3": @"MacBook Pro 14\" (M1 Pro, 2021)",
    @"MacBookPro18,4": @"MacBook Pro 14\" (M1 Max, 2021)",
    @"MacBookPro18,1": @"MacBook Pro 16\" (M1 Pro, 2021)",
    @"MacBookPro18,2": @"MacBook Pro 16\" (M1 Max, 2021)",

    // Simulator
    @"i386": @"Simulator",
    @"x86_64": @"Simulator",
};

return deviceMap[platform] ? deviceMap[platform] : platform;

}

您可以使用以下代码对其进行测试:

NSLog(@"Current device is: %@", [self userDeviceName]);

评论

0赞 Magoo 10/6/2016
澄清一下...... 是 6 但 6S 是吗?他们在 6 和 6S 中反转了?iPhone7,2iPhone8,1
2赞 D1mers0n 10/8/2016
是的,这是正确的。您可以在这里查看: theiphonewiki.com/wiki/Models
0赞 Victor Engel 12/10/2018
有什么方法可以检查模拟器的类型吗?:)
0赞 Victor Engel 12/10/2018
这篇文章似乎回答了我的问题:stackoverflow.com/questions/33493800/......
1赞 DawnSong 11/30/2016 #27

在 Swift 3 中回答,

struct DeviceInformation {
    // UIDevice.current.model's value is "iPhone" or "iPad",which does not include details; the following "model" is detailed, such as, iPhone7,1 is actually iPhone 6 plus
    static let model: String = {
        var size = 0
        sysctlbyname("hw.machine", nil, &size, nil, 0)
        var machine = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname("hw.machine", &machine, &size, nil, 0)
        return String(cString: machine)
    }()

    static let uuid = UIDevice.current.identifierForVendor?.uuidString ?? ""
    static let idForAdService = ASIdentifierManager.shared().advertisingIdentifier.uuidString

    static func diviceTypeReadableName() -> String {
        switch model {
        case "iPhone1,1":   return "iPhone 1G"
        case "iPhone1,2":   return "iPhone 3G"
        case "iPhone2,1":   return "iPhone 3GS"
        case "iPhone3,1":   return "iPhone 4"
        case "iPhone3,3":   return "iPhone 4 (Verizon)"
        case "iPhone4,1":   return "iPhone 4S"
        case "iPhone5,1":   return "iPhone 5 (GSM)"
        case "iPhone5,2":   return "iPhone 5 (GSM+CDMA)"
        case "iPhone5,3":   return "iPhone 5c (GSM)"
        case "iPhone5,4":   return "iPhone 5c (GSM+CDMA)"
        case "iPhone6,1":   return "iPhone 5s (GSM)"
        case "iPhone6,2":   return "iPhone 5s (GSM+CDMA)"
        case "iPhone7,2":   return "iPhone 6"
        case "iPhone7,1":   return "iPhone 6 Plus"
        case "iPhone8,1":   return "iPhone 6s"
        case "iPhone8,2":   return "iPhone 6s Plus"
        case "iPhone8,4":   return "iPhone SE"
        case "iPhone9,1":   return "iPhone 7"
        case "iPhone9,3":   return "iPhone 7"
        case "iPhone9,2":   return "iPhone 7 Plus"
        case "iPhone9,4":   return "iPhone 7 Plus"
        case "iPod1,1":     return "iPod Touch 1G"
        case "iPod2,1":     return "iPod Touch 2G"
        case "iPod3,1":     return "iPod Touch 3G"
        case "iPod4,1":     return "iPod Touch 4G"
        case "iPod5,1":     return "iPod Touch 5G"
        case "iPod7,1":     return "iPod Touch 6G"
        case "iPad1,1":     return "iPad 1G"
        case "iPad2,1":     return "iPad 2 (Wi-Fi)"
        case "iPad2,2":     return "iPad 2 (GSM)"
        case "iPad2,3":     return "iPad 2 (CDMA)"
        case "iPad2,4":     return "iPad 2 (Wi-Fi)"
        case "iPad2,5":     return "iPad Mini (Wi-Fi)"
        case "iPad2,6":     return "iPad Mini (GSM)"
        case "iPad2,7":     return "iPad Mini (GSM+CDMA)"
        case "iPad3,1":     return "iPad 3 (Wi-Fi)"
        case "iPad3,2":     return "iPad 3 (GSM+CDMA)"
        case "iPad3,3":     return "iPad 3 (GSM)"
        case "iPad3,4":     return "iPad 4 (Wi-Fi)"
        case "iPad3,5":     return "iPad 4 (GSM)"
        case "iPad3,6":     return "iPad 4 (GSM+CDMA)"
        case "iPad4,1":     return "iPad Air (Wi-Fi)"
        case "iPad4,2":     return "iPad Air (Cellular)"
        case "iPad4,3":     return "iPad Air (China)"
        case "iPad4,4":     return "iPad Mini 2G (Wi-Fi)"
        case "iPad4,5":     return "iPad Mini 2G (Cellular)"
        case "iPad4,6":     return "iPad Mini 2G (China)"
        case "iPad4,7":     return "iPad Mini 3 (Wi-Fi)"
        case "iPad4,8":     return "iPad Mini 3 (Cellular)"
        case "iPad4,9":     return "iPad Mini 3 (China)"
        case "iPad5,1":     return "iPad Mini 4 (Wi-Fi)"
        case "iPad5,2":     return "iPad Mini 4 (Cellular)"
        case "iPad5,3":     return "iPad Air 2 (Wi-Fi)"
        case "iPad5,4":     return "iPad Air 2 (Cellular)"
        case "iPad6,3":     return "iPad Pro 9.7' (Wi-Fi)"
        case "iPad6,4":     return "iPad Pro 9.7' (Cellular)"
        case "iPad6,7":     return "iPad Pro 12.9' (Wi-Fi)"
        case "iPad6,8":     return "iPad Pro 12.9' (Cellular)"
        case "AppleTV2,1":  return "Apple TV 2G"
        case "AppleTV3,1":  return "Apple TV 3"
        case "AppleTV3,2":  return "Apple TV 3 (2013)"
        case "AppleTV5,3":  return "Apple TV 4"
        case "Watch1,1":    return "Apple Watch Series 1 (38mm, S1)"
        case "Watch1,2":    return "Apple Watch Series 1 (42mm, S1)"
        case "Watch2,6":    return "Apple Watch Series 1 (38mm, S1P)"
        case "Watch2,7":    return "Apple Watch Series 1 (42mm, S1P)"
        case "Watch2,3":    return "Apple Watch Series 2 (38mm, S2)"
        case "Watch2,4":    return "Apple Watch Series 2 (42mm, S2)"
        case "i386":        return "Simulator"
        case "x86_64":      return "Simulator"

        default:
            return ""
        }
    }
}
0赞 Bartłomiej Semańczyk 1/9/2017 #28

要识别 iPhone 4S,只需检查以下内容:

var isIphone4S: Bool {

    let width = UIScreen.main.bounds.size.width
    let height = UIScreen.main.bounds.size.height
    let proportions = width > height ? width / height : height / width

    return proportions == 1.5 && UIDevice.current.model == "iPhone"
}
-1赞 Xiang LI 2/3/2017 #29

更新平台字符串。

// Created from this thread: https://gist.github.com/Jaybles/1323251

// Apple TV
if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3G";
if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3G";
if ([platform isEqualToString:@"AppleTV5,3"])   return @"Apple TV 4G";

// Apple Watch
if ([platform isEqualToString:@"Watch1,1"])     return @"Apple Watch";
if ([platform isEqualToString:@"Watch1,2"])     return @"Apple Watch";

// iPhone
if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 2G";
if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4 (GSM)";
if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 (GSM/2012)";
if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (Global)";
if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (Global)";
if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (Global)";
if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
if ([platform isEqualToString:@"iPhone9,1"])    return @"iPhone 7 (Global)";
if ([platform isEqualToString:@"iPhone9,3"])    return @"iPhone 7 (GSM)";
if ([platform isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus (Global)";
if ([platform isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus (GSM)";

// iPad
if ([platform isEqualToString:@"iPad1,1"])      return @"iPad 1";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (Mid 2012)";
if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (CDMA)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (Global)";

// iPad Air
if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air (China)";
if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";

// iPad Mini
if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (Global)";
if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (WiFi)";
if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
if ([platform isEqualToString:@"iPad4,6"])      return @"iPad Mini 2 (China)";
if ([platform isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (WiFi)";
if ([platform isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
if ([platform isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
if ([platform isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
if ([platform isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (Cellular)";

// iPad Pro
if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro (9.7 inch/WiFi)";
if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro (9.7 inch/Cellular)";
if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro (12.9 inch/WiFi)";
if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro (12.9 inch/Cellular)";

// iPod
if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPod7,1"])      return @"iPod Touch 6G";

// Simulator
if ([platform isEqualToString:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
1赞 Md Milan Mia 4/19/2018 #30

为了简单比较,我总是喜欢宏:

#define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

它简单易用。

0赞 Gabriele Quatela 1/22/2019 #31

我创建了一个扩展程序,可让您拥有 3 个级别的身份识别:

  1. 设备类型
  2. 按英寸划分的 iPhone/iPad 类型
  3. 设备型号

它使用最新的iOS设备进行更新

UIDeviceDetector