简介:根据开发工具Xcode10,硬件:苹果放弃4s/5/5c,从se,5s及以上的手机适配;软件:支持iOS8.0及以上系统适配。
一、手机识别
1、硬件编号识别
一款iPhone除了有IPhone XS、XS Max、XR等这样的对外发布手机编号,还有一套对内的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:-屏幕尺寸宏定义
* 导航栏高度 状态栏高度 底部tabbar高度 苹果X底部安全区高度
*/
//屏幕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))
//状态栏高度
#define StatusBarHeight (IS_iPhoneX ? 44.f : 20.f)
//导航栏高度
#define NavBarHeight (44.f+StatusBarHeight)
//底部标签栏高度
#define TabBarHeight (IS_iPhoneX ? (49.f+34.f) : 49.f)
//安全区域高度
#define TabbarSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)
//刘海屏除了上面的根据分辨率,还可按底部编剧判断方式
// 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系列出场系统是iOS12,刘海屏的系统都是在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 实现
以前的Assets.xcassets:
五、app图标适配