支持Xcode10和適配iPhone XS Max、iPhone XR

timg.jpeg

目前我們項目已做了Xcode10(swift4.0)和新機型的適配,總結一下遇到的問題和修改的內容,希望幫助到其他人,如果您有不同的看法或遺漏,歡迎指出!

1.第三方庫編譯報錯

如果項目里用到了Mixpanel-swift和SwiftLint,這兩個在Xcode10上會報錯,目前作者已提交新版本分別是2.4.5和0.27.0,更新后即可解決報錯。

2.library not found for - lstdc++.6.0.9

pod工程編譯通過后會進行主工程的編譯,如果依賴了libstdc++.tbd和libstdc++.6.0.9.tbd,就會報這個error,原因是蘋果在XCode10和iOS12中移除了libstdc++這個庫,由libc++這個庫取而代之,蘋果的解釋是libstdc++已經標記為廢棄有5年了,建議大家使用經過了llvm優化過并且全面支持C++11的libc++庫。

臨時的解決方法就是把libstdc++.6.0.9.tbd這個文件導入到Xcode10中,分別放到以下目錄 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/ 和 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/ 這時編譯可以通過。

但這只是臨時的解決方案,如果你自己的業務模塊使用了libstdc++,那么就把模塊代碼重新調整為依賴libc++,然后重新檢查是否存在問題,重新編譯。如果你引用的第三方廠商提供的sdk中依賴了libstdc++,那么抓緊聯系廠商,要求版本升級。

3.Enum case '...' not found in type '...'

解決好上面兩個報錯,編譯程序時還會顯示這個error,具體場景如下:

PosVisitQuestionType: String {
    case text
    case textArea = "text_area"
    case dropDownList = "drop_down_list"
    case radioButton = "radio_button"
}
let type: PosVisitQuestionType!
...
switch type {
case .text, .textArea:
    errorText = NSLocalizedString("Please enter the following options", comment: "")
case .dropDownList, .radioButton:
    errorText = NSLocalizedString("Click the right button to get current location", comment: "")
default:
    break
}

Xcode10建議每個case 情況下加“?”

屏幕快照 2018-09-20 下午4.24.46.png

原因可能是 type是可選的,所以每個case情況要與type類型保持一致,所以提示加 “?”,可能是Xcode10編譯器更新的原因。

修改的方法是如果確定type會被賦值,那在定義的時候就把“!”去掉,如果不確定type是否有值就按照Xcode提示修改。

4.適配iPhone XS Max、iPhone XR

我們項目在獲取機型等信息用的是DeviceKit這個第三方庫,所以也需要更新一下才能獲取到新機型的信息,最新版是1.8.1。在最新版有這樣一個變量

/// All Face ID Capable Devices
    static public var allFaceIDCapableDevices: [Device] {
      return [.iPhoneX, .iPhoneXs, .iPhoneXsMax, .iPhoneXr]
    }

由于iPhone X、iPhone XS、iPhone XS Max、iPhone XR這些機型的navigationBar高度以及tabBar高度都一致,所以可以用allFaceIDCapableDevices是否包含當前設備,來判斷當前設備是否有“齊劉海”。

示例:

static let faceIDDeviceArray = Device.allFaceIDCapableDevices

static let navigationHeight: CGFloat = {
        if faceIDDeviceArray.contains(currentDevice) {
            return faceIDDeviceNavHeight
        } else {
            return ordinaryDeviceNavHeight
        }
    }()

同時DeviceKit中也提供這樣一個方法,運行模擬器的時候調用,也會返回真實的設備名稱

/// Get the real device from a device. If the device is a an iPhone8Plus simulator this function returns .iPhone8Plus (the real device).
    /// If the parameter is a real device, this function returns just that passed parameter.
    ///
    /// - parameter device: A device.
    ///
    /// - returns: the underlying device If the `device` is a `simulator`,
    /// otherwise return the `device`.
    public static func realDevice(from device: DeviceKit.Device) -> DeviceKit.Device

示例:

static let currentDevice = Device.realDevice(from: Device())
if currentDevice == .iPhoneX {}
// 取代以下寫法
if Device() == .iPhoneX || Device() == .simulator(.iPhoneX) {}

最后別忘了再切兩張啟動圖,因為iPhone XS和尺寸和iPhone X是一樣的,所以iPhone XS可以忽略

iPhone XR:828px x 1792px

iPhone XS Max: 1242px x 2688px

參考鏈接

謝謝各位,歡迎指教!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1info.plist沖突 Xcode10 構建方法做了調整,導致編譯報info.plist的錯誤。 解決方法:f...
    VicoV閱讀 2,300評論 0 3
  • iOS 12系統目前看來并沒有多大變化.不像iOS 11那樣增加了新的安全區域概念.在iOS 12中新機型的安全區...
    香橙柚子閱讀 7,231評論 5 15
  • 一、 libstdc++ 庫項目用到了c++,依賴了libstdc++.tbd和libstdc++.6.0.9.t...
    執著攀攀閱讀 5,004評論 1 9
  • 9.18更新:經測試發現iOS12修改了非劉海屏safeArea的值! 拿iPhone6豎屏情況下為例iOS11中...
    zolobdz閱讀 18,013評論 9 21
  • 可能之前吃了不少上火的食物,結果昨天晚上一次性爆發了,牙齦紅腫疼痛好久,不管是吃藥、喝水、消炎都不怎么頂用,折騰了...
    潔_寞碎閱讀 133評論 0 0