iOS各系统版本适配总结

「这是我参与2022首次更文挑战的第2天,活动详情查看:2022首次更文挑战」。

记录一下iOS各个系统版本的适配点

整理一下iOS各个系统的适配版本,以下文章只做记录整理用,有些是笔者遇到的,有些是参考网上整理的,因为有些适配的点笔者并没有使用到,因此也没实际适配,最好遇到的点都自己验证一下。

iOS11需要适配的点

1. ios11新增新的左滑删除方法,支持图片和文字样式
// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
复制代码
2. UIScrollView、UITableView、UICollectionView导航偏移问题

滚动视图在被Navigation、TabBar遮住的时候,系统默认会修改滚动视图的contentInset属性,如果用户设置滚动视图的contentInsetAdjustmentBehavior属性为 .never,则系统不会再做相关处理。一般我们都自己处理滚动视图的布局frame,默认滚动视图在导航栏下及TabBar上,因此都需要写上

tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
复制代码

iOS12需要适配的点

1. library not found for -lstdc++.6.0.9

苹果在Xcode10 和 iOS 12中移除了 libstdc++库,由libc++这个库取而代之

解决方案:

一般会报此错误的都是旧版本的SDK库问题,可以删除那个库或者从Xcode9中复制-lstdc++.6.0.9到Xcode10中

/// 模拟器
Xcode 10:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/
Xcode 11:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/
/// 真机
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/
复制代码
2. iOS 12系统WiFi获取SSID(wifi名称)和BSSID(mac地址)失败

iOS 12系统WiFi获取SSID(wifi名称)和BSSID(mac地址)失败, 在iOS 12系统之后,苹果提升了获取WiFi名称和mac地址的权限控制,要获取这些信息,需要手动为应用打开获取WiFi信息的权限。具体操作可以参考《获取iOS设备WiFi名字和mac地址+iOS12系统获取失败解决》。
解决方案:

在开发者账号中,勾选项目的App IDAccess WiFi Infomation选项;
在Xcode的Capabilities中,勾选项目的Access WiFi Infomation选项。

3. webView播放视频返回后状态栏消失

视频播放完成主window成为KeyWindow的时候仍隐藏着UIStatusBar
解决方案:

- (void)videoPlayerFinishedToShowStatusBar {

 if (@available(iOS 12.0, *)) {
   [[NSNotificationCenter defaultCenter] addObserverForName:UIWindowDidBecomeKeyNotification object:self.window queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    }];
  }
}
复制代码

iOS13需要适配的点

1. 深色模式的适配

iOS13新增了深色模式,如果项目中禁止深色模式,需要在Info.plist

image.png 添加,否则深色主题下,项目中系统控件背景色变成黑色背景了。

2. 模态弹出全屏和卡片样式

iOS13的模态弹出模式并不是全屏样式了,如果需要模态依然都是iOS13之前的样式,需要添加 vc.modalPresentationStyle = UIModalPresentationFullScreen; 这样模态弹出就和之前一样了。

3. Sign In with Apple

如果你的应用包含三方登录方式,比如QQ、微信..., 那么你的应用必须包含AppID登录,否则应用上架将会被拒。

4. 私有属性的修改

在 iOS 13 中不再允许使用valueForKeysetValue:forKey: 等方法获取或设置私有属性,虽然编译可以通过,但是在运行时会直接崩溃,并提示一下崩溃信息

/// 使用的私有方法
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

/// 崩溃提示信息
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug' 
复制代码

替换方案

/// 修改UITextField的placeholder字体大小及颜色
NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc]initWithString:self.valueTextField.placeholder attributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xD8D8D8),NSFontAttributeName:UIDEFAULTFONTSIZE(12)}];
self.valueTextField.attributedPlaceholder = arrStr;
复制代码

一般尽量不要在项目中访问系统的私有属性,如果使用的话,可以添加个分类交换KVC方法来处理KVC底层遍历访问key逻辑中的崩溃点

image.png

5. UISearchBar的黑线处理问题

之前为了处理搜索框的黑线问题,通常会遍历 searchBar 的 subViews,找到并删除 UISearchBarBackground

image.png

在 iOS13 中这么做会导致 UI 渲染失败,然后直接崩溃,崩溃信息如下:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Missing or detached view for search bar layout'
复制代码

可以使用以下方式处理黑线

[searchBar setBackgroundImage:[UIImage new]];
复制代码

image.png

6. LaunchImage 被弃用

iOS 8 之前我们是在LaunchImage 来设置启动图,每当苹果推出新的屏幕尺寸的设备,我们需要 assets 里面放入对应的尺寸的启动图,这是非常繁琐的一个步骤。因此在 iOS 8 苹果引入了 LaunchScreen,可以直接在 Storyboard 上设置启动界面样式,可以很方便适配各种屏幕。

需要注意的是,苹果在 Modernizing Your UI for iOS 13 section 中提到 ,从2020年4月开始,所有支持 iOS 13 的 App 必须提供 LaunchScreen.storyboard,否则将无法提交到 App Store 进行审批。

使用 LaunchScreen.storyboard 设置启动页,弃用 LaunchImage

iOS14需要适配的点

1. 照片权限新增了选择照片权限

iOS14中,选择照片权限使用户允许应用访问部分照片的权限。

image.png

2. UITableView contentView

iOS14中,UITableViewCell上的控件直接添加到cell上,点击事件将被contentView挡住,因此,不管是否有点击事件,cell上的控件都需要添加到self.contentView上。

3. 获取唯一标识符 API 废弃

在 iOS13 及以前,系统会默认为用户开启允许追踪设置,我们可以简单的通过代码来获取到用户的IDFA标识符。

if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
    NSString *idfaString = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
    NSLog(@"%@", idfaString);
}
复制代码

iOS 14 以后,会默认关闭广告追踪权限,而且上面判断是否开启权限的方法已经废弃。

解决方案: 需要我们需要在 Info.plist 中配置" `NSUserTrackingUsageDescription " 及描述文案,接着使用 AppTrackingTransparency 框架中的 ATTrackingManager 中的 requestTrackingAuthorizationWithCompletionHandler 请求用户权限,在用户授权后再去访问 IDFA 才能够获取到正确信息。

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

- (void)testIDFA {
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                NSString *idfaString = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
            }
        }];
    } else {
        // 使用原方式访问 IDFA
    }
}
复制代码
4. 定位问题

在 iOS13 及以前,App 请求用户定位授权时为如下形态:一旦用户同意应用获取定位信息,当前应用就可以获取到用户的精确定位。

iOS14 新增用户大致位置选项可供用户选择。iOS14 授权弹窗新增的 Precise的开关默认会选中精确位置。用户通过这个开关可以进行更改,当把这个值设为 On 时,地图上会显示精确位置;切换为Off时,将显示用户的大致位置。

对于对用户位置敏感度不高的 App 来说,这个似乎无影响,但是对于强依赖精确位置的 App 适配工作就显得非常重要了。可以通过用户在 “隐私设置” 中设置来开启精确定位,但是可能用户宁可放弃使用这个应用也不愿意开启。这个时候,iOS14 在 CLLocationManager 新增两个方法可用于向用户申请临时开启一次精确位置权限。

image.png

5. 相机和录音

iOS14 中 App 使用相机和麦克风时会有图标提示以及绿点和黄点提示,并且会显示当前是哪个 App 在使用此功能。我们无法控制是否显示该提示。

6. KVC 方法禁用

如同 iOS 13 时,UITextField 禁止使用 setValue:forKey: 来设置 _placeholderLabel 私有属性,iOS 14 中,UIPageControl 也禁止使用 setValue 方法,来设置 _pageImage 和 _currentPageImage 属性。否则运行时将会崩溃。

iOS15需要适配的点

1. iOS导航栏背景颜色设置失效处理

image.png

image.png

image.png

2. iOS TabBar处理同NavigationBar

image.png

image.png

3. UITableView新增TopPadding高度

从 iOS 15 开始,增加sectionHeaderTopPadding属性,TableView如果是plain类型,一旦实现了viewForHeaderInSection及heightForHeaderInSection方法,如果没有设置sectionHeaderTopPadding为0,默认情况sectionHeaderTopPadding会有22个像素的高度。一般我们需要禁止。

image.png

4. UIImageWriteToSavedPhotosAlbum

在iOS15中,UIImageWriteToSavedPhotosAlbum存储图片之后的回调不再返回图片了,会返回nil。

UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
}
复制代码

总结

暂时先整理这些,有遇到后续再补充。

参考链接

lstdc++.6.0.9适配

iOS12适配及兼容问题解决

iOS14 隐私适配及部分解决方案

iOS 14 适配

iOS15 适配相关

猜你喜欢

转载自juejin.im/post/7062171333089558558