是否有方法确定正在运行应用程序的设备。如果可能的话,我想区分iPhone和iPod Touch。


当前回答

最新更新- 2021年12月:增加iPhone 13, iPhone 13 mini, iPhone 13 Pro, iPhone 13 Pro Max, iPad(第9代),iPad Pro 11"(第三代),iPad Pro 12,9"(第5代),iPad mini(第6代)苹果手表系列7,iMac 24" (M1, 2021), Mac mini (M1, 2020), MacBook Air (M1, 2020), MacBook Pro 13" (M1, 2020), MacBook Pro 14" (M1 Pro/Max, 2021), MacBook Pro 16" (M1 Pro/Max, 2021)

删除了不支持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)
    }()

    return [
        // 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 12 mini",
        "iPhone14,5": "iPhone 12",
        "iPhone14,2": "iPhone 12 Pro",
        "iPhone14,3": "iPhone 12 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) (Wi-Fi)",
        "iPad6,12": "iPad (5th generation) (Cellular)",
        "iPad7,5": "iPad (6th generation) (2018) (Wi-Fi)",
        "iPad7,6": "iPad (6th generation) (2018) (Cellular)",
        "iPad7,11": "iPad (7th generation) (2019) (Wi-Fi)",
        "iPad7,12": "iPad (7th generation) (2019) (Cellular)",
        "iPad11,6": "iPad (8th generation) (2020) (Wi-Fi)",
        "iPad11,7": "iPad (8th generation) (2020) (Cellular)",
        "iPad12,1": "iPad (9th generation) (2021) (Wi-Fi)",
        "iPad12,2": "iPad (9th generation) (2021) (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)",
        // 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)",
        // 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)",

        // 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]
}

你可以使用下面的代码来测试它:

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)",
        @"iPhone13,1": @"iPhone 12 mini",
        @"iPhone13,2": @"iPhone 12",
        @"iPhone13,3": @"iPhone 12 Pro",
        @"iPhone13,4": @"iPhone 12 Pro Max",
        @"iPhone14,4": @"iPhone 12 mini",
        @"iPhone14,5": @"iPhone 12",
        @"iPhone14,2": @"iPhone 12 Pro",
        @"iPhone14,3": @"iPhone 12 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) (Wi-Fi)",
        @"iPad6,12": @"iPad (5th generation) (Cellular)",
        @"iPad7,5": @"iPad (6th generation) (2018) (Wi-Fi)",
        @"iPad7,6": @"iPad (6th generation) (2018) (Cellular)",
        @"iPad7,11": @"iPad (7th generation) (2019) (Wi-Fi)",
        @"iPad7,12": @"iPad (7th generation) (2019) (Cellular)",
        @"iPad11,6": @"iPad (8th generation) (2020) (Wi-Fi)",
        @"iPad11,7": @"iPad (8th generation) (2020) (Cellular)",
        @"iPad12,1": @"iPad (9th generation) (2021) (Wi-Fi)",
        @"iPad12,2": @"iPad (9th generation) (2021) (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)",
        // 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)",
        // 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)",

        // 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];
}

你可以使用下面的代码来测试它:

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

其他回答

我在我的应用程序中使用这个。截至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;
}  

更有用

#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");

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

设备的家庭

[GBDeviceInfo deviceDetails]。family == GBDeviceFamilyiPhone;

设备模型

[GBDeviceInfo deviceDetails]。model == GBDeviceModeliPhone6。

欲了解更多,请参阅Readme。

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

这个类允许你做这样的事情:

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

and

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

设备的分类在platformBits方法中。这个方法对于本文的读者来说应该非常熟悉。我根据设备的类型和年代对其进行了分类,因为我最感兴趣的是整体性能,但源代码可以调整以提供您感兴趣的任何分类,如视网膜屏幕、网络功能等。

的可能值

[[UIDevice currentDevice] model];

是iPod touch, iPhone, iPhone模拟器,iPad, iPad模拟器

如果你想知道iOS正在破坏哪些硬件,比如iPhone3, iPhone4, iPhone5等,下面是代码


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

Objective-C: GitHub/DeviceUtil

吉hub /恶魔大师


#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;
}