最新:iOS 13 适配

iOS 13 如期而至,虽然正式版还没出来,可是适配工做能够开展起来啦。在适配 iOS 13 过程当中,遇到了以下一些问题。ios

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

遇到的第一个崩溃是修改UITextFieldplaceholder的颜色,历史遗留代码以下:app

[_textField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

收到的错误信息⚠️学习

'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug'

那么这个问题如何处理呢?flex

其实,UITextField有个attributedPlaceholder的属性,咱们能够自定义这个富文原本达到咱们须要的结果。ui

修改以下:this

NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;

注意⚠️,iOS 13 经过 KVC 方式修改私有属性,有 Crush 风险,谨慎使用!code

2. 控制器的 modalPresentationStyle 默认值变了

对于这个变化,有点措手不及,直接修改了模态窗口的交互。 查阅了下 UIModalPresentationStyle枚举定义,赫然发现iOS 13新加了一个枚举值:orm

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationFormSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationCurrentContext API_AVAILABLE(ios(3.2)),
    UIModalPresentationCustom API_AVAILABLE(ios(7.0)),
    UIModalPresentationOverFullScreen API_AVAILABLE(ios(8.0)),
    UIModalPresentationOverCurrentContext API_AVAILABLE(ios(8.0)),
    UIModalPresentationPopover API_AVAILABLE(ios(8.0)) API_UNAVAILABLE(tvos),
    UIModalPresentationBlurOverFullScreen API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios) API_UNAVAILABLE(watchos),
    UIModalPresentationNone API_AVAILABLE(ios(7.0)) = -1,
    UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,
};

是的,就是UIModalPresentationAutomatic,苹果竟然直接将modalPresentationStyle默认值改为这个,有点不解,难道是怕咱们不知道新加了这个交互?这个也彻底违反了开闭原则吧😒。token

如何修改: 若是你彻底接受苹果的这个默认效果,那就不须要去修改任何代码。 若是,你原来就比较细心,已经设置了modalPresentationStyle的值,那你也不会有这个影响。 对于想要找回原来默认交互的同窗,直接设置以下便可:ip

self.modalPresentationStyle = UIModalPresentationOverFullScreen;

注意:UIModalPresentationOverFullScreen最低支持iOS 8,若是你还要支持iOS 8如下版本,那么你能够用UIModalPresentationFullScreen,这个两个值的交互略有些细微差异,具体的能够本身看下效果。

3. MPMoviePlayerController 在iOS 13已经不能用了

在使用到MPMoviePlayerController的地方,直接抛了异常:

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

如何修改: 这个没啥好说的,既然不能再用了,那只能换掉了。替代方案就是AVKit里面的那套播放器。

4. iOS 13 DeviceToken有变化‼️

这个很重要⚠️ 可能大多数使用第三方推送的童鞋都不会注意到这个问题,通常如今的第三方推送都是将DeviceToken原始数据丢进去,具体的解析都是第三方内部处理,因此,这些第三方解析DeviceToken的方式正确的话,那就毫无问题。若是大家是经过这种方式来获取DeviceToken,那你须要注意了。(这个坑也是多年前埋下的,不少文章介绍的也是下面这个方法,不规范的作法早晚要还的🤣),以下:

NSString *dt = [deviceToken description];
dt = [dt stringByReplacingOccurrencesOfString: @"<" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @">" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @" " withString: @""];

这段代码运行在 iOS 13 上已经没法获取到准确的DeviceToken字符串了,iOS 13 经过[deviceToken description]获取到的内容已经变了。

{length = 32, bytes = 0x778a7995 29f32fb6 74ba8167 b6bddb4e ... b4d6b95f 65ac4587 }

能够看到,跟原来咱们认识的那个已经彻底不同了。其实,形成这样的问题,主要仍是没有使用正确的方式来操做,下面是解决办法:

NSMutableString *deviceTokenString = [NSMutableString string];
const char *bytes = deviceToken.bytes;
NSInteger count = deviceToken.length;
for (int i = 0; i < count; i++) {
    [deviceTokenString appendFormat:@"%02x", bytes[i]&0x000000FF];
}

或者你也可使用极光提供的方法(2019年7月24日更新)

- (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);
}

5.Sign in with Apple (提供第三方登陆的注意啦⚠️)

若是你的应用使用了第三方登陆,那么你可能也须要加下 「Sign in with Apple」🤪

Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.

怎么作呢?网上已经有不少demo了,此处就不展开啦。 附上官方Demo:点我下载

6.即将废弃的 LaunchImage

从 iOS 8 的时候,苹果就引入了 LaunchScreen,咱们能够设置 LaunchScreen来做为启动页。固然,如今你还可使用LaunchImage来设置启动图。不过使用LaunchImage的话,要求咱们必须提供各类屏幕尺寸的启动图,来适配各类设备,随着苹果设备尺寸愈来愈多,这种方式显然不够 Flexible。而使用 LaunchScreen的话,状况会变的很简单, LaunchScreen是支持AutoLayout+SizeClass的,因此适配各类屏幕都不在话下。 注意啦⚠️,从2020年4月开始,全部使⽤ iOS13 SDKApp 将必须提供 LaunchScreenLaunchImage即将退出历史舞台。

7. Dark Mode

Apps on iOS 13 are expected to support dark mode Use system colors and materials Create your own dynamic colors and images Leverage flexible infrastructure

收录:原文地址

对于iOS 13 适配,有问题,想要更好的探讨,能够进入iOS技术群,一块儿探讨学习

相关文章
相关标签/搜索