参考资料
识别 MacBook Pro 机型:https://support.apple.com/zh-cn/HT201300
识别 MacBook 机型:https://support.apple.com/zh-cn/HT201608
识别 MacBook Air 机型:https://support.apple.com/zh-cn/HT201862
识别您的 iMac 机型:https://support.apple.com/zh-cn/HT201634
识别 Mac Pro 机型:https://support.apple.com/zh-cn/HT202888等等
Mac电脑的版本:
https://en.wikipedia.org/wiki/Mac_Mini
https://en.wikipedia.org/wiki/IMac
获取 Mac的系统版本号:
NSString *versionString; //系统版本号 NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; versionString = [sv objectForKey:@"ProductVersion"];
获取 设备型号对应的字符串:(机型标识符)
NSString *macDevTypeStr = @"Unknown Mac";//设备型号 size_t len = 0; sysctlbyname("hw.model", NULL, &len, NULL, 0); if (len) { NSMutableData *data = [NSMutableData dataWithLength:len]; sysctlbyname("hw.model", [data mutableBytes], &len, NULL, 0); macDevTypeStr = [NSString stringWithUTF8String:[data bytes]]; }
书写一个类别(NSString+SystemOrDeviceInfo
)来实现这2个功能:“.h”文件:
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface NSString (SystemOrDeviceInfo) /** 获取 设备型号 @return 设备型号 */ +(NSString *)getTheDeviceType; //设备型号 /** 获取 系统版本号 @return 系统版本号 */ +(NSString *)getTheSystemVersion; //系统版本号 @end NS_ASSUME_NONNULL_END
“.m”文件:
#import "NSString+SystemOrDeviceInfo.h" #import <sys/sysctl.h> @implementation NSString (SystemOrDeviceInfo) +(NSString *)getTheSystemVersion { NSString *versionString; //系统版本号 NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; versionString = [sv objectForKey:@"ProductVersion"]; return versionString; } +(NSString *)getTheDeviceType { NSString *macDevTypeStr = @"Unknown Mac";//设备型号 size_t len = 0; sysctlbyname("hw.model", NULL, &len, NULL, 0); if (len) { NSMutableData *data = [NSMutableData dataWithLength:len]; sysctlbyname("hw.model", [data mutableBytes], &len, NULL, 0); macDevTypeStr = [NSString stringWithUTF8String:[data bytes]]; } return macDevTypeStr; } @end
支持 Metal 的 Mac 电脑:https://support.apple.com/zh-cn/HT205073
识别 MacBook:https://support.apple.com/zh-cn/HT201608
识别 MacBook Air机型:https://support.apple.com/zh-cn/HT201862
识别 MacBook Pro 机型:https://support.apple.com/zh-cn/HT201300
识别 Mac mini 机型:https://support.apple.com/zh-cn/HT201894
识别 iMac:https://support.apple.com/zh-cn/HT201634
识别 Mac Pro:https://support.apple.com/zh-cn/HT202888
如下设备及以上 且 系统版本为OS X El Capitan(10.11)及以上 可支持metal!MacBook:机型标识符(设备类型) 字符串为"MacBook8,1"及以上 (2015)
MacBook Air:机型标识符(设备类型) 字符串为"MacBookAir5,1"及以上 (2012)
MacBook Pro:机型标识符(设备类型) 字符串为"MacBookPro9,1"及以上 (2012)
Mac mini:机型标识符(设备类型) 字符串为"Macmini6,1"及以上 (2012)
iMac:机型标识符(设备类型) 字符串为"iMac13,1"及以上 (2012)
iMac Pro:机型标识符(设备类型) 字符串含"iMacPro" (iMac Pro所有机型)
Mac Pro:机型标识符(设备类型) 字符串为"MacPro5,1"及以上 (2010)
/** 支持Metal与否 */
NSString * devTypeStr = [NSString getTheDeviceType];
BOOL isSupportMetal = [self checkIsSupportTheMetalWithDevTypeStr:devTypeStr];//支持Metal与否
NSLog(@"支持Metal与否:%d\n",isSupportMetal);
/**
备注: 如下设备及以上支持metal
MacBook机型标识符(设备类型) 字符串为"MacBook8,1"以上 (2015)
MacBook Air机型标识符(设备类型) 字符串为"MacBookAir5,1"以上 (2012)
MacBook Pro机型标识符(设备类型) 字符串为"MacBookPro9,1"以上 (2012)
Mac mini机型标识符(设备类型) 字符串为"Macmini6,1"以上 (2012)
iMac机型标识符(设备类型) 字符串为"iMac13,1"以上 (2012)
iMac Pro机型标识符(设备类型) 字符串含"iMacPro" (iMac Pro所有机型)
Mac Pro机型标识符(设备类型) 字符串为"MacPro5,1"以上 (2010)
*/
/**
判断电脑是否支持Metal
@param devTypeStr 电脑的设备类型字符串
@return 是否支持Metal
*/
-(BOOL)checkIsSupportTheMetalWithDevTypeStr:(NSString *)devTypeStr {
BOOL isSupport = NO;
//"MacBook8,1"以上/"MacBookAir5,1"以上/"MacBookPro9,1"以上/"Macmini6,1"以上/"iMac13,1"以上/含有"iMacPro"/"MacPro5,1"以上
if ([devTypeStr containString:@"MacBook"] && (![devTypeStr containString:@"MacBookAir"] && ![devTypeStr containString:@"MacBookPro"]) ) { //不是MacBook Air和MacBook Pro
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBook" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"8,1"] == YES) { //"MacBook8,1"以上 (当前型号 > "8,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"MacBookAir"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBookAir" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"5,1"] == YES) { //"MacBookAir5,1"以上 (当前型号 > "5,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"MacBookPro"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBookPro" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"9,1"] == YES) { //"MacBookPro9,1"以上 (当前型号 > "9,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"Macmini"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"Macmini" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"6,1"] == YES) { //"Macmini6,1"以上 (当前型号 > "6,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"iMac"] && (![devTypeStr containString:@"iMacPro"]) ) { //不是iMac Pro
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"iMac" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"13,1"] == YES) { //"iMac13,1"以上 (当前型号 > "13,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"iMacPro"]) {
//含有"iMacPro"
isSupport = YES;
} else if ([devTypeStr containString:@"MacPro"] && ![devTypeStr containString:@"iMacPro"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacPro" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"5,1"] == YES) { //"MacPro5,1"以上 (当前型号 > "5,1")
isSupport = YES;
}
}
NSString * sys_Ver_Str = [NSString getTheSystemVersion];
if ([self compareVersionStr:sys_Ver_Str andBaseVesrionStr:@"10.11"] == YES) { //系统版本 OS X El Capitan (10.11)及以上
}
else { //系统版本 OS X El Capitan (10.11) 以下的系统
isSupport = NO;
}
//NSLog(@"isSupport:%d\n",isSupport);
return isSupport;
}
//mac的系统版本号的大小比较判断
-(BOOL)compareVersionStr:(NSString *)verStr andBaseVesrionStr:(NSString *)baseVerStr {
BOOL isBiggerThanBaseVer = NO;
NSArray * base_VerStr_Arr = [baseVerStr componentsSeparatedByString:@"."];
NSArray * comp_VerStr_Arr = [verStr componentsSeparatedByString:@"."];
for (int i = 0; i < base_VerStr_Arr.count; i ++) {
int comp_Ver_int = [comp_VerStr_Arr[i] intValue];
int base_Ver_int = [base_VerStr_Arr[i] intValue];
if (comp_Ver_int < base_Ver_int) { //该位上版本号 < 基础版本号
isBiggerThanBaseVer = NO;
break;
}
if (i == base_VerStr_Arr.count - 1) { //最后一位任然 大于 基础版本号
isBiggerThanBaseVer = YES;
}
}
return isBiggerThanBaseVer;
}
/**
机型版本型号大小比较
@param devTypeStr 当前设备的型号
@param baseStr 基础设备型号
@return 是否是‘当前设备的型号 >= 基础设备型号’
*/
-(BOOL)biggerCompareTheDevTypeStr:(NSString *)devTypeStr withTheBaseStr:(NSString *)baseStr {
BOOL isBiggerThanBaseVer = NO;
NSArray * comp_DevTypeStr_Arr = [devTypeStr componentsSeparatedByString:@","];
int comp_Ver_int_0 = [comp_DevTypeStr_Arr[0] intValue];
int comp_Ver_int_1 = [comp_DevTypeStr_Arr[1] intValue];
NSArray * base_DevTypeStr_Arr = [baseStr componentsSeparatedByString:@","];
int base_Ver_int_0 = [base_DevTypeStr_Arr[0] intValue];
int base_Ver_int_1 = [base_DevTypeStr_Arr[1] intValue];
if (comp_Ver_int_0 >= base_Ver_int_0) { //第一段
if (comp_Ver_int_1 >= base_Ver_int_1) { //第二段
isBiggerThanBaseVer = YES;
}
}
return isBiggerThanBaseVer;
}
支持视网膜显示屏(Retina):
使用4K显示器时,也是满足支持Retina的标准!(代码上要加相应判断)
识别 MacBook:https://support.apple.com/zh-cn/HT201608
识别 MacBook Air机型:https://support.apple.com/zh-cn/HT201862
识别 MacBook Pro 机型:https://support.apple.com/zh-cn/HT201300
识别 Mac mini 机型:https://support.apple.com/zh-cn/HT201894
识别 iMac:https://support.apple.com/zh-cn/HT201634Mac Book:机型标识符(设备类型) 字符串为"MacBook8,1"及以上 (2015)
MacBook Air:机型标识符(设备类型) 字符串为"MacBookAir8,1"及以上 (2018)
MacBook Pro:机型标识符(设备类型) 字符串为"MacBookPro10,1"及以上 (2012)
iMac:机型标识符(设备类型) 字符串为"iMac15,1"及以上 (2014) ("iMac16,1"(2015)\"iMac18,1"(2017)例外)
iMac Pro:机型标识符(设备类型) 字符串含"iMacPro" (iMac Pro所有机型)
/** 支持Retina与否 */
NSString * devTypeStr = [NSString getTheDeviceType];
BOOL isSupportRetina = [self checkIsSupportRitinaWithDevTypeStr:devTypeStr];//支持Retina与否
NSLog(@"支持Retina与否:%d\n",isSupportRetina);
方法的实现:(注意—Mac mini使用4K显示器时,也是满足支持Retina的标准!需要添加相应判断!)
/**
备注:如下设备及以上支持Retina
MacBook机型标识符(设备类型) 字符串为"MacBook8,1"以上 (2015)
MacBook Air机型标识符(设备类型) 字符串为"MacBookAir8,1"以上 (2018)
MacBook Pro机型标识符(设备类型) 字符串为"MacBookPro10,1"以上 (2012)
iMac机型标识符(设备类型) 字符串为"iMac15,1"以上 (2014)除 iMac16,1(2015) iMac18,1(2017)外
iMac Pro机型标识符(设备类型) 字符串含"iMacPro" (iMac Pro所有机型)
4K显示器 也是满足支持Retina的标准!
*/
/**
判断电脑是否支持Ritina
@param devTypeStr 电脑的设备类型字符串
@return 是否支持Ritina
*/
-(BOOL)checkIsSupportRitinaWithDevTypeStr:(NSString *)devTypeStr {
BOOL isSupport = NO;
if ([self isRetinaDisplayOfScreen]) {//是4K显示器
isSupport = YES;
return isSupport;
}
//"MacBook8,1"以上/"MacBookAir8,1"以上/"MacBookPro10,1"以上/"iMac15,1"以上(16,1\18,1例外)/含有"iMacPro"
if ([devTypeStr containString:@"MacBook"] && (![devTypeStr containString:@"MacBookAir"] && ![devTypeStr containString:@"MacBookPro"]) ) { //不是MacBook Air和MacBook Pro
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBook" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"8,1"] == YES) { //"MacBook8,1"以上 (当前型号 > "8,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"MacBookAir"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBookAir" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"8,1"] == YES) { //"MacBookAir8,1"以上 (当前型号 > "8,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"MacBookPro"]) {
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"MacBookPro" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"10,1"] == YES) { //"MacBookPro10,1"以上 (当前型号 > "10,1")
isSupport = YES;
}
} else if ([devTypeStr containString:@"iMac"] && (![devTypeStr containString:@"iMacPro"]) ) { //不是iMac Pro
NSString * sub_CampareStr = [devTypeStr stringByReplacingOccurrencesOfString:@"iMac" withString:@""];
if ([self biggerCompareTheDevTypeStr:sub_CampareStr withTheBaseStr:@"15,1"] == YES) { //"iMac15,1"以上 (当前型号 > "15,1")
isSupport = YES;
if ([sub_CampareStr isEqualToString:@"16,1"] || [sub_CampareStr isEqualToString:@"18,1"]) { //(16,1\18,1例外)
isSupport = NO;
}
}
} else if ([devTypeStr containString:@"iMacPro"]) {
//含有"iMacPro"
isSupport = YES;
}
return isSupport;
}
//是4K显示器——满足支持Retina的标准!
-(BOOL)isRetinaDisplayOfScreen {
NSSize deviceResolution = NSZeroSize;
deviceResolution = [self deviceResolutionOfScreen:[NSScreen mainScreen]];
if (NSEqualSizes(NSZeroSize, deviceResolution)) {
return NO;
}
if (deviceResolution.width >= 144 || deviceResolution.height >= 144) {//144Hz刷新率?????
return YES;
} else {
return NO;
}
}
- (NSSize)deviceResolutionOfScreen:(NSScreen *)aScreen {//获取当前显示器的屏幕信息
if (aScreen == nil) {
return NSZeroSize;
}
NSDictionary * dictionary = [aScreen deviceDescription];
NSSize deviceResolution = NSZeroSize;
deviceResolution = [[dictionary objectForKey:(id)NSDeviceResolution] sizeValue];
return deviceResolution;
}
实际使用:
支持Retina的屏幕,在展示效果(画面/视频)时——Retina屏所展示大小是非Retina屏所展示大小的4倍(长、宽各是2倍!)
我们公司产品是视频播放类的App,所以设置和使用播放的渲染画面时—宽、高都乘了2!
参:有关retina和HiDPI那点事
HiDPI在Retina屏产品中是默认开启的!HiDPI 就是每个维度(宽、高)上的像素增加了一倍。非Retina屏像素尺寸是Retina屏的4倍,导致在显示相同的内容时,非Retina屏要占用更大的面积。
使用实例
系统版本号获取:
NSString *versionString; //系统版本号 NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; versionString = [sv objectForKey:@"ProductVersion"]; NSLog(@"versionString:%@", versionString);
打印:
versionString:10.13.6
NSString *macDevType = @"Unknown Mac";//设备型号 size_t len = 0; sysctlbyname("hw.model", NULL, &len, NULL, 0); if (len) { NSMutableData *data = [NSMutableData dataWithLength:len]; sysctlbyname("hw.model", [data mutableBytes], &len, NULL, 0); macDevType = [NSString stringWithUTF8String:[data bytes]]; } NSLog(@"macDeviceType:%@",macDevType);
打印:
macDeviceType:Macmini7,1