获取iPhone设备型号

目前常用的是通过屏幕分辨率来区分iphone设备型号(iphone4、iphone5、iphoneX等等),不过在有些时候使用这个方法判断会出现错误的结果。经过查阅资料,找到以下方法来判断,此方法是相当的靠谱!靠谱!靠谱!!!废话不多说,直接上代码:

ps:需要引入头文件:

#import "sys/utsname.h"

#import

.h文件:

typedefNS_ENUM(NSInteger,APDeviceType) {

    APDeviceIphone4 =0,//iphone4系列设备

    APDeviceIphone5,//iphone5系列设备

    APDeviceIphone6,//iphone6系列设备

    APDeviceIphonePlus,//iphonePlus系列设备

    APDeviceIphoneX,//iphoneX系列设备

    APDeviceIphoneSimulator,//iphone模拟器

    APDeviceIphoneOther//其他iphone系列设备

};

@interfaceKS_Tool :NSObject

//获取手机设备型号

+ (APDeviceType)currentDeviceType;

@end

.m文件:

+ (APDeviceType)currentDeviceType

{

    structutsnamesystemInfo;

    uname(&systemInfo);

    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];


    if([deviceString isEqualToString:@"iPhone3,1"])    returnAPDeviceIphone4;

    if([deviceString isEqualToString:@"iPhone3,2"])    returnAPDeviceIphone4;

    if([deviceString isEqualToString:@"iPhone3,3"])    returnAPDeviceIphone4;

    if([deviceString isEqualToString:@"iPhone4,1"])    returnAPDeviceIphone4;

    if([deviceString isEqualToString:@"iPhone5,1"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone5,2"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone5,3"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone5,4"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone6,1"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone6,2"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone7,1"])    returnAPDeviceIphonePlus;

    if([deviceString isEqualToString:@"iPhone7,2"])    returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone8,1"])    returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone8,2"])    returnAPDeviceIphonePlus;

    if([deviceString isEqualToString:@"iPhone8,4"])    returnAPDeviceIphone5;

    if([deviceString isEqualToString:@"iPhone9,1"])    returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone9,2"])    returnAPDeviceIphonePlus;

    if([deviceString isEqualToString:@"iPhone9,3"])    returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone9,4"])    returnAPDeviceIphonePlus;

    if([deviceString isEqualToString:@"iPhone10,1"])  returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone10,4"])  returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone10,2"])  returnAPDeviceIphonePlus;

    if([deviceString isEqualToString:@"iPhone10,5"])  returnAPDeviceIphone6;

    if([deviceString isEqualToString:@"iPhone10,3"])  returnAPDeviceIphoneX;

    if([deviceString isEqualToString:@"iPhone10,6"])  returnAPDeviceIphoneX;

    if([deviceString isEqualToString:@"x86_64"] || [deviceStringisEqualToString:@"i386"]) {

        if ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) {

            return APDeviceIphone5;

        }else if ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO) {

            return APDeviceIphone6;

        }else if ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(1125, 2001), [[UIScreen mainScreen] currentMode].size)

                                                                                   ||CGSizeEqualToSize(CGSizeMake(1242,2208), [[UIScreenmainScreen]currentMode].size)) :NO) {

            return APDeviceIphonePlus;

        }else if ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO) {

            return APDeviceIphoneX;

        }else{

            return APDeviceIphoneOther;

        }

    }

    return APDeviceIphoneOther;

}



通过deviceString变量,还可以判断出对应的iPad设备,此处就不一一列举了。。。

你可能感兴趣的:(获取iPhone设备型号)