IOS13适配-详细

坑一

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

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

坑二

控制器的 modalPresentationStyle 默认值变了

查阅了下 UIModalPresentationStyle枚举定义,赫然发现iOS 13新加了一个枚举值:
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,
};

解决方案

如果你完全接受苹果的这个默认效果,那就不需要去修改任何代码。
如果,你原来就比较细心,已经设置了modalPresentationStyle的值,那你也不会有这个影响。
对于想要找回原来默认交互的同学,直接设置如下即可:

self.modalPresentationStyle = UIModalPresentationOverFullScreen;

坑三

IOS13更改了[self presentViewController: animated: completion:] 更改了 不是全屏并且下滑动会崩溃

解决方案

self.modalPresentationStyle = UIModalPresentationFullScreen; //设置模式为全屏 如果滑动还有崩溃 设置animated为NO

坑四

MPMoviePlayerController 在iOS 13已经不能用了

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

解决方案:

既然不能再用了,那只能换掉了。替代方案就是AVKit里面的那套播放器。

坑五

UITextField设置leftView 会出现图片无法按照意图显示的问题。

// Confuse in beta4 iOS13
UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//search_icon  15*15
iconView.image = [UIImage imageNamed:@"icon"];
iconView.contentMode = UIViewContentModeCenter;
UITextField *file = [[UITextField alloc] init];
file.leftView = iconView;

解决方案

自定义UITextfile 前面增加间距
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    CGRect iconRect = [super leftViewRectForBounds:bounds];
    iconRect.origin.x += 8; //像右边偏15
    return iconRect;
}

//UITextField 文字与输入框的距离
- (CGRect)textRectForBounds:(CGRect)bounds{
    
    if (_margin>0) {
         return CGRectInset(bounds, _margin, 0);
    }else{
         return CGRectInset(bounds, 34, 0);
    }
    
   
    
}

//控制文本的位置
- (CGRect)editingRectForBounds:(CGRect)bounds{
    
    if (_margin>0) {
        return CGRectInset(bounds, _margin, 0);
    }else{
        return CGRectInset(bounds, 34, 0);
    }
}

然后在设置

坑六

iOS 13 DeviceToken有变化

NSString *dt = [deviceToken description];
dt = [dt stringByReplacingOccurrencesOfString: @"<" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @">" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @" " withString: @""];
这段代码运行在 iOS 13 上已经无法获取到准确的DeviceToken字符串了,iOS 13 通过[deviceToken description]获取到的内容已经变了。

解决方案

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

坑七

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:点我下载

坑八

即将废弃的 LaunchImage

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

坑九

KVC获取状态栏(_statusBar)会导致崩溃

