iOS11正式發(fā)布后只锭,尤其隨著新機(jī)型iPhoneX的即將發(fā)售。iOS開發(fā)面臨很多新的的兼容性問題需要適配和修復(fù)院尔。經(jīng)過一段時間的適配蜻展,大部分暴露的問題已經(jīng)得到解決。這里把這些問題作一個統(tǒng)一的匯總邀摆,方便其他同學(xué)再遇到類似的問題時纵顾,進(jìn)行查閱。
這些問題栋盹,大致分為下面幾類:
一施逾、iOS11適配相關(guān)
1、iOS11系統(tǒng)版本號判斷:
(1)擴(kuò)展原有的宏判斷,增加
#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)通過宏進(jìn)行判斷
#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組件:
具體的代碼實(shí)現(xiàn)其實(shí)和指紋識別就是同一個系統(tǒng)接口曹仗,也就是說之前的TouchID實(shí)現(xiàn)在iPhoneX上可以不作任何適配,直接無縫切換到FaceID闷愤。
詳細(xì)的代碼參考如下:
// 返回是否支持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 = @"刷臉認(rèn)證";
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:@"驗(yàn)證成功"
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:@"驗(yàn)證失敗"
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整葡、保存圖片權(quán)限導(dǎo)致的閃退問題:
試用保存圖片到相冊時,可能會發(fā)生下面的閃退問題:
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.
原因:
權(quán)限隱私提示語沒有導(dǎo)致的讥脐。
解決:
info.plist增加提示語:
二遭居、iPhoneX適配相關(guān)
iPhoneX最重要的改動是屏幕分辨率發(fā)生了變化。
并且取消了物理按鍵旬渠,由手勢取而代之俱萍。對于開發(fā)人員而言,多了個safearea(安全區(qū)域)的概念告丢。
1枪蘑、iPhoneX屏幕尺寸介紹
- 豎屏尺寸:1125px × 2436px(375pt × 812pt @3x)
- 橫屏尺寸:2436px × 1125px(812pt × 375pt @3x)
與其它iOS設(shè)備的差異
以下是與iPhone 6、7岖免、8 (375×667pt) 相比岳颇。
-
Status Bar 高度由 20pt 增長為44pt。
底部:預(yù)留 34pt 給 Home Indicator區(qū)域
寬度不變颅湘,高度多出 145pt话侧,但由于 Status Bar 以及 Home Indicator,實(shí)際上可有效利用的高度為
145 – 24 (Status Bar) – 34 (Home Indicator) = 87pt
差異對比
- 鍵盤高度由 216pt 增長為 291pt
這里主要基于Firefly客戶端框架現(xiàn)有的UI結(jié)構(gòu)闯参,總結(jié)下適配的經(jīng)驗(yàn)瞻鹏。
Firefly現(xiàn)有UI結(jié)構(gòu):
- (1)demo UI使用絕對坐標(biāo),沒有使用自動布局鹿寨;
- (2)UI基本由代碼實(shí)現(xiàn)新博,基本沒有使用XIB;
- (3)導(dǎo)航欄自定義脚草,沒有使用系統(tǒng)導(dǎo)航欄赫悄;
- (4)底部欄使用系統(tǒng)組件UIToolbar實(shí)現(xiàn);
- (5)默認(rèn)的自適配設(shè)置如下:
- (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上下為黑色區(qū)域,不是全屏
適配方案
添加新的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、基礎(chǔ)適配代碼改造
底層框架需要增加的一些適配公共代碼:
//iPhoneX機(jī)型判斷
#define IS_iPhoneX ([UIScreen mainScreen].bounds.size.width == 375 && [UIScreen mainScreen].bounds.size.height == 812)
//狀態(tài)欄高度配置
#define STATUS_GAP (IS_iPhoneX? 44 : 20)
//設(shè)備底部安全距離配置
#define iPhoneXSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)
3樟澜、iPhoneX導(dǎo)航欄適配:
基于Firefly UI現(xiàn)有的導(dǎo)航欄實(shí)現(xiàn)方案误窖,iPhoneX設(shè)備上Navigation Bar 的適配只需要將高度增加 44pt即可叮盘。
相關(guān)的代碼實(shí)現(xiàn)如下:
#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現(xiàn)有的導(dǎo)航欄實(shí)現(xiàn)方案霹俺,iPhoneX設(shè)備上底部欄的適配需要作以下改動:
(1)toolbar高度適配
相關(guān)的代碼實(shí)現(xiàn)如下:
#define iPhoneXSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)
...
#define TOOLBAR_HEIGHT (48 + iPhoneXSafeBottomMargin)
(2)toolbar改用自定義FireflyToolbar實(shí)現(xiàn)
實(shí)現(xiàn)的代碼如下:
@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適配相關(guān)
1、函數(shù)定義跳轉(zhuǎn)修改:
XCode使用“commad+函數(shù)”默認(rèn)不再直接跳向函數(shù)定義丙唧,而是彈出一個工具面板愈魏。如下所示:
恢復(fù)之前的設(shè)置,可以進(jìn)行如下修改:
2想际、打包命令修改:
控制臺打包命令
xcodebuild -exportArchive -exportOptionsPlist "$exportPlistPath" -archivePath "$xcarchivePath" -exportPath "$exportPath"
參數(shù)-exportOptionsPlist 配置文件需要進(jìn)行修改培漏,增加provisioningProfiles字段,新的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>development</string>
<key>provisioningProfiles</key>
<dict>
<key>com.cmbc.firefly.testlib</key>
<string>firefly161109</string>
</dict>
</dict>
</plist>
參考:
iPhoneX人機(jī)交互指南
http://www.reibang.com/p/670318acae90
https://developer.apple.com/ios/update-apps-for-iphone-x/