Swift开发

iOS13 适配问题 看这一篇就够了

2019-11-03  本文已影响0人  浮生随笔

技术参考:

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 禁用暗黑模式

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];

4. UITextField

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

[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;
}

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

7. statusBar

// 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

9. UISearchBar

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系统的特有图层。

上一篇 下一篇

猜你喜欢

热点阅读