Determine device (iPhone, iPod Touch) with iPhone

2018-12-31 01:24发布

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

30条回答
旧时光的记忆
2楼-- · 2018-12-31 02:26
NSString *deviceType = [UIDevice currentDevice].model;
查看更多
妖精总统
3楼-- · 2018-12-31 02:26
NSString *deviceType = [[UIDevice currentDevice] systemName];

I can assure that the above suggested one will work in iOS 7 and above. I believe it will work in iOS 6 too. But am not sure about that.

查看更多
伤终究还是伤i
4楼-- · 2018-12-31 02:27

I use this in my app. Up to date as of December 2012.

- (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;
}  
查看更多
怪性笑人.
5楼-- · 2018-12-31 02:27

Based on the very good answers above, here is what I came up with. This is very similar to @Rodrigo's answer, but addresses @Oliver's concern from the comment on that answer. This also provides the option of including the model string in the output string.

+ (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];
}
查看更多
若你有天会懂
6楼-- · 2018-12-31 02:27

Below mentioned code snippet should help :

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   // iPhone device
 }
 else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
   // iPad device
 }
 else {
  // Other device i.e. iPod
 }
查看更多
素衣白纱
7楼-- · 2018-12-31 02:28

Update platform string.

// 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";
查看更多
登录 后发表回答