iPhone的設備類型越來越多,
iPhone各代設備一覽:
設備名 | 設備標識 | 分辨率 | 屏幕尺寸 | 屏幕模式 | 上市時間 |
---|---|---|---|---|---|
iPhone初號機 | iPhone1,1 | 320*480 | 3.5(inch) | 1x | 2007-5-29 |
iPhone 3 | iPhone1,2 | 320*480 | 3.5(inch) | 1x | 2008-6-11 |
iPhone 3GS | iPhone2,1 | 320*480 | 3.5(inch) | 1x | 2009-5-19 |
iPhone 4 | iPhone3,1/iPhone3,2/iPhone3,3 | 640*960 | 3.5(inch) | 2x | 2010-6/2010-10 |
iPhone 4s | iPhone4,1 | 640*960 | 3.5(inch) | 2x | 2011-10-4 |
iPhone 5 | iPhone5,1/iPhone5,2 | 640*1136 | 4.0(inch) | 2x | 2012-12-12 |
iPhone 5c | iPhone5,3/iPhone5,4 | 640*1136 | 4.0(inch) | 2x | 2012-12-12 |
iPhone 6 | iPhone7,2 | 750*1334 | 4.7'' | 2x | 2014-9-9 |
iPhone 6 Plus | iPhone7,1 | 1080*1920 | 5.5'' | 3x | 2014-9-9 |
iPhone 6s | iPhone8,1 | 750*1334 | 4.7'' | 2x | 2015-9-9 |
iPhone 6s Plus | iPhone8,2 | 750*1334 | 5.5'' | 3x | 2015-9-9 |
iPhone SE(1st generation) | iPhone8,4 | 640*1136 | 4.0'' | 2x | 2016-3 |
iPhone 7 | iPhone9,1/Phone9,3 | 750*1334 | 4.7'' | 2x | 2016-9-7 |
iPhone 7 Plus | iPhone9,2/ iPhone9,4 | 1080*1920 | 5.5'' | 3x | 2016-9-7 |
iPhone 8 | iPhone10,1/iPhone10,4 | 750*1334 | 4.7'' | 2x | 2017-12-12 |
iPhone 8 Plus | iPhone10,2/iPhone10,5 | 1080*1920 | 5.5'' | 3x | 2017-12-12 |
iPhone X | iPhone10,3/iPhone10,6 | 1125*2436 | 5.8'' | 2x | 2017-12-12 |
iPhone XR | iPhone11,8 | 1125*2436 | 5.8'' | 3x | 2017-12-12 |
iPhone XS | iPhone11,2 | 1125*2436 | 5.8'' | 3x | 2018-9-21 |
iPhone XS Max | iPhone11,4/iPhone11,6 | 1125*2436 | 6.5'' | 3x | 2018-9-12 |
iPhone 11 | iPhone12,1 | 828*1792 | 5.8'' | 3x | 2019-9-10 |
iPhone 11 Pro | iPhone12,3 | 1125*2430 | 5.8'' | 3x | 2019-9-10 |
iPhone 11 Pro Max | iPhone12,5 | 1242*2680 | 6.5'' | 3x | 2019-9-10 |
iPhone SE(2ed generation) | iPhone12,8 | 750*1334 | 4,.7'' | 2x | 2019-9-10 |
其中iPhoneX之前,屏幕分辨率是很好識別的除了比較老的機型(iPhone1-iPhone3GS)3.5‘’是320480分辨率,4.0''是640960,4.7‘’是7501344,5.5''是10801920.基本能記得住,分得清。iPhoneX之后,實在是記不住嘍。所以做適配時有必要做區分。
1、檢測iPhoneX/XS/XR 設備的幾種方式
//
// Tools.m
// ThirdLibPro
//
// Created by wjwdive on 2020/6/10.
// Copyright © 2020 wjwdive. All rights reserved.
//
#import "Tools.h"
#import <sys/sysctl.h>
#import <sys/utsname.h>
// 指紋解鎖必須的頭文件
#import <LocalAuthentication/LocalAuthentication.h>
/**
iPhoneX 開發尺寸
iPhone XS: 5.8 英寸,375pt * 812pt (@3x);
iPhone XR: 6.1 英寸,414pt * 896pt (@2x);
iPhone XS Max: 6.5 英寸,414pt * 896pt (@3x);
*/
@implementation Tools
/// 根據分辨率判斷是否是劉海屏設備,以下兩種情況不適用
// 缺陷1: 在APP支持橫豎屏切換時,
// 缺陷2: 在模擬器調試時,能夠正確判斷當前所選擇的模擬器是不是iPhoneX
+ (BOOL)isiPhoneX {
//iPhone X/XS: 375pt * 812pt(@3x)
//iPhone XS Max: 414pt * 896pt(@3x)
//iPhone XR : 414pt * 896(@2x)
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if (screenHeight == 812.0f || screenHeight == 896.0f) {
return YES;
}
return NO;
}
//通過deviceMoel 來判斷
//缺陷,在模擬器里獲得的是i386,或 x86_64
//獲取device model/ machine name 的方法2
+ (NSString *)machineName1 {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
if (machine == NULL) {
return nil;
}
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
//獲取device model/ machine name 的方法2
+ (NSString *)machineName2 {
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
//在模擬器里可以用下面的方法正確獲取模擬器對應的device model
+ (NSString *)machineSimulator {
NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
return model;
}
//綜上,我們的方法
+ (BOOL)isiPhoneXFull {
static BOOL isiPhone = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
#if TARGET_IPHONE_SIMULATOR
//獲取模擬器對應的DeviceModel
NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
#else
//獲取真機設備的 device model
struct utsname systemInfo;
uname(&systemInfo);
NSString *model [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
#endif
//判斷device model是否為 “iPhone10,3” 和 “iPhone10,6”,或者以“iPhone11開頭”
isiPhone = [model isEqualToString:@"iPhone10,3"] || [model isEqualToString:@"iPhone10,6"] || [model hasPrefix:@"iPhone11,"];
});
return isiPhone;
}
//通過屏幕的寬高【開發尺寸】而非分辨率來判斷
/**
iphoneX 劉海屏只有兩種開發尺寸。一般屏幕(X, XR, XS): 375pt*812pt 大屏幕(XR Max ):414pt * 896pt.
如果APP只支持豎屏,那么寬高值大的就是高度,根據高度如果等於 812pt,或者896pt 就可以判斷是iPhoneX系列的劉海屏
如果APP要支持橫豎屏,就要考慮還有一個手機平放的時候獲取不到橫豎屏幕的值,UIDevice中提供了一個朝向的屬性orientation(橫向,豎向,水平),若要考慮設備處於橫屏或者豎屏,然后分別取對應的屏幕寬度(橫屏)或者高度(豎屏)來判斷,但是當這個屬性的值為FaceUp或者FaceDown(設備水平放置),是無法知道設備的橫屏還是豎屏。
*/
//我們只需要獲取屏幕的寬度和高度,取較大的一方比較是否等於 812或者 896,若有一個相等,就是iPhoneX系列的屏幕
+ (BOOL)isiPhoneXByHeight {
//判斷當前設備是否為iPhone 或 iPod touch
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
//獲取屏幕的寬度和高度, 取較大一方進行比較是否等於 812.0 或者 896.0
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat maxLength = screenWidth > screenHeight ? screenWidth : screenHeight;
if (maxLength == 812.0f || maxLength == 896.f) {
return YES;
}
}
return NO;
}
// 通過底部安全區域來判斷(iOS11 之后引入了安全區域的概念)
/*
iPhone X在豎屏下,keyWindow的safeAreaInsets值為
{top: 44, left: 0, bottom: 34, right: 0}
在橫屏下:
{top: 0, left: 44, bottom: 21, right: 44}
因此我們可以通過判斷 safeAreaInsets 的 bottom是否等於 34.0 或者 21.0 來判斷設備是否是 iPhone X,因為其他設備對應的bottm橫豎屏Inset都為0
缺陷:1、iOS 11之后。2、該方法必須在 window初始化之后才可以使用,也就是didFinishLaunchingWithOptions的回調中才能判斷
*/
+ (BOOL)isiPhoneByBottomInset {
if (@available(iOS 11.0, *)) {
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
//獲取底部安全區域高度, iPhone X 豎屏下為34.0,橫屏下為21.0 其他設備都為0
CGFloat bottomSafeInset = keyWindow.safeAreaInsets.bottom;
if (bottomSafeInset == 34.0 || bottomSafeInset == 21.0) {
return YES;
}
}
return NO;
}
//通過UIStateBar的高度來判斷
/**
在iPhoneX 之前,所有iPhone設備的StatusBar 狀態欄高度都為20.0pt, 而iPhoneX 的為44pt, 因此我們可以根據狀態欄是哦符等於44 .0 來判斷
不足:該方法只適用於豎屏切狀態欄顯示的情況下才能正確檢測,而在橫屏模式下,或者App 隱藏導航欄時,獲取到的狀態欄高度都為0 (StatusBarFrame 的值為 CGRectZero )
*/
+ (BOOL)isiPhoneByStatusBarHeight {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
if (statusBarFrame.size.height == 44.0f) {
return YES;
}
return NO;
}
//通過是否支持FaceID 判斷
/**
只有劉海屏的iPhoneX 設備才支持 FaceID ,因此我們可以通過判斷設備是否支持FaceID 來判斷
缺陷:如果用戶禁用 面部解鎖, canEvaluatePolicy:error: 方法的使用將無法正確的判斷,而且也不適用於模擬器中判斷
*/
+ (BOOL)isiPhoneByFaceID {
if (@available(iOS 11.0, *)) {
//
LAContext *contex = [[LAContext alloc] init];
if ([contex canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
return (contex.biometryType == LABiometryTypeFaceID);
}
}
return NO;
}
@end