iOS 13適配彙總

隨着iPhone 11的發佈,iOS 13適配也提上了日程,接下來就開發中升級iOS13的手機可能出現的問題
Xcode: 11.0
iOS : 13.0

UIViewController 模態彈出界面

viewController.present(presentVC, animated: true, completion: nil)
在調用模態彈出視圖,會發現彈出的界面沒有全屏。如圖
模態彈出界面
通過多次的嘗試,發現在低版本里面不會發生這種情況(iOS12及以下),於是我查閱了最新的開發文檔,發現了端倪,主要還是因爲我們之前忽略了UIViewController裏面的一個屬性,即:modalPresentationStyle

 Defines the presentation style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter.
 If this property has been set to UIModalPresentationAutomatic, reading it will always return a concrete presentation style. By default UIViewController resolves UIModalPresentationAutomatic to UIModalPresentationPageSheet, but other system-provided view controllers may resolve UIModalPresentationAutomatic to other concrete presentation styles.
 Defaults to UIModalPresentationAutomatic on iOS starting in iOS 13.0, and UIModalPresentationFullScreen on previous versions. Defaults to UIModalPresentationFullScreen on all other platforms.


public enum UIModalPresentationStyle : Int {
    case fullScreen
    
    @available(iOS 3.2, *)
    case pageSheet
    @available(iOS 3.2, *)
    case formSheet

    @available(iOS 3.2, *)
    case currentContext

    @available(iOS 7.0, *)
    case custom

    @available(iOS 8.0, *)
    case overFullScreen

    @available(iOS 8.0, *)
    case overCurrentContext

    @available(iOS 8.0, *)
    case popover

    
    @available(iOS 7.0, *)
    case none

    @available(iOS 13.0, *)
    case automatic
}

通過查看API 可以看到,iOS 13 新增一個:automatic類型,默認情況下就是這個所以纔會彈出不是全屏的界面。如果我們想要修改爲全屏的話
可以:presentVC.modalPresentationStyle = .fullScreen設置爲全屏即可

KVC 限制

iOS13以後已經不能肆無忌憚的通過 KVC來修改一些沒有暴露出來的屬性了。

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to xxx's _xxx ivar is prohibited. This is an application bug'

我們常用的有

// UITextField 的 _placeholderLabel
        let textField = UITextField.init()
        textField.setValue(UIColor.red, forKey: "_placeholderLabel.textColor")
        
        /// UISearchBar 的 _searchField
        [searchBar valueForKey:@"_searchField"]
        

下面方法替換

///分別設置字體大小和顏色(富文本)
textField.attributedPlaceholder = NSAttributedString.init(string: "請輸入....", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red], [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)])

 /// UISearchBar 用 searchField代替
bar.value(forKey: "searchField") as! UITextField

UISegmentedControl 默認樣式改變


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

UITabbar

如果之前有通過TabBar上圖片位置來設置紅點位置,在iOS13上會發現顯示位置都在最左邊去了。遍歷UITabBarButton的subViews發現只有在TabBar選中狀態下才能取到UITabBarSwappableImageView,解決辦法是修改爲通過UITabBarButton的位置來設置紅點的frame

App啓動過程中,部分View可能無法實時獲取到frame

// 只有等執行完 UIViewController 的 viewDidAppear 方法以後,才能獲取到正確的值,在viewDidLoad等地方 frame Size 爲 0,例如:
 UIApplication.shared.statusBarFrame

廢棄UIWebView

查看API可以看到:iOS 2.0 到 iOS 11.0
在12.0就已經被廢棄,部分APP使用webview時, 審覈被拒

@available(iOS, introduced: 2.0, deprecated: 12.0, message: "No longer supported; please adopt WKWebView.")
open class UIWebView : UIView, NSCoding, UIScrollViewDelegate {
	.........
	.........
	.........
}

CNCopyCurrentNetworkInfo

iOS13 以後只有開啓了 Access WiFi Information capability,才能獲取到 SSID 和 BSSID wi-fi or wlan 相關使用變更
最近收到了蘋果的郵件,說獲取WiFi SSID的接口CNCopyCurrentNetworkInfo 不再返回SSID的值。不仔細看還真會被嚇一跳,對物聯網的相關APP簡直是炸彈。仔細看郵件還好說明了可以先獲取用戶位置權限才能返回SSID。
注意:目本身已經打開位置權限,則可以直接獲取

- (NSString*) getWifiSsid {
    if (@available(iOS 13.0, *)) {
        //用戶明確拒絕,可以彈窗提示用戶到設置中手動打開權限
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
            NSLog(@"User has explicitly denied authorization for this application, or location services are disabled in Settings.");
            //使用下面接口可以打開當前應用的設置頁面
            //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            return nil;
        }
        CLLocationManager* cllocation = [[CLLocationManager alloc] init];
        if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
            //彈框提示用戶是否開啓位置權限
            [cllocation requestWhenInUseAuthorization];
            usleep(50);
            //遞歸等待用戶選選擇
            return [self getWifiSsidWithCallback:callback];
        }
    }
    NSString *wifiName = nil;
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    if (!wifiInterfaces) {
        return nil;
    }
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));

        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
            NSLog(@"network info -> %@", networkInfo);
            wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
            CFRelease(dictRef);
        }
    }
    CFRelease(wifiInterfaces);
    return wifiName;
}

打印:如下

 network info -> {
    BSSID = "44:dd:fb:43:91:ff";
    SSID = "Asus_c039";
    SSIDDATA = <41737573 5f633033 39>;
}
不同意
network info -> {
    BSSID = "00:00:00:00:00:00";
    SSID = WLAN;
    SSIDDATA = <574c414e>;
}

SceneDelegate

有關Xcode 11 新建工程創建SceneDelegate文件,可以參考我的這篇文章:iOS 13 SceneDelegate適配

iOS 13 暗黑模式

當我們系統開啓暗黑模式的時候,你會發現我們的有些界面白色界面變黑,黑色字體變白。
原因:我們沒有設置背景例如UITableviewCell背景,以及採用UILabel的默認背景黑色,當切換到暗黑模式的時候就會出現上述情況。
當我們還不打算適配暗黑模式的時候,解決方式1:

  1. 手動設置各個頁面背景。顯然不太好,麻煩還容易遺漏
  2. 在info.plist中添加一個配置文件就好了:User Interface Style設置爲Light

APP暗黑模式取消
**註釋:**關於如何優雅的適配暗黑模會在後續的博客中更新,敬請期待!
持續更新中…

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