美文网首页
获取设备型号

获取设备型号

作者: 我是彪小A | 来源:发表于2019-02-16 22:28 被阅读0次

    概述

    在开发过程中,我们经常需要获取设备的具体型号(如iPhone X,iPhone8 Plus等)进行相应的适配等工作。并没有对应的系统API供我们直接调用来获取设备的型号。
    UIDevice类中可以获取设备的相关信息。model属性获取设备的类型,如iPhone、iPad,无法获得设备的具体型号。
    每一种iOS设备都有对应的标识,称为device model。首先获取到设备的device model 然后通过编码的方式映射为具体的设备型号,或者可以将值传到后端进行映射后再返回响应的型号,这样做法可以使得代码能够判断出最新的设备。
    完整的device model参考:https://www.theiphonewiki.com/wiki/Models

    代码

    /*引入头文件
    #import <sys/utsname.h>
    #import <UIKit/UIKit.h>
    */
    +(NSString *)plateformString{
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *machineName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        if(!machineName){
            return [UIDevice currentDevice].model;
        }
        NSDictionary *deviceDic = [XADevice deviceDictionary];
        NSString *plateform = [deviceDic objectForKey:machineName];
        return plateform;
    }
    +(NSDictionary *)deviceDictionary{
        NSDictionary *deviceDic =@{
                                   @"iPhone1,1":@"iPhone",
                                   @"iPhone1,2":@"iPhone 3G",
                                   @"iPhone2,1":@"iPhone 3GS",
                                   
                                   @"iPhone3,1":@"iPhone 4",
                                   @"iPhone3,2":@"iPhone 4",
                                   @"iPhone3,3":@"iPhone 4",
                                   @"iPhone4,1":@"iPhone 4S",
                                   
                                   @"iPhone5,1":@"iPhone 5",
                                   @"iPhone5,2":@"iPhone 5",
                                   @"iPhone5,3":@"iPhone 5c",
                                   @"iPhone5,4":@"iPhone 5c",
                                   @"iPhone6,1":@"iPhone 5s",
                                   @"iPhone6,2":@"iPhone 5s",
                                   
                                   @"iPhone7,2":@"iPhone 6",
                                   @"iPhone7,1":@"iPhone 6 Plus",
                                   
                                   @"iPhone8,1":@"iPhone 6s",
                                   @"iPhone8,2":@"iPhone 6s Plus",
                                   
                                   @"iPhone8,4":@"iPhone SE",
                                   
                                   @"iPhone9,1":@"iPhone 7",
                                   @"iPhone9,3":@"iPhone 7",
                                   @"iPhone9,2":@"iPhone 7 Plus",
                                   @"iPhone9,4":@"iPhone 7 Plus",
                                   
                                   @"iPhone10,1":@"iPhone 8",
                                   @"iPhone10,4":@"iPhone 8",
                                   @"iPhone10,2":@"iPhone 8 Plus",
                                   @"iPhone10,5":@"iPhone 8 Plus",
                                   
                                   @"iPhone10,3":@"iPhone X",
                                   @"iPhone10,6":@"iPhone X",
                                   
                                   @"iPhone11,8":@"iPhone XR",
                                   
                                   @"iPhone11,2":@"iPhone XS",
                                   
                                   @"iPhone11,6":@"iPhone XS Max",
                                   @"iPhone11,4":@"iPhone XS Max",
                                   
                                   @"iPod1,1":@"iPod touch",
                                   @"iPod2,1":@"iPod touch (2nd generation)",
                                   @"iPod3,1":@"iPod touch (3rd generation)",
                                   @"iPod4,1":@"iPod touch (4th generation)",
                                   @"iPod5,1":@"iPod touch (5th generation)",
                                   @"iPod7,1":@"iPod touch (6th generation)",
                                   
                                   @"iPad2,5":@"iPad mini",
                                   @"iPad2,6":@"iPad mini",
                                   @"iPad2,7":@"iPad mini",
                                   
                                   @"iPad4,4":@"iPad mini 2",
                                   @"iPad4,5":@"iPad mini 2",
                                   @"iPad4,6":@"iPad mini 2",
                                   
                                   @"iPad4,7":@"iPad mini 3",
                                   @"iPad4,8":@"iPad mini 3",
                                   @"iPad4,9":@"iPad mini 3",
                                   
                                   @"iPad5,1":@"iPad mini 4",
                                   @"iPad5,2":@"iPad mini 4",
                                   
                                   @"i386":@"Simulator",
                                   @"x86_64":@"Simulator"
                                   };
        return deviceDic;
    }
    

    下载:云盘下载

    相关文章

      网友评论

          本文标题:获取设备型号

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