獲取 iOS 裝置的型別

weixin_33728268發表於2016-07-05

首先匯入標頭檔案

#include <sys/sysctl.h>

獲取裝置內部型號

+ (NSString*)innerDeviceModleString
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char*)malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *innerModle = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    return innerModle;
}

轉換成通用裝置型號

+ (NSString *)currentDeviceModleString {
    NSString *deviceModle = [self innerDeviceModleString];
//iPhone
    if ([deviceModle isEqualToString:@"iPhone1,1"]) return @"iPhone1";
    
    if ([deviceModle isEqualToString:@"iPhone1,2"]) return @"iPhone3";
    
    if ([deviceModle isEqualToString:@"iPhone2,1"]) return @"iPhone3GS";
    
    if ([deviceModle isEqualToString:@"iPhone3,1"]) return @"iPhone4";
    
    if ([deviceModle isEqualToString:@"iPhone3,2"]) return @"iPhone4";
    
    if ([deviceModle isEqualToString:@"iPhone3,3"]) return @"iPhone4";
    
    if ([deviceModle isEqualToString:@"iPhone4,1"]) return @"iPhone4s";
    
    if ([deviceModle isEqualToString:@"iPhone5,1"]) return @"iPhone5";
    
    if ([deviceModle isEqualToString:@"iPhone5,2"]) return @"iPhone5";
    
    if ([deviceModle isEqualToString:@"iPhone5,3"]) return @"iPhone5C";
    
    if ([deviceModle isEqualToString:@"iPhone5,4"]) return @"iPhone5C";
    
    if ([deviceModle isEqualToString:@"iPhone6,1"]) return @"iPhone5S";
    
    if ([deviceModle isEqualToString:@"iPhone6,2"]) return @"iPhone5S";
    
    if ([deviceModle isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus";
    
    if ([deviceModle isEqualToString:@"iPhone7,2"]) return @"iPhone6";
    
    if ([deviceModle isEqualToString:@"iPhone8,1"]) return @"iPhone6S";
    
    if ([deviceModle isEqualToString:@"iPhone8,2"]) return @"iPhone6SPlus";
    
    //iPot Touch
    
    if ([deviceModle isEqualToString:@"iPod1,1"]) return @"iPodTouch";
    
    if ([deviceModle isEqualToString:@"iPod2,1"]) return @"iPodTouch2";
    
    if ([deviceModle isEqualToString:@"iPod3,1"]) return @"iPodTouch3";
    
    if ([deviceModle isEqualToString:@"iPod4,1"]) return @"iPodTouch4";
    
    if ([deviceModle isEqualToString:@"iPod5,1"]) return @"iPodTouch5";
    
    //iPad
    
    if ([deviceModle isEqualToString:@"iPad1,1"]) return @"iPad";
    
    if ([deviceModle isEqualToString:@"iPad2,1"]) return @"iPad2";
    
    if ([deviceModle isEqualToString:@"iPad2,2"]) return @"iPad2";
    
    if ([deviceModle isEqualToString:@"iPad2,3"]) return @"iPad2";
    
    if ([deviceModle isEqualToString:@"iPad2,4"]) return @"iPad2";
    
    if ([deviceModle isEqualToString:@"iPad2,5"]) return @"iPadMini1";
    
    if ([deviceModle isEqualToString:@"iPad2,6"]) return @"iPadMini1";
    
    if ([deviceModle isEqualToString:@"iPad2,7"]) return @"iPadMini1";
    
    if ([deviceModle isEqualToString:@"iPad3,1"]) return @"iPad3";
    
    if ([deviceModle isEqualToString:@"iPad3,2"]) return @"iPad3";
    
    if ([deviceModle isEqualToString:@"iPad3,3"]) return @"iPad3";
    
    if ([deviceModle isEqualToString:@"iPad3,4"]) return @"iPad4";
    
    if ([deviceModle isEqualToString:@"iPad3,5"]) return @"iPad4";
    
    if ([deviceModle isEqualToString:@"iPad3,6"]) return @"iPad4";
    
    if ([deviceModle isEqualToString:@"iPad4,1"]) return @"iPadAir";
    
    if ([deviceModle isEqualToString:@"iPad4,2"]) return @"iPadAir";
    
    if ([deviceModle isEqualToString:@"iPad4,3"]) return @"iPadAir";
    
    if ([deviceModle isEqualToString:@"iPad4,4"]) return @"iPadMini2";
    
    if ([deviceModle isEqualToString:@"iPad4,5"]) return @"iPadMini2";
    
    if ([deviceModle isEqualToString:@"iPad4,6"]) return @"iPadMini2";
    
    if ([deviceModle isEqualToString:@"iPad4,7"]) return @"iPadMini3";
    
    if ([deviceModle isEqualToString:@"iPad4,8"]) return @"iPadMini3";
    
    if ([deviceModle isEqualToString:@"iPad4,9"]) return @"iPadMini3";
    
    if ([deviceModle isEqualToString:@"iPad5,3"]) return @"iPadAir2";
    
    if ([deviceModle isEqualToString:@"iPad5,4"]) return @"iPadAir2";
    // 模擬器
    if ([deviceModle isEqualToString:@"i386"])        return @"iPhoneSimulator";

    if ([deviceModle isEqualToString:@"x86_64"])    return @"iPhoneSimulator";

    if ([deviceModle isEqualToString:@"iPhoneSimulator"]) return @"iPhoneSimulator";

    return deviceModle;
}

相關文章