iOS 封装获取机型工具类

Posted by Calvin on 2017-04-14

日常开发中我们常需要根据机型来做一些特殊的处理,本文针对此种需求,将此功能抽离出来,封装成工具类,能够极大方便日常的开发。

Tool.h 暴露接口

+ (NSString *)getCurrentDeviceModel;

Tool.m 实现

+ (NSString *)getCurrentDeviceModel
{
int mib[2];
size_t len;
char *machine;
mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
free(machine);
// iPhone
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone2G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone5";
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone5";
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone5c";
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone5c";
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone5s";
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone5s";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone6";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone6s";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone6sPlus";
if ([platform isEqualToString:@"iPhone8,3"]) return @"iPhoneSE";
if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE";
if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone7";
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus";
//iPod Touch
if ([platform isEqualToString:@"iPod1,1"]) return @"iPodTouch";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPodTouch2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPodTouch3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPodTouch4G";
if ([platform isEqualToString:@"iPod5,1"]) return @"iPodTouch5G";
if ([platform isEqualToString:@"iPod7,1"]) return @"iPodTouch6G";
//iPad
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad2";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad4";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad4";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad4";
//iPad Air
if ([platform isEqualToString:@"iPad4,1"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad4,3"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad5,3"]) return @"iPadAir2";
if ([platform isEqualToString:@"iPad5,4"]) return @"iPadAir2";
//iPad mini
if ([platform isEqualToString:@"iPad2,5"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad2,6"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,6"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,7"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad4,8"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad4,9"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad5,1"]) return @"iPadmini4";
if ([platform isEqualToString:@"iPad5,2"]) return @"iPadmini4";
if ([platform isEqualToString:@"i386"]) return @"iPhoneSimulator";
if ([platform isEqualToString:@"x86_64"]) return @"iPhoneSimulator";
return platform;
}

当然,这只是一种工具类的封装思路。在日常的开发中,我们常遇到诸如 手机号校验、银行卡校验、时间戳转换、获取 UUID 等。

类似的一些小需求,都可以根据实际情况封装成工具类,以便开发。