美文网首页
iOS:获取当前设备型号等信息

iOS:获取当前设备型号等信息

作者: wooke | 来源:发表于2017-11-02 15:44 被阅读45次

    1、获取设备型号(2017.11.02)

    #include <sys/types.h> 
    #include <sys/sysctl.h>
    //获得设备型号 
    + (NSString *)getCurrentDeviceModel 
    { 
        int mib[2]; 
        size_t len; 
        charchar *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 *deviceString = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding]; 
        free(machine); 
    
        // iPhone
        if ([deviceString isEqualToString:@"iPhone1,1"]) { return @"iPhone 1G";}
        if ([deviceString isEqualToString:@"iPhone1,2"]) { return @"iPhone 3G";}
        if ([deviceString isEqualToString:@"iPhone2,1"]) { return @"iPhone 3GS";}
        if ([deviceString isEqualToString:@"iPhone3,1"]) { return @"iPhone 4";}
        if ([deviceString isEqualToString:@"iPhone3,2"]) { return @"Verizon iPhone 4";}
        if ([deviceString isEqualToString:@"iPhone4,1"]) { return @"iPhone 4S";}
        if ([deviceString isEqualToString:@"iPhone5,1"]) { return @"iPhone 5";}
        if ([deviceString isEqualToString:@"iPhone5,2"]) { return @"iPhone 5";}
        if ([deviceString isEqualToString:@"iPhone5,3"]) { return @"iPhone 5C";}
        if ([deviceString isEqualToString:@"iPhone5,4"]) { return @"iPhone 5C";}
        if ([deviceString isEqualToString:@"iPhone6,1"]) { return @"iPhone 5S";}
        if ([deviceString isEqualToString:@"iPhone6,2"]) { return @"iPhone 5S";}
        if ([deviceString isEqualToString:@"iPhone7,1"]) { return @"iPhone 6 Plus";}
        if ([deviceString isEqualToString:@"iPhone7,2"]) { return @"iPhone 6";}
        if ([deviceString isEqualToString:@"iPhone8,1"]) { return @"iPhone 6s";}
        if ([deviceString isEqualToString:@"iPhone8,2"]) { return @"iPhone 6s Plus";}
        if ([deviceString isEqualToString:@"iPhone8,4"]) { return @"iPhone SE";}
        if ([deviceString isEqualToString:@"iPhone9,1"]) { return @"iPhone 7";}
        if ([deviceString isEqualToString:@"iPhone9,3"]) { return @"iPhone 7";}
        if ([deviceString isEqualToString:@"iPhone9,2"]) { return @"iPhone 7 Plus";}
        if ([deviceString isEqualToString:@"iPhone9,4"]) { return @"iPhone 7 Plus";}
        if ([deviceString isEqualToString:@"iPhone10,1"]) { return @"iPhone 8";}
        if ([deviceString isEqualToString:@"iPhone10,4"]) { return @"iPhone 8";}
        if ([deviceString isEqualToString:@"iPhone10,2"]) { return @"iPhone 8 Plus";}
        if ([deviceString isEqualToString:@"iPhone10,5"]) { return @"iPhone 8 Plus";}
        if ([deviceString isEqualToString:@"iPhone10,3"]) { return @"iPhone X";}
        if ([deviceString isEqualToString:@"iPhone10,6"]) { return @"iPhone X";}
        
        // iPod
        if ([deviceString isEqualToString:@"iPod1,1"])   { return @"iPod Touch";}
        if ([deviceString isEqualToString:@"iPod2,1"])   { return @"iPod Touch 2";}
        if ([deviceString isEqualToString:@"iPod3,1"])   { return @"iPod Touch 3";}
        if ([deviceString isEqualToString:@"iPod4,1"])   { return @"iPod Touch 4";}
        if ([deviceString isEqualToString:@"iPod5,1"])   { return @"iPod Touch 5";}
        if ([deviceString isEqualToString:@"iPod7,1"])   { return @"iPod Touch 6";}
        
        // iPad
        if ([deviceString isEqualToString:@"iPad1,1"])   { return @"iPad";}
        if ([deviceString isEqualToString:@"iPad2,1"])   { return @"iPad 2";}
        if ([deviceString isEqualToString:@"iPad2,2"])   { return @"iPad 2";}
        if ([deviceString isEqualToString:@"iPad2,3"])   { return @"iPad 2";}
        if ([deviceString isEqualToString:@"iPad2,4"])   { return @"iPad 2";}
        
        if ([deviceString isEqualToString:@"iPad3,1"])   { return @"iPad 3";}
        if ([deviceString isEqualToString:@"iPad3,2"])   { return @"iPad 3";}
        if ([deviceString isEqualToString:@"iPad3,3"])   { return @"iPad 3";}
        if ([deviceString isEqualToString:@"iPad3,4"])   { return @"iPad 4";}
        if ([deviceString isEqualToString:@"iPad3,5"])   { return @"iPad 4";}
        if ([deviceString isEqualToString:@"iPad3,6"])   { return @"iPad 4";}
        
        if ([deviceString isEqualToString:@"iPad4,1"])   { return @"iPad Air";}
        if ([deviceString isEqualToString:@"iPad4,2"])   { return @"iPad Air";}
        if ([deviceString isEqualToString:@"iPad4,3"])   { return @"iPad Air";}
        if ([deviceString isEqualToString:@"iPad5,3"])   { return @"iPad Air 2";}
        if ([deviceString isEqualToString:@"iPad5,4"])   { return @"iPad Air 2";}
        
        if ([deviceString isEqualToString:@"iPad6,7"])   { return @"iPad Pro (12.9-inch)";}
        if ([deviceString isEqualToString:@"iPad6,8"])   { return @"iPad Pro (12.9-inch)";}
        if ([deviceString isEqualToString:@"iPad6,3"])   { return @"iPad Pro (9.7-inch)";}
        if ([deviceString isEqualToString:@"iPad6,4"])   { return @"iPad Pro (9.7-inch)";}
        
        if ([deviceString isEqualToString:@"iPad6,11"])   { return @"iPad (5th generation)";}
        if ([deviceString isEqualToString:@"iPad6,12"])   { return @"iPad (5th generation)";}
        if ([deviceString isEqualToString:@"iPad7,1"])   { return @"iPad Pro (12.9-inch, 2nd generation)";}
        if ([deviceString isEqualToString:@"iPad7,2"])   { return @"iPad Pro (12.9-inch, 2nd generation)";}
        if ([deviceString isEqualToString:@"iPad7,3"])   { return @"iPad Pro (10.5-inch)";}
        if ([deviceString isEqualToString:@"iPad7,4"])   { return @"iPad Pro (10.5-inch)";}
        
        //iPad mini
        if ([deviceString isEqualToString:@"iPad2,5"])   { return @"iPad mini";}
        if ([deviceString isEqualToString:@"iPad2,6"])   { return @"iPad mini";}
        if ([deviceString isEqualToString:@"iPad2,7"])   { return @"iPad mini";}
        
        if ([deviceString isEqualToString:@"iPad4,4"])   { return @"iPad mini 2";}
        if ([deviceString isEqualToString:@"iPad4,5"])   { return @"iPad mini 2";}
        if ([deviceString isEqualToString:@"iPad4,6"])   { return @"iPad mini 2";}
        
        if ([deviceString isEqualToString:@"iPad4,7"])   { return @"iPad mini 3";}
        if ([deviceString isEqualToString:@"iPad4,8"])   { return @"iPad mini 3";}
        if ([deviceString isEqualToString:@"iPad4,9"])   { return @"iPad mini 3";}
        
        if ([deviceString isEqualToString:@"iPad5,1"])   { return @"iPad mini 4";}
        if ([deviceString isEqualToString:@"iPad5,2"])   { return @"iPad mini 4";}
        
        
        //模拟器
        if ([deviceString isEqualToString:@"i386"])      { return @"Simulator";}
        if ([deviceString isEqualToString:@"x86_64"])    { return @"Simulator";}
        
        return deviceString;
    }
    

    苹果用的硬件编码识别,具体可以参考:https://www.theiphonewiki.com/wiki/Models

    2、获取设备的信息

    • 当前设备
      UIDevice *device = [UIDevice currentDevice];
    • 获取设备所有者的名称,例如:@ "小明的 iPhone"
      NSString *name = device.name;
    • 获取设备的类别 ,例如:@"iPhone", @"iPod touch"
      NSString *model = device.model;
    • 获取本地化版本,基本和model一样
      NSString *type = device.localizedModel;
    • 获取当前运行的系统 ,例如:@"iOS"
      NSString *systemName = device.systemName;
    • 获取当前系统的版本,例如:@"4.0"
      NSString *systemVersion = device.systemVersion;

    相关文章

      网友评论

          本文标题:iOS:获取当前设备型号等信息

          本文链接:https://www.haomeiwen.com/subject/lvoopxtx.html