如何以编程方式区分iPhone 4和iPhone 4S?

我正在应用程序,需要检查iPhone模型,但我无法find任何解决scheme。 对于任何build议我都会感激不尽。

这简单得多。 只需简单的复制和粘贴

此外,我已经包含匹配的机器名称的型号名称。

//MARK: Required import #import <sys/utsname.h> + (NSString*)deviceModelName { struct utsname systemInfo; uname(&systemInfo); NSString *machineName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; //MARK: More official list is at //http://theiphonewiki.com/wiki/Models //MARK: You may just return machineName. Following is for convenience NSDictionary *commonNamesDictionary = @{ @"i386": @"iPhone Simulator", @"x86_64": @"iPad Simulator", @"iPhone1,1": @"iPhone", @"iPhone1,2": @"iPhone 3G", @"iPhone2,1": @"iPhone 3GS", @"iPhone3,1": @"iPhone 4", @"iPhone3,2": @"iPhone 4(Rev A)", @"iPhone3,3": @"iPhone 4(CDMA)", @"iPhone4,1": @"iPhone 4S", @"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,1": @"iPhone 6+ (GSM+CDMA)", @"iPhone7,2": @"iPhone 6 (GSM+CDMA)", @"iPhone8,1": @"iPhone 6S (GSM+CDMA)", @"iPhone8,2": @"iPhone 6S+ (GSM+CDMA)", @"iPad1,1": @"iPad", @"iPad2,1": @"iPad 2(WiFi)", @"iPad2,2": @"iPad 2(GSM)", @"iPad2,3": @"iPad 2(CDMA)", @"iPad2,4": @"iPad 2(WiFi Rev A)", @"iPad2,5": @"iPad Mini 1G (WiFi)", @"iPad2,6": @"iPad Mini 1G (GSM)", @"iPad2,7": @"iPad Mini 1G (GSM+CDMA)", @"iPad3,1": @"iPad 3(WiFi)", @"iPad3,2": @"iPad 3(GSM+CDMA)", @"iPad3,3": @"iPad 3(GSM)", @"iPad3,4": @"iPad 4(WiFi)", @"iPad3,5": @"iPad 4(GSM)", @"iPad3,6": @"iPad 4(GSM+CDMA)", @"iPad4,1": @"iPad Air(WiFi)", @"iPad4,2": @"iPad Air(GSM)", @"iPad4,3": @"iPad Air(GSM+CDMA)", @"iPad5,3": @"iPad Air 2 (WiFi)", @"iPad5,4": @"iPad Air 2 (GSM+CDMA)", @"iPad4,4": @"iPad Mini 2G (WiFi)", @"iPad4,5": @"iPad Mini 2G (GSM)", @"iPad4,6": @"iPad Mini 2G (GSM+CDMA)", @"iPad4,7": @"iPad Mini 3G (WiFi)", @"iPad4,8": @"iPad Mini 3G (GSM)", @"iPad4,9": @"iPad Mini 3G (GSM+CDMA)", @"iPod1,1": @"iPod 1st Gen", @"iPod2,1": @"iPod 2nd Gen", @"iPod3,1": @"iPod 3rd Gen", @"iPod4,1": @"iPod 4th Gen", @"iPod5,1": @"iPod 5th Gen", @"iPod7,1": @"iPod 6th Gen", }; NSString *deviceName = commonNamesDictionary[machineName]; if (deviceName == nil) { deviceName = machineName; } return deviceName; } 

