您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

UIDevice 完全指南:讀取 iOS 設備信息、電池狀態與傳感器

編輯:iOS 相關教學

UIDevice 是 UIKit 中專門用來描述「當前設備」的單例類。它通過屬性、類方法和通知三種形式,把機型、系統版本、屏幕方向、電量、距離感應等信息一次性交給開發者。可以把它理解為應用與硬件之間的一層薄封裝。

基礎信息讀取

NSString *strName = [[UIDevice currentDevice] name];
NSLog(@"設備名稱:%@", strName);          // 例如 "My iPhone"

NSString *strSysName = [[UIDevice currentDevice] systemName];
NSLog(@"系統名稱:%@", strSysName);        // 例如 "iOS"

NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];
NSLog(@"系統版本:%@", strSysVersion);     // 例如 "17.0"

NSString *strModel = [[UIDevice currentDevice] model];
NSLog(@"設備型號:%@", strModel);          // 例如 "iPhone"、"iPod touch"

NSString *strLocModel = [[UIDevice currentDevice] localizedModel];
NSLog(@"本地化型號:%@", strLocModel);     // 隨系統語言變化

需要特別提醒的是,早期常用的 uniqueIdentifier(UDID)自 iOS 5 起已被蘋果禁用,不應再使用。現在的替代方案是 identifierForVendor:同一廠商下的 App 拿到相同的值,卸載全部應用後該值會重置。

常用屬性與方法

// 單例
+ (UIDevice *)currentDevice;

// 設備名稱
@property (nonatomic, readonly, strong) NSString *name;
// 設備型號
@property (nonatomic, readonly, strong) NSString *model;
// 本地化型號
@property (nonatomic, readonly, strong) NSString *localizedModel;
// 系統名稱與版本
@property (nonatomic, readonly, strong) NSString *systemName;
@property (nonatomic, readonly, strong) NSString *systemVersion;
// 設備方向
@property (nonatomic, readonly) UIDeviceOrientation orientation;
// 廠商標識
@property (nullable, nonatomic, readonly, strong) NSUUID *identifierForVendor;

// 電池監測開關,默認為 NO,必須先打開才能讀到有效值
@property (nonatomic, getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled;
// 電池狀態
@property (nonatomic, readonly) UIDeviceBatteryState batteryState;
// 電量,取值 0.0 ~ 1.0
@property (nonatomic, readonly) float batteryLevel;

設備方向枚舉

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,              // home 鍵在下
    UIDeviceOrientationPortraitUpsideDown,    // home 鍵在上
    UIDeviceOrientationLandscapeLeft,         // home 鍵在右
    UIDeviceOrientationLandscapeRight,        // home 鍵在左
    UIDeviceOrientationFaceUp,                // 屏幕朝上
    UIDeviceOrientationFaceDown               // 屏幕朝下
};

電池狀態枚舉

typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // 使用電池供電
    UIDeviceBatteryStateCharging,    // 充電中,未充滿
    UIDeviceBatteryStateFull         // 已充滿
};

完整的使用套路是:先打開監測開關,再註冊電量與狀態變化的通知,離開時關閉監測。

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
NSLog(@"電量 = %f, 狀態 = %ld",
      [[UIDevice currentDevice] batteryLevel],
      (long)[[UIDevice currentDevice] batteryState]);

方向通知的開與關

// 是否正在產生方向通知
@property (nonatomic, readonly, getter=isGeneratingDeviceOrientationNotifications)
    BOOL generatesDeviceOrientationNotifications;

- (void)beginGeneratingDeviceOrientationNotifications;
- (void)endGeneratingDeviceOrientationNotifications;

這組方法採用引用計數式的配對語義,多次 begin 需要對應多次 end,否則通知不會停止。

距離傳感器相關

@property (nonatomic, getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled;
@property (nonatomic, readonly) BOOL proximityState;

UIDevice 的通知一覽

// 設備方向改變
UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;
// 電池狀態改變(充電 / 放電 / 充滿)
UIKIT_EXTERN NSString *const UIDeviceBatteryStateDidChangeNotification;
// 電量改變
UIKIT_EXTERN NSString *const UIDeviceBatteryLevelDidChangeNotification;
// 距離感應狀態改變
UIKIT_EXTERN NSString *const UIDeviceProximityStateDidChangeNotification;

實戰注意事項

UIDevice 的信息並非全部可用於業務判斷。name 是用戶在系統設置裡自定義的手機名稱,不能作為設備唯一標識;model 返回的是「iPhone」這類粗粒度字符串,如果需要精確到 iPhone 15 Pro 這樣的具體機型,得走 sysctl 讀取utsname 的途徑。另外,電池相關的一切讀值都建立在 batteryMonitoringEnabled = YES 的前提上,這是新手最常忽略的一步。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved