iOS11、iPhoneX以及XCode9适配总结

iOS11、iPhoneX以及XCode9适配总结

iOS11正式发布后,尤其随着新机型iPhoneX的即将发售。iOS开发面临很多新的的兼容性问题需要适配和修复。经过一段时间的适配,大部分暴露的问题已经得到解决。这里把这些问题作一个统一的汇总,方便其他同学再遇到类似的问题时,进行查阅。

这些问题,大致分为下面几类:

一、iOS11适配相关

1、iOS11系统版本号判断:

(1)扩展原有的宏判断,增加

#define IOS11_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"11.0" options:NSNumericSearch] != NSOrderedAscending )

#define IOS10_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"10.0" options:NSNumericSearch] != NSOrderedAscending )

#define IOS10_OR_EARLIER ( !IOS11_OR_LATER )

#define IOS9_OR_EARLIER ( !IOS10_OR_LATER )

(2)通过宏进行判断

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0

...

#else

...

#endif

(3)使用iOS11新的语法接口:

if (@available(iOS 11.0, *))

{

NSLog(@"%@", [[UIDevice currentDevice] systemVersion]);

}

2、新增face ID人脸识别JS组件:

具体的代码实现其实和指纹识别就是同一个系统接口,也就是说之前的TouchID实现在iPhoneX上可以不作任何适配,直接无缝切换到FaceID。

详细的代码参考如下:

// 返回是否支持faceid人脸识别

- (void)isSupportFaceIDVerify:(NSDictionary *)dict

{

NSString *callBack = [dict objectForKey:@"success"];

NSString *resultValue = nil;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0

LAContext *myContext = [[LAContext alloc] init];

NSError *authError = nil;

NSString *myLocalizedReasonString = @"刷脸认证";

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

resultValue = @"支持";

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

localizedReason:myLocalizedReasonString

reply:^(BOOL success, NSError *error) {

if (success) {

// User authenticated successfully, take appropriate action

dispatch_async(dispatch_get_main_queue(), ^{

[FireflyAlertView showWithTitle:nil

message:@"验证成功"

buttonTitle:@"确定"];

});

} else {

// User did not authenticate successfully, look at error and take appropriate action

if (error.code == kLAErrorUserFallback)

{

WebJSLog_i(@"User tapped Enter Password");

dispatch_async(dispatch_get_main_queue(), ^{

[FireflyAlertView showWithTitle:nil

message:@"手动输入密码"

buttonTitle:@"确定"];

});

}

else if (error.code == kLAErrorUserCancel)

{

WebJSLog_i(@"User tapped Cancel");

}

else

{

dispatch_async(dispatch_get_main_queue(), ^{

[FireflyAlertView showWithTitle:nil

message:@"验证失败"

buttonTitle:@"确定"];

});

}

}

}];

}

else

{

WebJSLog_i(@"error:%@", [authError.userInfo description]);

[FireflyAlertView showWithTitle:nil

message:[authError.userInfo description]

buttonTitle:@"确定"];

resultValue = @"不支持";

}

#else

{

resultValue = @"不支持";

}

#endif

NSDictionary *returnDict = @{@"success" : resultValue,

@"errorMsg" : @""

};

NSString *returnString = [[NSString alloc] initWithData:[returnDict toJSON]

encoding:NSUTF8StringEncoding];

NSString *fullCallback = [NSString stringWithFormat:@"%@('%@')",callBack, returnString];

[self.webController.webView stringByEvaluatingJavaScriptFromString:fullCallback];

}

3、保存图片权限导致的闪退问题:

试用保存图片到相册时,可能会发生下面的闪退问题:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

原因:

权限隐私提示语没有导致的。

解决:

info.plist增加提示语:

屏幕快照 2017-11-08 上午10.32.59.png

二、iPhoneX适配相关

iPhoneX最重要的改动是屏幕分辨率发生了变化。

并且取消了物理按键,由手势取而代之。对于开发人员而言,多了个safearea(安全区域)的概念。

1、iPhoneX屏幕尺寸介绍

竖屏尺寸:1125px × 2436px(375pt × 812pt @3x)

横屏尺寸:2436px × 1125px(812pt × 375pt @3x)

与其它iOS设备的差异

以下是与iPhone 6、7、8 (375×667pt) 相比。

Status Bar 高度由 20pt 增长为44pt。

底部:预留 34pt 给 Home Indicator区域

宽度不变,高度多出 145pt,但由于 Status Bar 以及 Home Indicator,实际上可有效利用的高度为

145 – 24 (Status Bar) – 34 (Home Indicator) = 87pt

差异对比

键盘高度由 216pt 增长为 291pt

这里主要基于Firefly客户端框架现有的UI结构,总结下适配的经验。

Firefly现有UI结构:

(1)demo UI使用绝对坐标,没有使用自动布局;

(2)UI基本由代码实现,基本没有使用XIB;

(3)导航栏自定义,没有使用系统导航栏;

(4)底部栏使用系统组件UIToolbar实现;

(5)默认的自适配设置如下:

