在进行iOS开发的过程中,我们有时需要对设备的类型进行判别(比如发微博时显示来自哪个客户端,就是通过获取设备的型号然后显示给用户看)以下就介绍获取设备的方法。
我们可以通过代码直接把方法写在所需的类中,然后在类中调用此方法,但写重复的代码对于程序员来说是很不建议去做的事儿,因为这无疑是在做无用功,我们可以把它写在一个类中封装起来,这样在别的类中就可以随时调用。
参考:https://www.theiphonewiki.com/wiki/Models,这个是苹果官方的设备类型说明,可以参考一下
【1】首先我们先建一个New File,命名为GetDeviceModelMethod
【2】在GetDeviceModelMethod.h文件中声明这个方法(用于外部调用),具体代码如下:
#import
@interfaceGetDeviceModelMethod :UIViewController
+ (NSString*)GetCurrentDeviceModel;
@end
【3】然后我们再在 GetDeviceModelMethod.m 文件中实现GetCurrentDeviceModel方法,(记得#include)具体代码如下:
#import"GetDeviceModelMethod.h"
#include
@interfaceGetDeviceModelMethod()
@end
@implementationGetDeviceModelMethod
//获得设备型号
+ (NSString*)GetCurrentDeviceModel
{
intmib[2];
size_tlen;
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 = [NSStringstringWithCString:machineencoding:NSASCIIStringEncoding];
free(machine);
//iPhone
if([platformisEqualToString:@"iPhone1,1"])return@"iPhone 2G (A1203)";
if([platformisEqualToString:@"iPhone1,2"])return@"iPhone 3G (A1241/A1324)";
if([platformisEqualToString:@"iPhone2,1"])return@"iPhone 3GS (A1303/A1325)";
if([platformisEqualToString:@"iPhone3,1"])return@"iPhone 4 (A1332)";
if([platformisEqualToString:@"iPhone3,2"])return@"iPhone 4 (A1332)";
if([platformisEqualToString:@"iPhone3,3"])return@"iPhone 4 (A1349)";
if([platformisEqualToString:@"iPhone4,1"])return@"iPhone 4S (A1387/A1431)";
if([platformisEqualToString:@"iPhone5,1"])return@"iPhone 5 (A1428)";
if([platformisEqualToString:@"iPhone5,2"])return@"iPhone 5 (A1429/A1442)";
if([platformisEqualToString:@"iPhone5,3"])return@"iPhone 5c (A1456/A1532)";
if([platformisEqualToString:@"iPhone5,4"])return@"iPhone 5c (A1507/A1516/A1526/A1529)";
if([platformisEqualToString:@"iPhone6,1"])return@"iPhone 5s (A1453/A1533)";
if([platformisEqualToString:@"iPhone6,2"])return@"iPhone 5s (A1457/A1518/A1528/A1530)";
if([platformisEqualToString:@"iPhone7,2"])return@"iPhone 6 (A1549/A1586)";
if([platformisEqualToString:@"iPhone7,1"])return@"iPhone 6 Plus (A1522/A1524)";
if([platformisEqualToString:@"iPhone8,1"])return@"iPhone 6s (A1633/A1688/A1691/A1700)";
if([platformisEqualToString:@"iPhone8,2"])return@"iPhone 6s Plus (A1634/A1687/A1690/A1699)";
//iPod Touch
if([platformisEqualToString:@"iPod1,1"])return@"iPod Touch (A1213)";
if([platformisEqualToString:@"iPod2,1"])return@"iPod Touch 2G (A1288)";
if([platformisEqualToString:@"iPod3,1"])return@"iPod Touch 3G (A1318)";
if([platformisEqualToString:@"iPod4,1"])return@"iPod Touch 4G (A1367)";
if([platformisEqualToString:@"iPod5,1"])return@"iPod Touch 5G (A1421/A1509)";
if([platformisEqualToString:@"iPod7,1"])return@"iPod Touch 6G (A1574)";
//iPad
if([platformisEqualToString:@"iPad1,1"])return@"iPad (A1219/A1337)";
if([platformisEqualToString:@"iPad2,1"])return@"iPad 2 (A1395)";
if([platformisEqualToString:@"iPad2,2"])return@"iPad 2 (A1396)";
if([platformisEqualToString:@"iPad2,3"])return@"iPad 2 (A1397)";
if([platformisEqualToString:@"iPad2,4"])return@"iPad 2 (A1395+New Chip)";
if([platformisEqualToString:@"iPad3,1"])return@"iPad 3 (A1416)";
if([platformisEqualToString:@"iPad3,2"])return@"iPad 3 (A1403)";
if([platformisEqualToString:@"iPad3,3"])return@"iPad 3 (A1430)";
if([platformisEqualToString:@"iPad3,4"])return@"iPad 4 (A1458)";
if([platformisEqualToString:@"iPad3,5"])return@"iPad 4 (A1459)";
if([platformisEqualToString:@"iPad3,6"])return@"iPad 4 (A1460)";
//iPad Air
if([platformisEqualToString:@"iPad4,1"])return@"iPad Air (A1474)";
if([platformisEqualToString:@"iPad4,2"])return@"iPad Air (A1475)";
if([platformisEqualToString:@"iPad4,3"])return@"iPad Air (A1476)";
if([platformisEqualToString:@"iPad5,3"])return@"iPad Air 2 (A1566)";
if([platformisEqualToString:@"iPad5,4"])return@"iPad Air 2 (A1567)";
//iPad mini
if([platformisEqualToString:@"iPad2,5"])return@"iPad mini 1G (A1432)";
if([platformisEqualToString:@"iPad2,6"])return@"iPad mini 1G (A1454)";
if([platformisEqualToString:@"iPad2,7"])return@"iPad mini 1G (A1455)";
if([platformisEqualToString:@"iPad4,4"])return@"iPad mini 2 (A1489)";
if([platformisEqualToString:@"iPad4,5"])return@"iPad mini 2 (A1490)";
if([platformisEqualToString:@"iPad4,6"])return@"iPad mini 2 (A1491)";
if([platformisEqualToString:@"iPad4,7"])return@"iPad mini 3 (A1599)";
if([platformisEqualToString:@"iPad4,8"])return@"iPad mini 3 (A1600)";
if([platformisEqualToString:@"iPad4,9"])return@"iPad mini 3 (A1601)";
if([platformisEqualToString:@"iPad5,1"])return@"iPad mini 4 (A1538)";
if([platformisEqualToString:@"iPad5,2"])return@"iPad mini 4 (A1550)";
if([platformisEqualToString:@"i386"])return@"iPhone Simulator";
if([platformisEqualToString:@"x86_64"])return@"iPhone Simulator";
returnplatform;
}
@end
【4】这样我们就可以在别的类里调用该方法了,具体使用方法如下:
导入头文件#import"GetDeviceModelMethod.h"
NSString*CurrentDeviceModel=[GetDeviceModelMethodGetCurrentDeviceModel];
NSLog(@"设备类型=%@",CurrentDeviceModel);//在窗口打印设备类型
如有不当之处,欢迎批评补充!