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的屬性,我們可以自定義這個富文本來達到我們需要的結(jié)果。

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

iOS 13 通過 KVC 方式修改私有屬性,有 Crash 風險,謹慎使用!并不是所有KVC都會Crash,要嘗試!

坑二

控制器的 modalPresentationStyle 默認值變了

查閱了下 UIModalPresentationStyle枚舉定義,赫然發(fā)現(xiàn)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,
};

解決方案

如果你完全接受蘋果的這個默認效果,那就不需要去修改任何代碼。
如果,你原來就比較細心,已經(jīng)設(shè)置了modalPresentationStyle的值,那你也不會有這個影響。
對于想要找回原來默認交互的同學,直接設(shè)置如下即可:

self.modalPresentationStyle = UIModalPresentationOverFullScreen;

坑三

IOS13更改了[self presentViewController: animated: completion:] 更改了 不是全屏并且下滑動會崩潰

解決方案

self.modalPresentationStyle = UIModalPresentationFullScreen; //設(shè)置模式為全屏 如果滑動還有崩潰 設(shè)置animated為NO

坑四

MPMoviePlayerController 在iOS 13已經(jīng)不能用了

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

解決方案:

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

坑五

UITextField設(shè)置leftView 會出現(xiàn)圖片無法按照意圖顯示的問題。

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

然后在設(shè)置

坑六

iOS 13 DeviceToken有變化

NSString *dt = [deviceToken description];
dt = [dt stringByReplacingOccurrencesOfString: @"<" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @">" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @" " withString: @""];
這段代碼運行在 iOS 13 上已經(jīng)無法獲取到準確的DeviceToken字符串了,iOS 13 通過[deviceToken description]獲取到的內(nèi)容已經(jīng)變了。

解決方案

- (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 -提供第三方登錄的注意啦

如果你的應(yīng)用使用了第三方登錄,那么你可能也需要加下 「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,我們可以設(shè)置 LaunchScreen來作為啟動頁。當然,現(xiàn)在你還可以使用LaunchImage來設(shè)置啟動圖。不過使用LaunchImage的話,要求我們必須提供各種屏幕尺寸的啟動圖,來適配各種設(shè)備,隨著蘋果設(shè)備尺寸越來越多,這種方式顯然不夠 Flexible。而使用 LaunchScreen的話,情況會變的很簡單, LaunchScreen是支持AutoLayout+SizeClass的,所以適配各種屏幕都不在話下。
注意啦?,從2020年4月開始,所有使? iOS13 SDK的 App將必須提供 LaunchScreen,LaunchImage即將退出歷史舞臺。

坑九

KVC獲取狀態(tài)欄(_statusBar)會導(dǎo)致崩潰

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:// 沒有網(wǎng)絡(luò)
        {
            networkType = @"";
        }
            break;
        case TTReachableViaWiFi:// Wifi
        {
            networkType = @"Wi-Fi";
        }
            break;
        case TTReachableViaWWAN:// 手機自帶網(wǎng)絡(luò)
        {
            // 獲取手機網(wǎng)絡(luò)類型
            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;
}

坑十(謝謝網(wǎng)友提供)

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

網(wǎng)上這樣處理可以 
但是我發(fā)現(xiàn)其實是因為我的UMCCommon 這個太久了 pod update UMCCommon

坑十二

IOS13 使用AOP切面編程會報 TUICandidateView collectionView:didSelectItemAtIndexPath: unrecognized selector sent to instance 0x2802a4c60
發(fā)現(xiàn)我的項目中根本就沒有 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這個是系統(tǒng)的

目前暫時沒發(fā)現(xiàn)好的解決方案
    if([NSStringFromClass([delegate class]) isEqualToString:@"TUICandidateGrid"]){
        return;
    }

坑十三

IOS13使用暗黑模式,UIView,UITableview,UITextfile 默認背景色會變成暗黑色

解決方案: info.plist 加入

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

注意點:

  • xcode10 沒有這個屬性,如果是xcode10打包加入這個字段提交AppStore 會報錯,xcode10打包的話 不會受到暗黑模式影響
  • xcode11打包可以加上這個字段

歡迎大家補充

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 坑一 UITextField 的私有屬性 _placeholderLabel 被禁止訪問了 居然崩潰了,錯誤信息如...
    諶文閱讀 730評論 0 1
  • 對iOS13 適配的總結(jié),下面有些是自己遇到的,有些是收集的,希望能給大家一些思路 iOS13中presentVi...
    1劍天下閱讀 7,706評論 2 9
  • 這文章挺詳細,我轉(zhuǎn)載下來自己看的。 請點擊原文鏈接:http://m.itdecent.cn/p/46cd5...
    超_iOS閱讀 2,051評論 0 3
  • 坑一 UITextField 的私有屬性 _placeholderLabel 被禁止訪問了 居然崩潰了,錯誤信息如...
    Jackxu_q閱讀 1,473評論 1 0
  • crash 1.使用PHCachingImageManager獲取iCloud圖片會crash PHImageMa...
    wu大維閱讀 1,799評論 0 2

友情鏈接更多精彩內(nèi)容