如何確定當前 iPhone 機型資訊?


iOS 提供了一個 UIDevice 類,其中包含有關 iPhone 的所有資訊,這些資訊不會違反 Apple 制定的任何隱私法律。

使用 UIDevice,我們可以訪問以下資訊:

  • UIDevice.current.localizedModel - 返回模型的本地化版本

  • UIDevice.current.model - 返回當前裝置的型號,例如 @"iPhone"、@"iPod touch"

  • UIDevice.current.name - 返回正在使用的裝置的當前名稱,例如“我的 iPhone”

  • UIDevice.current.systemName - 返回系統名稱,例如 @"iOS"

  • UIDevice.current.systemVersion - 返回系統版本,例如 @"4.0"

  • UIDevice.current.batteryLevel - 返回電池電量,如果電量在 0 到 1 之間,則返回該值,否則如果狀態為 UIDeviceBatteryStateUnknown,則返回 -1.0。

  • UIDevice.current.batteryState - 返回電池狀態,根據 Apple API,它有四個可能的值。

public enum BatteryState : Int {
   case unknown
   case unplugged
   case charging
   case full
}

您可以在 View Controller 的 viewDidLoad 中編寫上述程式碼並列印以檢視結果。

print(UIDevice.current.model)
print(UIDevice.current.localizedModel)
print(UIDevice.current.systemVersion)

在 iPhone 7 plus 模擬器上執行時,它給出了以下結果。

iPhone
iPhone
12.0

以下是結果以及如何使用上述程式碼的示例。

更新於:2020-06-27

363 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告