iOS13适配注意事项

 

技术参考:

apple login

IOS13适配-详细

iOS 13 适配(持续更新中)

iOS13适配

掘金 iOS 13适配01

掘金 适配 iOS13

iOS 13 更新tips

View Controller Presentation Changes in iOS 13

1. Apple Login (2020年4月份)

附上官方Demo:下载地址

2. Dark Mode

2.1 暗黑模式 状态判断 && 切换监听

  • 判断模式状态
if (UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
    // 暗黑模式
}
else {
    // 正常模式
}
  • 监听模式切换
// 注意:参数为变化前的traitCollection
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection;

// 判断两个UITraitCollection对象是否不同
- (BOOL)hasDifferentColorAppearanceComparedToTraitCollection:(UITraitCollection *)traitCollection;

2.2 禁用暗黑模式

  • info.plist 内APP级别禁用暗黑模式

    Key: User Interface Style
    value: Light

  • widow级别的禁用暗黑模式

    if (@available(iOS 13.0, *)) {
        [UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    
  • ViewController级别禁用暗黑模式

    想全局禁用需要在 baseVC内全局禁用

    #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    - (UIUserInterfaceStyle)overrideUserInterfaceStyle{
        return UIUserInterfaceStyleLight;
    }
    #endif
    
  • view级别禁用暗黑模式

    view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    

2.3 适配暗黑模式颜色

参考这里 :iOS开发如何适配暗黑模式(Dark Mode)

+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
[UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trait) {
    if (trait.userInterfaceStyle == UIUserInterfaceStyleDark) {
        return UIColorRGB(0x000000);
    } else {
        return UIColorRGB(0xFFFFFF);
    }
 }];

3. present 半屏问题

modalPresentationStyle属性默认不是UIModalPresentationFullScreen了,需要根据需求手动设置。

LXNavigationViewController *nav = [[LXNavigationViewController alloc] initWithRootViewController:loginViewController];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nav animated:YES completion:nil];

  • 全局hook presentViewController方法

    + (void)hoookPresentFuc{
        [UIViewController aspect_hookSelector:@selector(presentViewController:animated:completion:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo){
            UIViewController *presentingVC =  (UIViewController *)aspectInfo.arguments.firstObject;
            presentingVC.modalPresentationStyle = UIModalPresentationFullScreen;
        } error:NULL];
    }
    

4. UITextField

  • 支付输入密码有问题

UITextField 的私有属性 _placeholderLabel 被禁止访问了

  • _placeholderLabel.textColor 私有属性被禁止访问
[self.textField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

崩溃信息:

'Access to UITextField's _placeholderLabel ivar is prohibited. 
This is an application bug' 
  • 解决方案
UITextField有个attributedPlaceholder的属性,我们可以自定义这个富文本来达到我们需要的结果。

NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;
  • 项目适配代码
if (@available(iOS 13.0, *)) {
    if (textField.attributedText.length > 0) {
        color = textField.attributedText.color;
    }
}else{
    color = [textField valueForKeyPath:@"_placeholderLabel.textColor"];
}

iOS 13 通过 KVC 方式修改私有属性,有 Crash 风险,谨慎使用!并不是所有KVC都会Crash,要尝试!

5. UITextView 同上

6. navigationBar 使用UINavigation+SXFixSpace

if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
    // 结构调整后 在这里return掉
    if (@available(iOS 13.0, *)) {
        if ([NSStringFromClass(subview.class) containsString:@"_UINavigationBarContentView"]) {
            return;
        }
    }

    //可修正iOS11之后的偏移
    subview.layoutMargins = UIEdgeInsetsMake(0, space, 0, space);break;
}

同时,下面的属性也已经被禁止访问了:

  • [barBgView valueForKey:@"_shadowView"];

  • [barBgView valueForKey:@"_backgroundEffectView"];

