iOS设备型号获取(更新到iPhone SE 2) 和iOS13状态栏高度获取

今天整理项目,发现之前在网上拷贝的获取设备型号,然后在根据分辨率给设备分类并保存型号的代码if语句太多,看着烦,就处理了下。

iOS13的状态高度获取

#define kStatusBarHeight     (@available(iOS 13.0, *)?[UIApplication sharedApplication].windows[0].windowScene.statusBarManager.statusBarFrame.size.height:[[UIApplication sharedApplication] statusBarFrame].size.height)

设备型号获取
创建一个设备列表plist文件
你可以通过右键这个文件点击"Open As"选择Source Code 用代码的方式打开,拷贝下面这些代码进去覆盖就可以了。





    display
    
        Simulator
        
            2688
            iPhone XS Max
            2436
            iPhone X
            1792
            iPhone XR
            2208
            iPhone 6 Plus
            1334
            iPhone 6
            1136
            iPhone 5
            960
            iPhone 4
            1024
            iPad
            2048
            iPad
            2160
            iPad
            2224
            iPad
            2388
            iPad
            2732
            iPad
        
        Device
        
            iPhone 4
            iPhone 4
            iPhone 4S
            iPhone 4
            iPhone 5
            iPhone 5
            iPhone 5c
            iPhone 5
            iPhone 5s
            iPhone 5
            iPod Touch 5G
            iPod Touch
            iPod Touch 6G
            iPod Touch
            iPhone SE
            iPhone 5
            iPhone SE 2
            iPhone 5
            iPhone 6
            iPhone 6
            iPhone 6s
            iPhone 6
            iPhone 7
            iPhone 6
            iPhone 8
            iPhone 6
            iPhone 6 Plus
            iPhone 6 Plus
            iPhone 6s Plus
            iPhone 6 Plus
            iPhone 7 Plus
            iPhone 6 Plus
            iPhone 8 Plus
            iPhone 6 Plus
            iPhone X
            iPhone X
            iPhone XS
            iPhone X
            iPhone XS Max
            iPhone X Max
            iPhone XR
            iPhone XR
            iPhone 11
            iPhone XR
            iPhone 11 Pro
            iPhone X
            iPhone 11 Pro Max
            iPhone X Max
            iPad
            iPad
            iPad 3G
            iPad
            iPad 2
            iPad
            iPad Mini
            iPad
            iPad 3
            iPad
            iPad 4
            iPad
            iPad Air
            iPad
            iPad Mini 2
            iPad
            iPad Mini 3
            iPad
            iPad Mini 4
            iPad
            iPad Air 2
            iPad
            iPad Pro 9.7
            iPad
            iPad Pro 12.9
            iPad
            iPad 5
            iPad
            iPad Pro 12.9 inch 2nd gen
            iPad
            iPad Pro 10.5 inch
            iPad
            iPad 6
            iPad
            iPad 7
            iPad
            iPad Pro 11-inch
            iPad
            iPad Pro 12.9-inch 3rd gen
            iPad
            iPad Pro 11-inch 2nd gen
            iPad
            iPad Pro 12.9-inch 4th gen
            iPad
            iPad Mini 5
            iPad
            iPad Air 3
            iPad
            iPod Touch 1G
            iPod Touch
            iPod Touch 2G
            iPod Touch
            iPod Touch 3G
            iPod Touch
            iPod Touch 4G
            iPod Touch
            iPod Touch 7G
            iPod Touch
        
    
    model
    
        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,1
        iPhone 6 Plus
        iPhone7,2
        iPhone 6
        iPhone8,1
        iPhone 6s
        iPhone8,2
        iPhone 6s Plus
        iPhone8,4
        iPhone SE
        iPhone9,1
        iPhone 7
        iPhone9,2
        iPhone 7 Plus
        iPhone9,3
        iPhone 7
        iPhone9,4
        iPhone 7 Plus
        iPhone10,1
        iPhone 8
        iPhone10,2
        iPhone 8 Plus
        iPhone10,3
        iPhone X
        iPhone10,4
        iPhone 8
        iPhone10,5
        iPhone 8 Plus
        iPhone10,6
        iPhone X
        iPhone11,2
        iPhone XS
        iPhone11,4
        iPhone XS Max
        iPhone11,6
        iPhone XS Max
        iPhone11,8
        iPhone XR
        iPhone12,1
        iPhone 11
        iPhone12,3
        iPhone 11 Pro
        iPhone12,5
        iPhone 11 Pro Max
        iPhone12,8
        iPhone SE 2
        i386
        Simulator
        x86_64
        Simulator
        iPod1,1
        iPod Touch 1G
        iPod2,1
        iPod Touch 2G
        iPod3,1
        iPod Touch 3G
        iPod4,1
        iPod Touch 4G
        iPod5,1
        iPod Touch 5G
        iPod7,1
        iPod Touch 6G
        iPod9,1
        iPod Touch 7G
        iPad1,1
        iPad
        iPad1,2
        iPad 3G
        iPad2,1
        iPad 2
        iPad2,2
        iPad 2
        iPad2,3
        iPad 2
        iPad2,4
        iPad 2
        iPad2,5
        iPad Mini
        iPad2,6
        iPad Mini
        iPad2,7
        iPad Mini
        iPad3,1
        iPad 3
        iPad3,2
        iPad 3
        iPad3,3
        iPad 3
        iPad3,4
        iPad 4
        iPad3,5
        iPad 4
        iPad3,6
        iPad 4
        iPad4,1
        iPad Air
        iPad4,2
        iPad Air
        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
        iPad5,3
        iPad Air 2
        iPad5,4
        iPad Air 2
        iPad6,3
        iPad Pro 9.7
        iPad6,4
        iPad Pro 9.7
        iPad6,7
        iPad Pro 12.9
        iPad6,8
        iPad Pro 12.9
        iPad6,11
        iPad 5
        iPad6,12
        iPad 5
        iPad7,1
        iPad Pro 12.9 inch 2nd gen
        iPad7,2
        iPad Pro 12.9 inch 2nd gen
        iPad7,3
        iPad Pro 10.5 inch
        iPad7,4
        iPad Pro 10.5 inch
        iPad7,5
        iPad 6
        iPad7,6
        iPad 6
        iPad7,11
        iPad 7
        iPad7,12
        iPad 7
        iPad8,1
        iPad Pro 11-inch
        iPad8,2
        iPad Pro 11-inch
        iPad8,3
        iPad Pro 11-inch
        iPad8,4
        iPad Pro 11-inch
        iPad8,5
        iPad Pro 12.9-inch 3rd gen
        iPad8,6
        iPad Pro 12.9-inch 3rd gen
        iPad8,7
        iPad Pro 12.9-inch 3rd gen
        iPad8,8
        iPad Pro 12.9-inch 3rd gen
        iPad8,9
        iPad Pro 11-inch 2nd gen
        iPad8,10
        iPad Pro 11-inch 2nd gen
        iPad8,11
        iPad Pro 12.9-inch 4th gen
        iPad8,12
        iiPad Pro 12.9-inch 4th gen
        iPad11,1
        iPad Mini 5
        iPad11,2
        iPad Mini 5
        iPad11,3
        iPad Air 3
        iPad11,4
        iPad Air 3
    


