iOS 13 適配 (填坑...)

1.私有API被封禁(KVC限制),禁止訪問.

iOS13中通過KVC方式來獲取私有屬性,有Carsh風險,儘量避免使用.比如我們常用的UITextFiled和UISearchController等,在iOS 13的searchbar添加了一個- (void)set_cancelButtonText:(NSString *)text方法,這個方法專門用來命中kvc,一旦命中就Crash。


//修改textField的佔位符字體顏色
[textField setValue:[UIColor xxx] forKeyPath:@"_placeholderLabel.textColor"];

(1).獲取SearchBar的textField

由於在13中把SearchBar中的textField直接暴露給開發者使用,無需在通過kvc獲取。


- (UITextField *)sa_GetSearchTextFiled{
    if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 13.0) {
        return self.searchTextField;
    }else{
        UITextField *searchTextField =  [self valueForKey:@"_searchField"];
        return searchTextField;
    }
}

(2).修改TextFiled的佔位符字體大小以及顏色,在iOS13中不能通過KVC來進行修改,可以通過其屬性字符串來進行修改

UITextField *textfield = [[UITextField alloc]init];
NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc]initWithString:textfield.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:12]}];
textfield.attributedPlaceholder = arrStr;



(3).獲取SearchBar的cancleButton,由於searcBar的層級發生變化以及對象的局部變量,因爲無法通過kvc的方式來獲取


if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 13.0) {
          for(id cc in [self.searchBar subviews]) {
              for (id zz in [cc subviews]) {
                  for (id gg in [zz subviews]) {
                      if([gg isKindOfClass:[UIButton class]]){
                          UIButton *cancelButton = (UIButton *)gg;
                          [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                      }
                  }
              }

          }
      }else{
          UIButton*cancelButton = (UIButton *)[self.searchBar getVarWithName:@"_cancelButton"];
          [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
      }



2.MPMoviePlayerController在iOS13中廢棄

MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.
在iOS13中對於MPMoviePlayerController使用的廢棄,需要使用AVKit中的AVPlayerViewController來達到播放的目的。

3.Sign in with Apple 第三方登錄

當 Sign In with Apple 服務正式上線以後,所有已接入其它第三方登錄的 App,Sign In with Apple 將被要求作爲一種登錄選擇,否則有可能就不給過。如果 APP 支持三方登陸(Facbook、Google、微信、QQ、支付寶等),就必須支持蘋果登錄,且要放前邊。解決方法:未來上線之後,添加登錄入口即可。

4.即將廢棄的 LaunchImage

從 iOS 8 的時候,蘋果就引入了 LaunchScreen,我們可以設置 LaunchScreen來作爲啓動頁。當然,現在你還可以使用LaunchImage來設置啓動圖。不過使用LaunchImage的話,要求我們必須提供各種屏幕尺寸的啓動圖,來適配各種設備,隨着蘋果設備尺寸越來越多,這種方式顯然不夠 Flexible。而使用 LaunchScreen的話,情況會變的很簡單, LaunchScreen是支持AutoLayout+SizeClass的,所以適配各種屏幕都不在話下。⚠️從2020年4月開始,所有使⽤ iOS13 SDK的 App將必須提供 LaunchScreen,LaunchImage即將退出歷史舞臺。可以使用Launch Storyboards來進行解決。

5.模態彈出默認交互改變

iOS 13 的 presentViewController 默認有視差效果,模態出來的界面現在默認都下滑返回。 一些頁面必須要點確認才能消失的,需要適配。如果項目中頁面高度全部是屏幕尺寸,那麼多出來的導航高度會出現問題。


// Swift
self.modalPresentationStyle = .fullScreen
// Objective-C
self.modalPresentationStyle = UIModalPresentationFullScreen;

UIViewController 增加一個了屬性 isModalInPresentation,默認爲 false,當該屬性爲 false 時,用戶下拉可以 dismiss 控制器,爲 true 時,下拉不可以 dismiss控制器。

6.UISegmentedControl 默認樣式改變

默認樣式變爲白底黑字,如果設置修改過顏色的話,頁面需要修改

7.增加一直使用藍牙的權限申請

CBCentralManager,iOS13以前,使用藍牙時可以直接用,不會出現權限提示,iOS13後,再使用就會提示了。在info.plist裏增加NSBluetoothAlwaysUsageDescription 我們要一直使用您的藍牙,具體做什麼別問我

8.廢棄 UISearchDisplayController

在 iOS 8 之前,我們在 UITableView 上添加搜索框需要使用 UISearchBar + UISearchDisplayController 的組合方式,而在 iOS 8 之後,蘋果就已經推出了 UISearchController 來代替這個組合方式。在 iOS 13 中,如果還繼續使用 UISearchDisplayController 會直接導致崩潰,

9.修改StatusBar的顏色

創建一個 UIView *_statusBar;
在頁面消失時置位nil,不影響其他頁面。
- (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 if (@available(iOS 13.0, *)) {
     [_statusBar removeFromSuperview];
     _statusBar = nil;
 }
}


if (@available(iOS 13.0, *)) {
     if (!_statusBar) {
         _statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
         [[UIApplication sharedApplication].keyWindow addSubview:_statusBar];
     }

 } else {
     
   _statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
}
 

 if ([_statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
     _statusBar.backgroundColor = color;
 }

未完待續…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章