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


当前回答

Dutchie432和Brian Robbins提供了很好的解决方案。但还有一款型号没有推出,那就是Verizon的iPhone 4。这是缺失的一行。

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

其他回答

我进一步将大的“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;
}

更新平台字符串。

// 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";
NSString *deviceType = [[UIDevice currentDevice] systemName];

我可以保证,上述建议将适用于iOS 7及以上版本。我相信iOS 6也能运行。但我不确定。

我喜欢埃里卡·萨顿的作品。她包括苹果电视设备和其他你可能想不到的设备。

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