7. statusBar

  • UIStatusBarStyleDefault枚举值代表含义有变化

    typedef NS_ENUM(NSInteger, UIStatusBarStyle) {
    UIStatusBarStyleDefault                                  = 0, // 根据用户交互样式自动选择状态条样式
    UIStatusBarStyleLightContent     API_AVAILABLE(ios(7.0)) = 1, // Light content, for use on dark backgrounds
    UIStatusBarStyleDarkContent     API_AVAILABLE(ios(13.0)) = 3, // Dark content, for use on light backgrounds
    UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
    UIStatusBarStyleBlackOpaque      NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
    } API_UNAVAILABLE(tvos);
    
  • 定制状态条的样式 使用apple 推荐的这个系统方法

// ios 13.0 之后,这个方法已经失效了
// [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)){
        return UIStatusBarStyleDarkContent;
    }
    return UIStatusBarStyleDefault;
}

或者 ------>>:

UIStatusBarStyleDefault 替换为

(@available(iOS 13.0, *) ? UIStatusBarStyleDarkContent : UIStatusBarStyleDefault)

如果你还是使用的Xcode 10 为了走过编译器这一步,可以使用预编译命令,这么写:

#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    if (@available(iOS 13.0, *)) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDarkContent];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
#else
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
#endif

8. UITabbar

  • 横线
    UITabbar 层次发生改变,无法通过设置 shadowImage去掉上面的线;
  • 红点
    如果之前有通过TabBar上图片位置来设置红点位置,在iOS13上会发现显示位置都在最左边去了。遍历UITabBarButton的subViews发现只有在TabBar选中状态下才能取到UITabBarSwappableImageView,解决办法是修改为通过UITabBarButton的位置来设置红点的frame

9. UISearchBar

  • UISearchBar的私有属性UISearchBarBackground,禁止访问和remove
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
    if (@available(iOS 13.0, *)) {
        subview.backgroundColor = [UIColor lightGrayColor];
        // subview.layer.contents = nil;
    }else{
        [subview removeFromSuperview];
    }
}

10. 增加一直使用蓝牙的权限申请

info.plist文件下
Key: NSBluetoothAlwaysUsageDescription
Value: 我们要一直使用您的蓝牙,具体做什么别问我

11. 推送 获取deviceToken

#include <arpa/inet.h>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = [deviceToken bytes];
    NSString *hexToken = [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:%@",hexToken);
}

12. CNCopyCurrentNetworkInfo

 An app that fails to meet any of the above requirements receives the following return value:

- An app linked against iOS 12 or earlier receives a dictionary with pseudo-values. In this case, the SSID is Wi-Fi (or WLAN in the China region), and the BSSID is 00:00:00:00:00:00.
- An app linked against iOS 13 or later receives NULL.
- 

iOS13 以后只有开启了 Access WiFi Information capability,才能获取到 SSID 和 BSSID

参考:CNCopyCurrentNetworkInfo

13. APP 启动速度优化 导致frame获取失败

App启动过程中,部分View可能无法实时获取到frame。

14. WKWebView 中测量页面内容高度的方式变更

iOS 13以前 document.body.scrollHeight iOS 13中 document.documentElement.scrollHeight 两者相差55 应该是浏览器定义高度变了

15. fishhook 导致的Crash

github fishhook

16. MPMoviePlayerController 已经被弃用

'MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.' 

17. LaunchImage 即将消失

从 iOS 8 的时候,苹果就引入了 LaunchScreen,我们可以设置 LaunchScreen来作为启动页。当然,现在你还可以使用LaunchImage来设置启动图。不过使用LaunchImage的话,要求我们必须提供各种屏幕尺寸的启动图,来适配各种设备,随着苹果设备尺寸越来越多,这种方式显然不够 Flexible。而使用 LaunchScreen的话,情况会变的很简单, LaunchScreen是支持AutoLayout+SizeClass的,所以适配各种屏幕都不在话下。
注意啦⚠️,从2020年4月开始,所有使⽤ iOS13 SDK 的 App 将必须提供 LaunchScreen,LaunchImage即将退出历史舞台。

18. window层级变化

遍历视图获取 keywindow 的方式会失效。
新增了 screenWindow 自定义视图和 keywindow之间新增iOS 13系统的特有图层。

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值