以下是对设备模型进行回顾的常用方法。 没有NS方法,所以你必须使用c

 #include <sys/types.h> #include <sys/sysctl.h> - (NSString *)machine { NSString *machine; size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *name = malloc(size); sysctlbyname("hw.machine", name, &size, NULL, 0); machine = [NSString stringWithUTF8String:name]; free(name); return machine; } 
 Update a list of devices: In future will be updated with iPad 4. + (NSString*)deviceModelName { /* @"i386" on the simulator @"x86_64" on the simulator @"iPod1,1" on iPod Touch @"iPod2,1" on iPod Touch Second Generation @"iPod3,1" on iPod Touch Third Generation @"iPod4,1" on iPod Touch Fourth Generation @"iPod5,1" on iPod Touch Fourth Generation @"iPhone1,1" on iPhone @"iPhone1,2" on iPhone 3G @"iPhone2,1" on iPhone 3GS @"iPad1,1" on iPad @"iPad2,1" on iPad 2 @"iPad2,5" on iPad mini 1 @"iPad3,1" on iPad 3 @"iPhone3,1" on iPhone 4 @"iPhone4,1" on iPhone 4S @"iPhone5,1" on iPhone 5 */ struct utsname systemInfo; uname(&systemInfo); NSString *modelName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; if([modelName isEqualToString:@"i386"] || [modelName isEqualToString:@"x86_64"]) { modelName = @"iPhone Simulator"; } else if([modelName isEqualToString:@"iPhone1,1"]) { modelName = @"iPhone"; } else if([modelName isEqualToString:@"iPhone1,2"]) { modelName = @"iPhone 3G"; } else if([modelName isEqualToString:@"iPhone2,1"]) { modelName = @"iPhone 3GS"; } else if([modelName isEqualToString:@"iPhone3,1"]) { modelName = @"iPhone 4"; } else if([modelName isEqualToString:@"iPhone4,1"]) { modelName = @"iPhone 4S"; } else if([modelName isEqualToString:@"iPhone5,1"]) { modelName = @"iPhone 5"; } else if([modelName isEqualToString:@"iPod1,1"]) { modelName = @"iPod 1st Gen"; } else if([modelName isEqualToString:@"iPod2,1"]) { modelName = @"iPod 2nd Gen"; } else if([modelName isEqualToString:@"iPod3,1"]) { modelName = @"iPod 3rd Gen"; } else if([modelName isEqualToString:@"iPod4,1"]) { modelName = @"iPod 4th Gen"; } else if([modelName isEqualToString:@"iPod5,1"]) { modelName = @"iPod 5th Gen"; } else if([modelName isEqualToString:@"iPad1,1"]) { modelName = @"iPad"; } else if([modelName isEqualToString:@"iPad2,1"]) { modelName = @"iPad 2(WiFi)"; } else if([modelName isEqualToString:@"iPad2,2"]) { modelName = @"iPad 2(GSM)"; } else if([modelName isEqualToString:@"iPad2,3"]) { modelName = @"iPad 2(CDMA)"; } else if([modelName isEqualToString:@"iPad2,4"]) { modelName = @"iPad 2(WiFi + New Chip)"; } else if([modelName isEqualToString:@"iPad2,5"]) { modelName = @"iPad mini (WiFi)"; } else if([modelName isEqualToString:@"iPad2,6"]) { modelName = @"iPad mini (GSM)"; } else if([modelName isEqualToString:@"iPad3,1"]) { modelName = @"iPad 3(WiFi)"; } else if([modelName isEqualToString:@"iPad3,2"]) { modelName = @"iPad 3(GSM)"; } else if([modelName isEqualToString:@"iPad3,3"]) { modelName = @"iPad 3(CDMA)"; } return modelName; 

}

为了我的目的,我写了一个小型的库,抽象出底层的C调用,并提供一个Objective-C接口。

 NSLog(@"Big model number: %d", deviceDetails.bigModel); //Big model number: 4 NSLog(@"Small model number: %d", deviceDetails.smallModel); //Small model number: 1 if (deviceDetails.model == GBDeviceModeliPhone4S) { NSLog(@"It's a 4S"); } //It's a 4S if (deviceDetails.family != GBDeviceFamilyiPad) { NSLog(@"It's not an iPad"); } //It's not an iPad NSLog(@"systemInfo string: %@", [GBDeviceInfo rawSystemInfoString]); //systemInfo string: iPhone4,1 

它在github上: GBDeviceInfo