通过拿到的设备类型说明字符去匹配就可以拿到型号,然后你也可以根据获得的设备型号再去匹配拿到同分辨率设备方便你不用写太多设备去判断分辨率。

下面这些则是具体的实现,你要是没空自己写就可以看看

获取当前型号类型,也就是 iPhone11,8这串字符串 。
记得导入头文件

#import 


+ (NSString *)getCurrentDeviceModelDescription{
    int mib[2];
    size_t len;
    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 = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
    return platform;
}

调用 getCurrentDeviceModelDescription 获取到设备的类型说明
把plist文件数据拿出来匹配到具体的设备型号

+ (NSString *)getCurrentDeviceModel{
    NSString *platform = [Tools+DeviceCategory getCurrentDeviceModelDescription];
    NSDictionary * dataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DeviceList"ofType:@"plist"]];
    NSDictionary * modelDic = dataDict[@"model"];
    if ([modelDic.allKeys containsObject: platform]) {
        return modelDic[platform];
    }
    return platform;
}

+ (void)saveDeviceInfo {
    NSDictionary * dataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DeviceList"ofType:@"plist"]];
    NSString *model = [self getCurrentDeviceModel];
    NSString *displayModel = model;
    NSDictionary * displayDic = dataDict[@"display"];
    if ([model isEqualToString:@"Simulator"])
    {
        NSDictionary *simulatorDic = displayDic[model];
        NSString *simulatorHeightStr = [NSString stringWithFormat:@"%.f",[UIScreen mainScreen].currentMode.size.height];
        if ([simulatorDic.allKeys containsObject:simulatorHeightStr])
        {
            displayModel = simulatorDic[simulatorHeightStr];
        }
    }
    else
    {
        NSDictionary *deviceDic = displayDic[@"Device"];
        if ([deviceDic.allKeys containsObject: model])
        {
            displayModel = deviceDic[model];
        }
    }
    [[NSUserDefaults standardUserDefaults] setObject:displayModel forKey:@"DeviceName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

你可能感兴趣的:(iOS设备型号获取(更新到iPhone SE 2) 和iOS13状态栏高度获取)