(6)App启动图使用LaunchImage配置;

- (void)setDefaultLayout

{

if (IOS6_OR_EARLIER)

{

self.view.frame = CGRectMake(0, 0,

self.view.bounds.size.width,

self.view.bounds.size.height);

}

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])

{

self.automaticallyAdjustsScrollViewInsets = NO;

self.edgesForExtendedLayout = UIRectEdgeNone;

}

}

1、启动图适配:

解决iPhoneX上的app上下为黑色区域,不是全屏

适配方案

添加新的LaunchImage,编辑LaunchImage的Contents.json文件,在前面添加如下代码

{

"extent" : "full-screen",

"idiom" : "iphone",

"subtype" : "2436h",

"filename" : "LaunchImage-Portrait~iphoneX@3x.png",

"minimum-system-version" : "11.0",

"orientation" : "portrait",

"scale" : "3x"

},

启动图分辨率为:1125x2436

2、基础适配代码改造

底层框架需要增加的一些适配公共代码:

//iPhoneX机型判断

#define IS_iPhoneX ([UIScreen mainScreen].bounds.size.width == 375 && [UIScreen mainScreen].bounds.size.height == 812)

//状态栏高度配置

#define STATUS_GAP (IS_iPhoneX? 44 : 20)

//设备底部安全距离配置

#define iPhoneXSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)

3、iPhoneX导航栏适配:

基于Firefly UI现有的导航栏实现方案,iPhoneX设备上Navigation Bar 的适配只需要将高度增加 44pt即可。

相关的代码实现如下:

#define STATUS_GAP (IS_iPhoneX? 44 : 20)

...

#define NAV_BAR_HEIGHT 44

#define NAV_BAR_FRAME (IOS7_OR_LATER? CGRectMake(0, 0, ScreenWidth, NAV_BAR_HEIGHT + STATUS_GAP):CGRectMake(0, 0, ScreenWidth, NAV_BAR_HEIGHT))

适配效果:

4、iPhoneX底部栏适配:

基于Firefly UI现有的导航栏实现方案,iPhoneX设备上底部栏的适配需要作以下改动:

(1)toolbar高度适配

相关的代码实现如下:

#define iPhoneXSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)

...

#define TOOLBAR_HEIGHT (48 + iPhoneXSafeBottomMargin)

(2)toolbar改用自定义FireflyToolbar实现

实现的代码如下:

@interface TestToolbar : FireflyToolbar

@implementation FireflyToolbar

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.backgroundColor = UIColorFromRGBAValue(0x0e, 0x17, 0x31, 0.85);

}

return self;

}

- (void)setItems:(NSArray *)aItems

{

[aItems count];

NSArray *tmpItems = [[NSArray alloc] initWithArray:aItems];

[tmpItems enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

UIView *tmpItem = (UIView *)obj;

CGRect itemFrame = [self getItemFrame:idx

count:[aItems count]

frame:tmpItem.frame];

tmpItem.frame = itemFrame;

[self addSubview:tmpItem];

}];

}

- (CGRect)getItemFrame:(NSUInteger)aIndex count:(NSUInteger)aCount frame:(CGRect)aFrame

{

CGRect itemFrame = CGRectZero;

CGFloat itemWidth = (ScreenWidth - (aCount + 1) * ToolbarItemGap) / aCount;

itemFrame.size.height = aFrame.size.height;

itemFrame.origin.y = (TOOLBAR_BASICHEIGHT - aFrame.size.height) / 2.0;

if (aFrame.size.width < itemWidth)

{

itemFrame.size.width = aFrame.size.width;

itemFrame.origin.x = aIndex * aFrame.size.width + (aIndex + 1) * (ScreenWidth - aCount * aFrame.size.width) / (aCount + 1);

}

else

{

itemFrame.size.width = itemWidth;

itemFrame.origin.x = aIndex * itemWidth + (aIndex + 1) * ToolbarItemGap;

}

return itemFrame;

}

适配效果:

三、XCode9适配相关

1、函数定义跳转修改:

XCode使用“commad+函数”默认不再直接跳向函数定义,而是弹出一个工具面板。如下所示:

恢复之前的设置,可以进行如下修改:

2、打包命令修改:

控制台打包命令

xcodebuild -exportArchive -exportOptionsPlist "$exportPlistPath" -archivePath "$xcarchivePath" -exportPath "$exportPath"

参数-exportOptionsPlist 配置文件需要进行修改,增加provisioningProfiles字段,新的配置文件如下:

method

development

provisioningProfiles

com.cmbc.firefly.testlib

firefly161109

参考:

iPhoneX人机交互指南

http://www.jianshu.com/p/670318acae90

https://developer.apple.com/ios/update-apps-for-iphone-x/

🖌️ 相关文章

关于PHA-3开机供电的感受
365bet亚洲版登陆

关于PHA-3开机供电的感受

📅 11-04 👁️ 7678
墨西哥公布大名单
365天电影观看免费

墨西哥公布大名单

📅 07-17 👁️ 9800