Loading

iOS 13 适配(部分)

一、iOS 13特性

再写iOS 13系统适配之前,先来了解一下iOS 13系统对比之前版本系统有哪些不同的地方。文章不是主要写iOS 13系统特性,所以挑几点讲讲:

1. 隐私安全

App 位置获取权限

使用新增的精细控制选项,控制向 app 传送的位置数据。你可以选择仅允许某个 app 获取一次位置信息,或允许该 app 在使用期间随时获取这些信息。

App 对位置信息的使用透明化

当有 app 在后台使用你的位置信息时,你会收到通知,从而可以决定是否更改权限。

无线网络和蓝牙位置隐私保护增强
API 方面有所变化,并新增了控制选项,有助于在你使用无线网络和蓝牙时,防止 app 未经你同意而获取你的位置信息。

分享照片时提供位置信息控制

现在,分享照片时你可以控制是否分享位置信息。

2. 性能

App 启动速度更快

在 iOS 13 中,app 启动速度提升最高达 2 倍。

面容 ID 解锁速度更快

iPhone X、iPhone XS、iPhone XS Max 和 iPhone XR 的解锁速度提升最高达 30% 之多。

App 下载占用的容量更小

App Store 中的 app 将采用全新的封装方式,由此可使占用的容量缩小最高达一半8。

App 更新占用的容量更小

App 更新占用的容量将平均缩小 60% 之多。

3. 深色模式(暗黑模式)

墙纸

新的墙纸针对深色模式进行了优化,会随着你切换深色和浅色模式而自动变化。

系统级整合

深色模式与 iOS 系统全面整合,无论内置 app、各种设置还是系统级视图,都可使用精美的深色外观。

4、还有等等。。。

iOS 13 经过精心设计,将出色体验提升至全新境界。流畅的系统和许多交互上的体验,这是iOS受大家欢迎的原因之一。
话不多说,我们开始进入今天的话题吧。

二、iOS 13 适配

iOS 13 系统安装了管用梯App,但是软件崩溃打不开。xocde全局断点无法定位到问题的地方。
瞬间慌张,怎么办?百度,谷歌。
百度,谷歌不到怎么办?听天由命。
走吧,带你体验 iOS 13 适配之旅。

1. 推送

发现问题

后端无法正常向app推送消息

解决办法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = [deviceToken bytes];
    NSString *token = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    NSLog(@"deviceToken:%@", token);
}

问题原因

直接将 NSData 类型的 deviceToken 转换成 NSString 字符串,然后替换掉多余的符号。以上这个方法在 iOS 13已经失效了,需要进行一次数据格式处理。

2. UITextField

发现问题

上面说到 App 的崩溃问题,这里算一个。奔溃源之一是下面这一段代码。

// 设置提示文本颜色
[_passwordTextField setValue:RGBCOLOR(176, 176, 176) forKeyPath:@"_placeholderLabel.textColor"];

UITextField 设置 placeholder 的颜色会产生崩溃

解决办法

attributedPlaceholder 代替KVC方法 [setValue:forKeyPath:]

//设置提示文本颜色
_passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"pleaseInputPassword",nil) attributes:@{NSForegroundColorAttributeName:RGBCOLOR(176, 176, 176)}];

问题原因

iOS 13系统禁止 KVC 对系统 API 私有属性的设置,同理其他私有属性的读写建议也进行修改。

3. 暗黑模式

发现问题

处于暗黑模式下,界面出现 UI 颜色混乱。

解决办法

由于管用梯不做暗黑模式适配,问题处理起来比较简单。
Supporting Files 文件夹下的info.list 文件添加

key: User Interface Style
value : Light

问题原因

切换到暗黑模式下,如果没有设置字体颜色或者组件背景颜色,会默认与暗黑模式下的系统配色一致。

4. 蓝牙权限设置

发现问题

这里也是两个 App 崩溃源之一。因为我们的这两产品就是与蓝牙有关的,必须要获取蓝牙权限。

解决办法

Supporting Files 文件夹下的 info.list 文件添加

key: UPrivacy - Bluetooth Always Usage Description
value : ****需要使用蓝牙

代替

key: UPrivacy - Bluetooth Peripheral Usage Description
value : ****需要使用蓝牙

问题原因

由于iOS 13系统增加了蓝牙位置隐私安全保护,苹果将原来蓝牙申请权限用的 NSBluetoothPeripheralUsageDescription 字段,替换为 NSBluetoothAlwaysUsageDescription 字段。

以下是苹果官方开发文档给出的解释:

For apps with a deployment target of iOS 13 and later, use NSBluetoothAlwaysUsageDescription instead.

5. 模态界面默认样式改变

发现问题

在 iOS 13,使用 presentViewController 方式打开视图,会跟导航栏有部分视觉差,如下图

解决办法

在调用 [self presentViewController:xxvc animated:NO] 之前加上:

xxvc.modalPresentationStyle = UIModalPresentationFullScreen;

这里修改起来并不难,但是有些 ViewController 是改的第三方代码,导致找调用 presentViewController 方法比较难找,这个可以通过 Bug View Hierarchy 定位到问题 VC ,再去项目中查找该 VC。

问题原因

苹果将 UIViewControllermodalPresentationStyle 属性的默认值改成了新加的一个枚举值 UIModalPresentationAutomatic,对于多数 UIViewController,此值会映射成 UIModalPresentationPageSheet

6. Tabbar 文字丢失

发现问题

从别的界面切回首页,Tabbar上的文字丢失

解决办法

再设置 tabbar 样式的地方适配 iOS 13 的 tabbar 配置代码

// 适配 iOS 13 Tabbar样式
if (@available(iOS 13.0, *)) {
            UITabBarAppearance *appearance = UITabBarAppearance.new;
            [appearance setBackgroundImage:[self createImageWithColor:[UIColor colorWithRed:0.243 green:0.243 blue:0.243 alpha:1.000]]];

            UITabBarItemStateAppearance *normal = appearance.stackedLayoutAppearance.normal;
            if (normal) {
                normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.557 green:0.557 blue:0.557 alpha:1.000]};
            }
            
            UITabBarItemStateAppearance *selected = appearance.stackedLayoutAppearance.selected;
            if (selected) {
                selected.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.941 green:0.847 blue:0.604 alpha:1.000]};
            }
            tabBarController.tabBar.standardAppearance = appearance;
        } else {
            // set the text Attributes
            // 设置文字属性
            NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
            normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.557 green:0.557 blue:0.557 alpha:1.000];
            
            NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
            selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.941 green:0.847 blue:0.604 alpha:1.000];
            UITabBarItem *tabBar = [UITabBarItem appearance];
            [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
            [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    }

问题原因

iOS 13增加设置 tabbar、navigationbar 样式的 API standardAppearance

新增样式属性:
standardAppearance: 竖屏时导航栏外观。
compactAppearance: 横屏时导航栏外观。
scrollEdgeAppearance: 描述当关联的UIScrollView到达与导航栏邻接的边缘时导航栏的外观。

7. 如 SVProgressHUD 等第三方控件

发现问题

部分第三方控件再使用时候出现样式错乱,不显示等问题。

解决办法

pod install 安装该控件最新版本

问题原因

这里的原因可上github,再其issues下查找问题原因。

posted @ 2021-03-27 01:41  QiuZH's  阅读(232)  评论(0编辑  收藏  举报