获取iOS设备UI类型和设备名称
在开发过程中有时需要获取设备具体型号然后进行不同的适配。
目前我们的在适配iOS设备主要分为UI方面的适配,比如iPhone5 、iPhone6、iPhone6 Plus、iPhoneX、iPhoneXS Max,具体的UI在这些屏幕的适配上都是不一样的。
部分APP还适配了iPad,适配iPad与iPhone 的UI 又不一样。
目前还有一部分的设备是需要获取具体的设备名称,比如我们通过代码获取设备的名称iPhone6,1,但对应的我们经常口头说的设备却并不是iPhone6,其实质对应的是我们最经典的iPhone5。
【获取iOS设备UI类型和设备名称】那么我们如何对应这些需求呢?
设备屏幕类型
我们一般通过宏的定义来区分具体的iPhone和iPad设备类型UI。
首先是我们如何区分iPad和iPhone设备。
// iPhone 设备
#define kUI_IPHONE([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
// iPad 设备
#define kUI_IPAD([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
具体可以参考代码中的枚举值
typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
UIUserInterfaceIdiomUnspecified = -1,
UIUserInterfaceIdiomPhone NS_ENUM_AVAILABLE_IOS(3_2), // iPhone and iPod touch style UI
UIUserInterfaceIdiomPad NS_ENUM_AVAILABLE_IOS(3_2), // iPad style UI
UIUserInterfaceIdiomTV NS_ENUM_AVAILABLE_IOS(9_0), // Apple TV style UI
UIUserInterfaceIdiomCarPlay NS_ENUM_AVAILABLE_IOS(9_0), // CarPlay style UI
};
一般情况下iPad设备一套UI 基本能够适配所有的iPad设备的UI类型,因为Mini系列 Air系列包括目前的新款全面屏其宽高比基本都是一致的。很少的情况去做特殊的UI适配。
首先是我们如何区分iPhone设备不同的UI类型。
// iPhone4 和4s系列
#define kUI_IPHONE4([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)// iPhone5 和5s系列
#define kUI_IPHONE5([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)// iPhone6 和6s系列(包括6,7,8三代产品)
#define kUI_IPHONE6([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)// iPhone6 Plus 和6s Plus系列(包括6,7,8三代产品)
#define kUI_IPHONE6PLUS([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)//判断iPHoneXr(CGSizeMake(828, 1792) 启动图CGSizeMake(750, 1624) 无启动图
#define kUI_IPhoneXR ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size)|| CGSizeEqualToSize(CGSizeMake(750, 1624), [[UIScreen mainScreen] currentMode].size))&& kUI_IPHONE : NO)//判断iPhoneXs 和iPhoneX
#define kUI_IPhoneXS ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)//判断iPhoneXs Max
#define kUI_IPhoneXSMAX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) && kUI_IPHONE : NO)//判断是否是X全系列 留海屏幕
#define kUI_IPhoneX(kUI_IPhoneXR ||kUI_IPhoneXS ||kUI_IPhoneXSMAX)
目前主要是iPhoneX系NaviBar 、 Tabbar 和StatusBar 的高度与其他系列都不一样,所以一般情况下还是有高度也有不一样
//iPhoneX系列
#define kStatusBarHeight(kUI_IPhoneX ? 44.0 : 20.0)
#define kNaviBarHeight(kUI_IPhoneX ? 88.0 : 64.0)
#define kTabBarHeight(kUI_IPhoneX ? 83.0 : 49.0)
获取设备类型名称 我们通过代码获取的设备硬件编码字符串是"iPhone6,1",其实这里面有三个参数, 每个参数都是有具体的含义的。
1、iPhone 代表的是具体的设备类型,比如iPhone代表手机,iPad代表平板具体代码如下
2、6 代表的是具体的设备版本,其中6对应的是iPhone手机5代产品,即我们常说的iPhone 5
3、1 代表销售地区和网络型号,例如"iPhone6,1"中的1表示只在美国、加拿大、墨西哥等地区销售的LTE 网络类型设备,其中2表示其他国家销售的苹果设备,但是也一样在美国和加拿大地区销售。
#import + (NSString *)deviceModelName{
struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//simulator
if ([platform isEqualToString:@"i386"])return @"Simulator";
if ([platform isEqualToString:@"x86_64"])return @"Simulator";
//AirPods
if ([platform isEqualToString:@"AirPods1,1"])return @"AirPods";
//Apple TV
if ([platform isEqualToString:@"AppleTV2,1"])return @"Apple TV (2nd generation)";
if ([platform isEqualToString:@"AppleTV3,1"])return @"Apple TV (3rd generation)";
if ([platform isEqualToString:@"AppleTV3,2"])return @"Apple TV (3rd generation)";
if ([platform isEqualToString:@"AppleTV5,3"])return @"Apple TV (4th generation)";
if ([platform isEqualToString:@"AppleTV6,2"])return @"Apple TV 4K";
//Apple Watch
if ([platform isEqualToString:@"Watch1,1"])return @"Apple Watch (1st generation)";
if ([platform isEqualToString:@"Watch1,2"])return @"Apple Watch (1st generation)";
if ([platform isEqualToString:@"Watch2,6"])return @"Apple Watch Series 1";
if ([platform isEqualToString:@"Watch2,7"])return @"Apple Watch Series 1";
if ([platform isEqualToString:@"Watch2,3"])return @"Apple Watch Series 2";
if ([platform isEqualToString:@"Watch2,4"])return @"Apple Watch Series 2";
if ([platform isEqualToString:@"Watch3,1"])return @"Apple Watch Series 3";
if ([platform isEqualToString:@"Watch3,2"])return @"Apple Watch Series 3";
if ([platform isEqualToString:@"Watch3,3"])return @"Apple Watch Series 3";
if ([platform isEqualToString:@"Watch3,4"])return @"Apple Watch Series 3";
//HomePod
if ([platform isEqualToString:@"AudioAccessory1,1"])return @"HomePod";
//iPad
if ([platform isEqualToString:@"iPad1,1"])return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])return @"iPad 2";
if ([platform isEqualToString:@"iPad2,2"])return @"iPad 2";
if ([platform isEqualToString:@"iPad2,3"])return @"iPad 2";
if ([platform isEqualToString:@"iPad2,4"])return @"iPad 2";
if ([platform isEqualToString:@"iPad3,1"])return @"iPad (3rd generation)";
if ([platform isEqualToString:@"iPad3,2"])return @"iPad (3rd generation)";
if ([platform isEqualToString:@"iPad3,3"])return @"iPad (3rd generation)";
if ([platform isEqualToString:@"iPad3,4"])return @"iPad (4th generation)";
if ([platform isEqualToString:@"iPad3,5"])return @"iPad (4th generation)";
if ([platform isEqualToString:@"iPad3,6"])return @"iPad (4th generation)";
if ([platform isEqualToString:@"iPad4,1"])return @"iPad Air";
if ([platform isEqualToString:@"iPad4,2"])return @"iPad Air";
if ([platform isEqualToString:@"iPad4,3"])return @"iPad Air";
if ([platform isEqualToString:@"iPad5,3"])return @"iPad Air 2";
if ([platform isEqualToString:@"iPad5,4"])return @"iPad Air 2";
if ([platform isEqualToString:@"iPad6,7"])return @"iPad Pro (12.9-inch)";
if ([platform isEqualToString:@"iPad6,8"])return @"iPad Pro (12.9-inch)";
if ([platform isEqualToString:@"iPad6,3"])return @"iPad Pro (9.7-inch)";
if ([platform isEqualToString:@"iPad6,4"])return @"iPad Pro (9.7-inch)";
if ([platform isEqualToString:@"iPad6,11"])return @"iPad (5th generation)";
if ([platform isEqualToString:@"iPad6,12"])return @"iPad (5th generation)";
if ([platform isEqualToString:@"iPad7,1"])return @"iPad Pro (12.9-inch, 2nd generation)";
if ([platform isEqualToString:@"iPad7,2"])return @"iPad Pro (12.9-inch, 2nd generation)";
if ([platform isEqualToString:@"iPad7,3"])return @"iPad Pro (10.5-inch)";
if ([platform isEqualToString:@"iPad7,4"])return @"iPad Pro (10.5-inch)";
if ([platform isEqualToString:@"iPad7,5"])return @"iPad (6th generation)";
if ([platform isEqualToString:@"iPad7,6"])return @"iPad (6th generation)";
//iPad mini
if ([platform isEqualToString:@"iPad2,5"])return @"iPad mini";
if ([platform isEqualToString:@"iPad2,6"])return @"iPad mini";
if ([platform isEqualToString:@"iPad2,7"])return @"iPad mini";
if ([platform isEqualToString:@"iPad4,4"])return @"iPad mini 2";
if ([platform isEqualToString:@"iPad4,5"])return @"iPad mini 2";
if ([platform isEqualToString:@"iPad4,6"])return @"iPad mini 2";
if ([platform isEqualToString:@"iPad4,7"])return @"iPad mini 3";
if ([platform isEqualToString:@"iPad4,8"])return @"iPad mini 3";
if ([platform isEqualToString:@"iPad4,9"])return @"iPad mini 3";
if ([platform isEqualToString:@"iPad5,1"])return @"iPad mini 4";
if ([platform isEqualToString:@"iPad5,2"])return @"iPad mini 4";
//iPhone
if ([platform isEqualToString:@"iPhone1,1"])return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"])return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"])return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"])return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"])return @"iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,2"])return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,3"])return @"iPhone 5C";
if ([platform isEqualToString:@"iPhone5,4"])return @"iPhone 5C";
if ([platform isEqualToString:@"iPhone6,1"])return @"iPhone 5S";
if ([platform isEqualToString:@"iPhone6,2"])return @"iPhone 5S";
if ([platform isEqualToString:@"iPhone7,1"])return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPhone7,2"])return @"iPhone 6";
if ([platform isEqualToString:@"iPhone8,1"])return @"iPhone 6s";
if ([platform isEqualToString:@"iPhone8,2"])return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,4"])return @"iPhone SE";
if ([platform isEqualToString:@"iPhone9,1"])return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,3"])return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,2"])return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPhone9,4"])return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPhone10,1"])return @"iPhone 8";
if ([platform isEqualToString:@"iPhone10,4"])return @"iPhone 8";
if ([platform isEqualToString:@"iPhone10,2"])return @"iPhone 8 Plus";
if ([platform isEqualToString:@"iPhone10,5"])return @"iPhone 8 Plus";
if ([platform isEqualToString:@"iPhone10,3"])return @"iPhone X";
if ([platform isEqualToString:@"iPhone10,6"])return @"iPhone X";
if ([platform isEqualToString:@"iPhone11,8"])return @"iPhone XR";
if ([platform isEqualToString:@"iPhone11,2"])return @"iPhone XS";
if ([platform isEqualToString:@"iPhone11,4"])return @"iPhone XS Max";
if ([platform isEqualToString:@"iPhone11,6"])return @"iPhone XS Max";
//iPod touch
if ([platform isEqualToString:@"iPod1,1"])return @"iPod touch";
if ([platform isEqualToString:@"iPod2,1"])return @"iPod touch (2nd generation)";
if ([platform isEqualToString:@"iPod3,1"])return @"iPod touch (3rd generation)";
if ([platform isEqualToString:@"iPod4,1"])return @"iPod touch (4th generation)";
if ([platform isEqualToString:@"iPod5,1"])return @"iPod touch (5th generation)";
if ([platform isEqualToString:@"iPod7,1"])return @"iPod touch (6th generation)";
return platform;
}
如果大家想了解更多可以点击查看维基网: 苹果设备类型说明
推荐阅读
- 基于SqlSugar的开发框架循序渐进介绍(10)--|基于SqlSugar的开发框架循序渐进介绍(10)-- 利用axios组件的封装,实现对后端API数据的访问和基类的统一封装处
- 只为圆你的钢琴梦|只为圆你的钢琴梦 -- 视谱练习 #iOS
- iOS上传项目的两种方法
- Java项目从文本框获取数值_Java编程获取文本框的内容实例解析
- 使用java代码实现推送IOS消息
- iOS|iOS RunLoop
- JS实现获取时间已经时间与时间戳转换
- 利用Java如何获取Mybatis动态生成的sql接口实现
- 微信|微信小程序文件实现下载并预览(ios及安卓手机)
- iOS|iOS translucent edgesForExtendedLayout和extendedLayoutIncludesOpaqueBars的关系