UIApplication *app = [UIApplication sharedApplication]; if ([[app valueForKeyPath:@"_statusBar"] isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
NSArray *views = [[[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

解决方案

使用第三方 Reachability

+ (NSString *)getNetworkType
{
    
    NSString *networkType = @"Wi-Fi";
    WSXReachability *reachability   = [WSXReachability reachabilityForInternetConnection];
    TTNetworkStatus internetStatus = [reachability currentReachabilityStatus];
    switch (internetStatus) {
        case NotTTReachable:// 没有网络
        {
            networkType = @"";
        }
            break;
        case TTReachableViaWiFi:// Wifi
        {
            networkType = @"Wi-Fi";
        }
            break;
        case TTReachableViaWWAN:// 手机自带网络
        {
            // 获取手机网络类型
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            NSString *currentStatus = info.currentRadioAccessTechnology;
            if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
                networkType = @"GPRS";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
                networkType = @"2.75G EDGE";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
                networkType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
                networkType = @"3.5G HSDPA";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
                networkType = @"3.5G HSUPA";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
                networkType = @"2G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
                networkType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
                networkType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
                networkType = @"3G";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
                networkType = @"HRPD";
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
                networkType = @"4G";
            }
        }
            break;
        default:
            break;
    }
    return networkType;
}

坑十(谢谢网友提供)

KVC获取searchbar的_searchField会崩溃
解决方案

extension UISearchBar {
public func getSearchTextField() -> UITextField{
if #available(iOS 13.0, *) {
return self.searchTextField
}else {
return value(forKey: "_searchField") as! UITextField
}
}
}

坑十一

我项目运行的时候 崩溃了 ,但是我项目里面并没有_LSDefaults,报错如下:

 [_LSDefaults sharedInstance]: unrecognized selector sent to class 

解决方案:

@implementation NSObject (Extend)
+ (void)load{

SEL originalSelector = @selector(doesNotRecognizeSelector:);
SEL swizzledSelector = @selector(sw_doesNotRecognizeSelector:);

Method originalMethod = class_getClassMethod(self, originalSelector);
Method swizzledMethod = class_getClassMethod(self, swizzledSelector);

if(class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))){
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}

+ (void)sw_doesNotRecognizeSelector:(SEL)aSelector{
//处理 _LSDefaults 崩溃问题
if([[self description] isEqualToString:@"_LSDefaults"] && (aSelector == @selector(sharedInstance))){
//冷处理...
return;
}
[self sw_doesNotRecognizeSelector:aSelector];
}

网上这样处理可以 
但是我发现其实是因为我的UMCCommon 这个太久了 pod update UMCCommon

坑十二

IOS13 使用AOP切面编程会报 TUICandidateView collectionView:didSelectItemAtIndexPath: unrecognized selector sent to instance 0x2802a4c60
发现我的项目中根本就没有 TUICandidateView 这个View

解决方案

- (void)swiz_setDelegate:(id<UICollectionViewDelegate>)delegate {
    [self swiz_setDelegate:delegate];
    
    if([NSStringFromClass([delegate class]) isEqualToString:@"TUICandidateGrid"]){
        return;
    }
    
    if (delegate && [delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
        NSNumber *isHook = objc_getAssociatedObject(delegate, delegateCollectionViewIsHook);
        if (isHook == nil || ![isHook boolValue]) {
            @try {
                NSError *error = nil;
                [(NSObject *)delegate aspect_hookSelector:@selector(collectionView:didSelectItemAtIndexPath:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo) {
                    
                    TZUserStatistics<AOPLoggerClickProtocol> *aopLoggerEngine=(TZUserStatistics<AOPLoggerClickProtocol>*)[TZStatisticInterceptionManager sharedStatLogger];
                    if ([aopLoggerEngine respondsToSelector:@selector(alcp_collectionView:didSelectItemAtIndexPath:from:)]) {
                        [aopLoggerEngine alcp_collectionView:aspectInfo.arguments[0] didSelectItemAtIndexPath:aspectInfo.arguments[1] from:aspectInfo.instance];
                    }
                    
                } error:&error];
                objc_setAssociatedObject(delegate, delegateCollectionViewIsHook, @(YES), OBJC_ASSOCIATION_RETAIN);
            }
            @catch (NSException *exception) {
            }
            
        }
    }
}

判断代理当前的类是不是 TUICandidateGrid这个是系统的

目前暂时没发现好的解决方案
    if([NSStringFromClass([delegate class]) isEqualToString:@"TUICandidateGrid"]){
        return;
    }

坑十三

IOS13使用暗黑模式,UIView,UITableview,UITextfile 默认背景色会变成暗黑色

解决方案: info.plist 加入

<key>UIUserInterfaceStyle</key>
    <string>UIUserInterfaceStyleLight</string>

注意点:

  • xcode10 没有这个属性,如果是xcode10打包加入这个字段提交AppStore 会报错,xcode10打包的话 不会受到暗黑模式影响
  • xcode11打包可以加上这个字段

欢迎大家补充

推荐阅读更多精彩内容

  • 坑一 UITextField 的私有属性 _placeholderLabel 被禁止访问了 居然崩溃了,错误信息如...
    谌文阅读 473评论 0 1
  • 对iOS13 适配的总结,下面有些是自己遇到的,有些是收集的,希望能给大家一些思路 iOS13中presentVi...
    1剑天下阅读 6,151评论 2 8
  • 这文章挺详细,我转载下来自己看的。 请点击原文链接:https://www.jianshu.com/p/46cd5...
    超_iOS阅读 1,665评论 0 3
  • 坑一 UITextField 的私有属性 _placeholderLabel 被禁止访问了 居然崩溃了,错误信息如...
    Jackxu_q阅读 833评论 1 0
  • crash 1.使用PHCachingImageManager获取iCloud图片会crash PHImageMa...
    wu大维阅读 819评论 0 2