使用这个代码。 这是任何实际的iDevice和型号的完整列表,可以通过检查http://theiphonewiki.com/wiki/Models轻松更新列表。;

 #import <sys/utsname.h> struct utsname systemInfo; uname(&systemInfo); NSString *modelName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; NSString *machinename = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; NSString *modelnumber = @"?"; //Simulator if([modelName isEqualToString:@"i386"] || [modelName isEqualToString:@"x86_64"]) { modelName = @"iPhone Simulator"; } //iPhone else if([modelName isEqualToString:@"iPhone1,1"]) { modelName = @"iPhone"; modelnumber = @"A1203"; } else if([modelName isEqualToString:@"iPhone1,2"]) { modelName = @"iPhone 3G"; modelnumber = @"A1241/A1324"; } else if([modelName isEqualToString:@"iPhone2,1"]) { modelName = @"iPhone 3GS"; modelnumber = @"A1303/A1325"; } else if([modelName isEqualToString:@"iPhone3,1"]) { modelName = @"iPhone 4 (GSM)"; modelnumber = @"A1332"; } else if([modelName isEqualToString:@"iPhone3,2"]) { modelName = @"iPhone 4 GSM Rev A"; modelnumber = @"?"; } else if([modelName isEqualToString:@"iPhone3,3"]) { modelName = @"iPhone 4 (CDMA)"; modelnumber = @"A1349"; } else if([modelName isEqualToString:@"iPhone4,1"]) { modelName = @"iPhone 4S"; modelnumber = @"A1387/A1431"; } else if([modelName isEqualToString:@"iPhone5,1"]) { modelName = @"iPhone 5 (GSM)"; modelnumber = @"A1428"; } else if([modelName isEqualToString:@"iPhone5,2"]) { modelName = @"iPhone 5 (GSM+CDMA)"; modelnumber = @"A1429/A1442"; } //iPod touch else if([modelName isEqualToString:@"iPod1,1"]) { modelName = @"iPod touch 1G"; modelnumber = @"A1213"; } else if([modelName isEqualToString:@"iPod2,1"]) { modelName = @"iPod touch 2G"; modelnumber = @"A1288"; } else if([modelName isEqualToString:@"iPod3,1"]) { modelName = @"iPod touch 3G"; modelnumber = @"A1318"; } else if([modelName isEqualToString:@"iPod4,1"]) { modelName = @"iPod touch 4G"; modelnumber = @"A1367"; } else if([modelName isEqualToString:@"iPod5,1"]) { modelName = @"iPod touch 5G"; modelnumber = @"A1421"; } //iPad else if([modelName isEqualToString:@"iPad1,1"]) { modelName = @"iPad 1G"; modelnumber = @"A1219/A1337"; } else if([modelName isEqualToString:@"iPad2,1"]) { modelName = @"iPad 2 (WiFi)"; modelnumber = @"A1395"; } else if([modelName isEqualToString:@"iPad2,2"]) { modelName = @"iPad 2 (GSM)"; modelnumber = @"A1396"; } else if([modelName isEqualToString:@"iPad2,3"]) { modelName = @"iPad 2 (CDMA)"; modelnumber = @"A1397"; } else if([modelName isEqualToString:@"iPad2,4"]) { modelName = @"iPad 2 (WiFi + Rev A)"; modelnumber = @"A1395"; } else if([modelName isEqualToString:@"iPad3,1"]) { modelName = @"iPad 3 (WiFi)"; modelnumber = @"A1416"; } else if([modelName isEqualToString:@"iPad3,2"]) { modelName = @"iPad 3 (GSM+CDMA)"; modelnumber = @"A1403"; } else if([modelName isEqualToString:@"iPad3,3"]) { modelName = @"iPad 3 (GSM)"; modelnumber = @"A1430"; } else if([modelName isEqualToString:@"iPad3,4"]) { modelName = @"iPad 4 (WiFi)"; modelnumber = @"A1458"; } else if([modelName isEqualToString:@"iPad3,5"]) { modelName = @"iPad 4 (GSM)"; modelnumber = @"A1459"; } else if([modelName isEqualToString:@"iPad3,6"]) { modelName = @"iPad 4 (GSM+CDMA)"; modelnumber = @"A1460"; } //iPad mini else if([modelName isEqualToString:@"iPad2,5"]) { modelName = @"iPad mini (WiFi)"; modelnumber = @"A1432"; } else if([modelName isEqualToString:@"iPad2,6"]) { modelName = @"iPad mini (GSM)"; modelnumber = @"A1454"; } else if([modelName isEqualToString:@"iPad2,7"]) { modelName = @"iPad mini (GSM+CDMA)"; modelnumber = @"A1455"; } //Apple TV, just for funn:) else if([modelName isEqualToString:@"AppleTV2,1"]) { modelName = @"Apple TV 2G"; modelnumber = @"A1378"; } else if([modelName isEqualToString:@"AppleTV3,1"]) { modelName = @"Apple TV 3G"; modelnumber = @"A1427"; } else if([modelName isEqualToString:@"AppleTV3,2"]) { modelName = @"Apple TV 3G (Rev A)"; modelnumber = @"A1469"; } NSLog(@"Device model: %@/ %@",modelName,modelnumber); 

这可能是输出: Device model: iPhone 4 GSM/ iPhone3,1

尝试使用以下内容:您将获得设备的所有详细信息…

 NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]); NSLog(@"name: %@", [[UIDevice currentDevice] name]); NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]); NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]); NSLog(@"model: %@", [[UIDevice currentDevice] model]); NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]); 

如果以上不适合你,那么使用github项目

编辑:

无论您使用的是iPhone还是iPod Touch,

 UIDevice *device = [UIDevice currentDevice]; NSString *systemName = [device systemName]; 

要检测操作系统的版本:

 UIDevice *device = [UIDevice currentDevice]; NSString *systemVersion = [device systemVersion]; 

对于参考: 我如何检测我是否有iPhone 2G,3G,3GS

但是,如果您正在模拟器上运行应用程序,则永远不会将设备名称设置为“iPhone6 +”的“iPhone 6”或类似的东西。 我目前正在使用Xcode Version 6.2 (6C131e) ,我甚至在模拟器上使用很less的macros成功地检查过设备types。 那些是

 // ******************* DEVICE INDENTIFICATION MACROS *****************// #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f) #define IS_IPHONE5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) #define IS_IPHONE6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0) #define IS_IPHONE6PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0) #define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))