簡介:根據(jù)開發(fā)工具Xcode10暖眼,硬件:蘋果放棄4s/5/5c,從se,5s及以上的手機適配;軟件:支持iOS8.0及以上系統(tǒng)適配纺裁。
一诫肠、手機識別
1、硬件編號識別
一款iPhone除了有IPhone XS欺缘、XS Max栋豫、XR等這樣的對外發(fā)布手機編號,還有一套對內(nèi)的iPhone11,2/11,4/11,8等對應的硬件編號
#import <sys/utsname.h>
+ (NSString *)DeviceModel {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceString isEqualToString:@"iPhone1,1"]) return @"Apple#iPhone 1G";
if ([deviceString isEqualToString:@"iPhone1,2"]) return @"Apple#iPhone 3G";
if ([deviceString isEqualToString:@"iPhone2,1"]) return @"Apple#iPhone 3GS";
if ([deviceString isEqualToString:@"iPhone3,1"]) return @"Apple#iPhone 4";
if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Apple#iPhone 4 Verizon";
if ([deviceString isEqualToString:@"iPhone4,1"]) return @"Apple#iPhone 4S";
if ([deviceString isEqualToString:@"iPhone5,2"]) return @"Apple#iPhone 5";
if ([deviceString isEqualToString:@"iPhone5,3"]) return @"Apple#iPhone 5c";
if ([deviceString isEqualToString:@"iPhone5,4"]) return @"Apple#iPhone 5c";
if ([deviceString isEqualToString:@"iPhone6,1"]) return @"Apple#iPhone 5s";
if ([deviceString isEqualToString:@"iPhone6,2"]) return @"Apple#iPhone 5s";
if ([deviceString isEqualToString:@"iPhone7,1"]) return @"Apple#iPhone 6 Plus";
if ([deviceString isEqualToString:@"iPhone7,2"]) return @"Apple#iPhone 6";
if ([deviceString isEqualToString:@"iPhone8,1"]) return @"Apple#iPhone 6s";
if ([deviceString isEqualToString:@"iPhone8,2"]) return @"Apple#iPhone 6s Plus";
if ([deviceString isEqualToString:@"iPhone8,4"]) return @"Apple#iPhone SE";
if ([deviceString isEqualToString:@"iPhone9,1"]) return @"Apple#iPhone 7";
if ([deviceString isEqualToString:@"iPhone9,2"]) return @"Apple#iPhone 7 Plus";
if ([deviceString isEqualToString:@"iPhone9,3"]) return @"Apple#iPhone 7";
if ([deviceString isEqualToString:@"iPhone9,4"]) return @"Apple#iPhone 7 Plus";
if ([deviceString isEqualToString:@"iPhone10,1"]) return @"Apple#iPhone 8 Global";
if ([deviceString isEqualToString:@"iPhone10,2"]) return @"Apple#iPhone 8 Plus Global";
if ([deviceString isEqualToString:@"iPhone10,3"]) return @"Apple#iPhone X Global";
if ([deviceString isEqualToString:@"iPhone10,4"]) return @"Apple#iPhone 8 GSM";
if ([deviceString isEqualToString:@"iPhone10,5"]) return @"Apple#iPhone 8 Plus GSM";
if ([deviceString isEqualToString:@"iPhone10,6"]) return @"Apple#iPhone X GSM";
if ([deviceString isEqualToString:@"iPhone11,2"]) return @"Apple#iPhone XS";
if ([deviceString isEqualToString:@"iPhone11,4"]) return @"Apple#iPhone XS Max (China)";
if ([deviceString isEqualToString:@"iPhone11,6"]) return @"Apple#iPhone XS Max";
if ([deviceString isEqualToString:@"iPhone11,8"]) return @"Apple#iPhone XR";
if ([deviceString isEqualToString:@"iPhone12,1"]) return @"Apple#iPhone 11";
if ([deviceString isEqualToString:@"iPhone12,3"]) return @"Apple#iPhone 11 Pro";
if ([deviceString isEqualToString:@"iPhone12,5"]) return @"Apple#iPhone 11 Pro Max";
if ([deviceString isEqualToString:@"i386"]) return @"Apple#Simulator 32";
if ([deviceString isEqualToString:@"x86_64"]) return @"Apple#Simulator 64";
#ifdef DEBUG
NSLog(@"NOTE: Unknown device type: %@", deviceString);
#endif
return [NSString stringWithFormat:@"Apple#%@",deviceString];
}
2谚殊、屏幕分辨率識別
iPhone 11 Pro Max和iPhone 8 Plus 是App Store審核商店的截圖示范機型
二丧鸯、劉海屏尺寸pt分布
三、實際適配宏定義
/**
* MARK:-屏幕尺寸宏定義
* 導航欄高度 狀態(tài)欄高度 底部tabbar高度 蘋果X底部安全區(qū)高度
*/
//屏幕rect
#define SCREEN_BOUNDS ([UIScreen mainScreen].bounds)
//屏幕寬度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//屏幕高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//屏幕分辨率
#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))
//iPhone X系列判斷
#define IS_iPhoneX (CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(414.f, 896.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(896.f, 414.f), [UIScreen mainScreen].bounds.size))
//狀態(tài)欄高度
#define StatusBarHeight (IS_iPhoneX ? 44.f : 20.f)
//導航欄高度
#define NavBarHeight (44.f+StatusBarHeight)
//底部標簽欄高度
#define TabBarHeight (IS_iPhoneX ? (49.f+34.f) : 49.f)
//安全區(qū)域高度
#define TabbarSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)
//劉海屏除了上面的根據(jù)分辨率络凿,還可按底部編劇判斷方式
// DYCommTool類:
+ (UIWindow *)o_getKeyWindow;
+ (UIWindow *)o_getKeyWindow {
if([[[UIApplication sharedApplication] delegate] window]){
return [[[UIApplication sharedApplication] delegate] window];
}else {
if (@available(iOS 13.0,*)) {
NSArray *arr = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *windowScene = (UIWindowScene *)arr[0];
UIWindow *mainWindow = [windowScene valueForKeyPath:@"delegate.window"];
if(mainWindow){
return mainWindow;
}else{
return [UIApplication sharedApplication].windows.lastObject;
}
}else {
return [UIApplication sharedApplication].keyWindow;
}
}
}
//獲得當前的window
#define KeyWindow [DYCommTool o_getKeyWindow]
// 劉海屏判斷 iPhone x系列出場系統(tǒng)是iOS12骡送,劉海屏的系統(tǒng)都是在iOS 11以上的昂羡,故可以用KeyWindow.safeAreaInsets判斷是否為劉海屏
#define IS_iPhoneX \
( \
{ \
BOOL iPhoneX = NO; \
if (@available(iOS 11.0, *)) { \
if (KeyWindow.safeAreaInsets.bottom > 0) { \
iPhoneX = YES; \
} \
} \
iPhoneX; \
}\
)\
四絮记、啟動圖適配
Assets.xcassets已棄用,推薦使用 LaunchScreen.storyboard 實現(xiàn)
以前的Assets.xcassets:
五虐先、app圖標適配