每部 iPhone 都有不同的内部名称:
- (void)viewDidLoad {
[super viewDidLoad];
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];
NSLog(@"iPhone Device%@",[self platformType:platform]);
free(machine);
}
- (NSString *) platformType:(NSString *)platform
{
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:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
}
顺便说一句,iPhone 4、5 和 6 都有不同的屏幕尺寸:
iPhone 4, 4S: (640, 960)
iPhone 5, 5C, 5S: (640, 1136)
iPhone 6: (750, 1334)
iPhone 6 Plus: (1080, 1920)
你可以参考这个问题:Identify new iPhone model on xcode (5, 5c, 5s)