Created
November 7, 2016 02:46
-
-
Save wanyakun/46b78984382cfdea6011f55141b8fc2d to your computer and use it in GitHub Desktop.
根据设备platform获取设备型号
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/sysctl.h> | |
+ (NSString *) platform { | |
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); | |
return platform; | |
} | |
+ (NSString *)model { | |
NSString *platform = [self platform]; | |
//iPhone | |
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone"; | |
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"; | |
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; | |
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; | |
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5"; | |
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c"; | |
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c"; | |
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s"; | |
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s"; | |
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"; | |
if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7"; | |
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus"; | |
if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus"; | |
//iPod | |
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch"; | |
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"; | |
//iPad | |
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; | |
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2"; | |
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2"; | |
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2"; | |
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2"; | |
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3"; | |
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3"; | |
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3"; | |
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4"; | |
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4"; | |
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4"; | |
if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air"; | |
if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air"; | |
if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air"; | |
if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2"; | |
if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2"; | |
if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro (12.9 inch)"; | |
if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro (12.9 inch)"; | |
if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro (9.7 inch)"; | |
if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro (9.7 inch)"; | |
//iPad mini | |
if ([platform isEqualToString:@"iPad2,5"]) return @"iPad mini"; | |
if ([platform isEqualToString:@"iPad2,6"]) return @"iPad mini"; | |
if ([platform isEqualToString:@"iPad2,7"]) return @"iPad mini"; | |
if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2"; | |
if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2"; | |
if ([platform isEqualToString:@"iPad4,6"]) return @"iPad mini 2"; | |
if ([platform isEqualToString:@"iPad4,7"]) return @"iPad mini 3"; | |
if ([platform isEqualToString:@"iPad4,8"]) return @"iPad mini 3"; | |
if ([platform isEqualToString:@"iPad4,9"]) return @"iPad mini 3"; | |
if ([platform isEqualToString:@"iPad5,1"]) return @"iPad mini 4"; | |
if ([platform isEqualToString:@"iPad5,2"]) return @"iPad mini 4"; | |
//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"; | |
if ([platform isEqualToString:@"Watch2,6"]) return @"Apple Watch Series 1"; | |
if ([platform isEqualToString:@"Watch2,7"]) return @"Apple Watch Series 1"; | |
if ([platform isEqualToString:@"Watch2,3"]) return @"Apple Watch Series 2"; | |
if ([platform isEqualToString:@"Watch2,4"]) return @"Apple Watch Series 2"; | |
if ([platform isEqualToString:@"i386"] || [platform isEqualToString:@"x86_64"]) return@"iPhone Simulator"; | |
return platform; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment