iOS各系统版本适配总结

作者: binya | 来源:发表于2022-02-08 11:13 被阅读0次

    记录一下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 中不再允许使用 valueForKey、setValue: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逻辑中的崩溃点

    - (void)setNilValueForKey:(NSString *)key {
        NSLog(@"[<%@ %p> setNilValueForKey]: could not set nil as the value for the key length.", self.class, self);
    }
    
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        NSLog(@"[<%@ %p> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key %@.", self.class, self, key);
    }
    
    - (id)valueForUndefinedKey:(NSString *)key {
        NSLog(@"[<%@ %p> valueForUndefinedKey:]: this class is not key value coding-compliant for the key: %@", self.class, self, key);
        return nil;
    }
    
    5. UISearchBar的黑线处理问题

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

    for (UIView *view in searchBar.subviews.lastObject.subviews) {
            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                [view removeFromSuperview];
                break;
            }
        }
    

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

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

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

    // [searchBar setBackgroundImage:[UIImage new]];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(50, 300, kScreenW - 100, 60)];
    [searchBar setBackgroundImage:[UIImage new]];
    [self.view addSubview:searchBar];
    
    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中,选择照片权限使用户允许应用访问部分照片的权限。

       if (@available(iOS 14.0, *)) {
            status = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
            // iOS14 新增的选择部分照片权限时
            // 首次状态之后,部分照片权限走这里
            if (status == PHAuthorizationStatusLimited) {
                if (completion) {
                    // 若允许部分照片权限,即仅仅存图片可用此权限 为YES,否则为NO
                    completion(YES);
                }
                return;
            }
        }
    
    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 新增用户大致位置选项可供用户选择,原因是大多数 App 实际上并不需要获取用户到用户最准确的定位信息。iOS14 授权弹窗新增的 Precise的开关默认会选中精确位置。用户通过这个开关可以进行更改,当把这个值设为 On 时,地图上会显示精确位置;切换为Off时,将显示用户的大致位置。

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

    - (void)requestTemporaryFullAccuracyAuthorizationWithPurposeKey:(NSString *)purposeKey completion:(void(^ _Nullable)(NSError * _Nullable))completion API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0), tvos(14.0));
    
    /*
     *  requestTemporaryFullAccuracyAuthorizationWithPurposeKey:
     *
     *  Discussion:
     *      This is a variant of requestTemporaryAccurateLocationAuthorizationWithPurposeKey:completion:
     *      which doesn't take a completion block.  This is equivalent to passing in a nil
     *      completion block.
     */
    - (void)requestTemporaryFullAccuracyAuthorizationWithPurposeKey:(NSString *)purposeKey API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0), tvos(14.0));
    
    
    5. 相机和录音

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

    6. KVC 方法禁用

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

    iOS15需要适配的点

    1. iOS导航栏背景颜色设置失效处理
    // 关于navigationBar背景图片失效的问题:
        if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
            appearance.backgroundImage = [UIImage imageNamed:@""];
            appearance.backgroundImageContentMode = UIViewContentModeScaleAspectFill;
            self.navigationBar.standardAppearance = appearance;
        } else {
            [self.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
        }
    
    /// 关于navigationBar背景颜色更改及文字大小、颜色修改:
        if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
            [appearance configureWithOpaqueBackground];
            NSDictionary *normalTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIColor whiteColor],
                                                NSForegroundColorAttributeName,
                                                UIDEFAULTFONTSIZE(18),
                                                NSFontAttributeName,
                                                nil];
            appearance.titleTextAttributes = normalTextAttributes;
            appearance.backgroundColor = [UIColor orangeColor]; // 设置导航栏背景色
            appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]]; // 设置导航栏下边界分割线透明
            self.navigationBar.scrollEdgeAppearance = appearance; // 带scroll滑动的页面
            self.navigationBar.standardAppearance = appearance; // 常规页面。描述导航栏以标准高度显示时要使用的外观属性。
        } else {
            [self.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
            // 导航栏文字
            NSDictionary *normalTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIColor whiteColor],
                                                NSForegroundColorAttributeName,
                                                UIDEFAULTFONTSIZE(18),
                                                NSFontAttributeName,
                                                nil];
            [self.navigationBar setTitleTextAttributes:normalTextAttributes];
            self.navigationBar.barTintColor = [UIColor orangeColor];
            // 默认不透明
            self.navigationBar.translucent = NO;
            // 着色,让返回按钮图片渲染为白色
            self.navigationBar.tintColor = [UIColor whiteColor];
        }
    
    2. iOS TabBar处理同NavigationBar
    if (@available(iOS 15.0, *)) {
            NSDictionary* normalTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  UIColorFromRGB(0xA0A0A0),
                                                NSForegroundColorAttributeName,
                                                UIDEFAULTFONTSIZE(10),
                                                NSFontAttributeName,
                                                nil];
            NSDictionary *selectTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                UIColorFromRGB(0X308014),
                                                NSForegroundColorAttributeName,
                                                UIDEFAULTFONTSIZE(10),
                                                NSFontAttributeName,
                                                nil];
            UITabBarAppearance *barAppearance = [[UITabBarAppearance alloc] init];
            barAppearance.backgroundColor = UIColorFromRGB(0xF5F5F5);
            barAppearance.shadowImage = [UIImage imageWithColor:UIColorFromRGB(0xEEEEEE)];
            // 如果是隐藏系统导航栏,自定义导航栏的话,initTabBar方法中设置选中字体颜色在 iOS15同样生效,若用了系统导航栏,iOS15则需要如下方法设置tabBar字体颜色
            barAppearance.stackedLayoutAppearance.normal.titleTextAttributes = normalTextAttributes;
            barAppearance.stackedLayoutAppearance.selected.titleTextAttributes = selectTextAttributes;
            self.tabBar.scrollEdgeAppearance = barAppearance;
            self.tabBar.standardAppearance = barAppearance; // 与nav同理
            /**
             背景图片
             barAppearance.backgroundImage = [UIImage imageNamed:@""];
             barAppearance.backgroundImageContentMode = UIViewContentModeScaleAspectFill;
             self.tabBar.standardAppearance = barAppearance;
             */
        } else {
            [[UITabBar appearance] setBarTintColor:UIColorFromRGB(0xF5F5F5)];//这样写才能达到效果。
            [UITabBar appearance].translucent = NO;// 这句表示取消tabBar的透明效果。
            /**
             背景图片
             [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@""]];
             [UITabBar appearance].translucent = NO;
             */
        }
    
    3. UITableView新增TopPadding高度

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

    if (@available(iOS 15.0, *)) {
       _mineTableView.sectionHeaderTopPadding = 0;
    }
    
    4. UIImageWriteToSavedPhotosAlbum

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

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

    总结

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

    参考链接

    https://github.com/cy920820/Libstdc-.6.0.9-files lstdc++.6.0.9适配
    https://juejin.cn/post/6844903792958423053 iOS12适配及兼容问题解决
    https://www.jianshu.com/p/1803bd950b90 iOS14 隐私适配及部分解决方案
    https://juejin.cn/post/6912339107268329485 iOS 14 适配
    https://www.jianshu.com/p/3e1f0ce35bd5 iOS15 适配相关

    相关文章

      网友评论

        本文标题:iOS各系统版本适配总结

        本文链接:https://www.haomeiwen.com/subject/wtbmkrtx.html