您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

透過 UIDevice 類取得 iOS 設備資訊的方法詳解

編輯:iOS 開發綜合

UIDevice 提供了多種屬性、類函式及狀態通知,幫助我們全方位了解設備狀況。從偵測電池電量到定位設備方向與臨近感應,UIDevice 所做的工作就是為應用程式提供使用者與設備的相關資訊。此外,它還能收集設備的各種具體細節,例如機型與 iOS 版本等,其中大部分屬性對開發工作都具有正面的輔助作用。下面的程式碼示範了如何以 UIDevice 取得手機屬性。

簡單示例:設備相關資訊的取得

NSString *strName = [[UIDevice currentDevice] name]; NSLog(@"設備名稱:%@", strName);//e.g. "My iPhone" NSString *strId = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@"設備唯一標識:%@", strId);//UUID,5.0 後不可用 NSString *strSysName = [[UIDevice currentDevice] systemName]; NSLog(@"系統名稱:%@", strSysName);// e.g. @"iOS" NSString *strSysVersion = [[UIDevice currentDevice] systemVersion]; NSLog(@"系統版本號:%@", strSysVersion);// e.g. @"4.0" NSString *strModel = [[UIDevice currentDevice] model]; NSLog(@"設備模式:%@", strModel);// e.g. @"iPhone", @"iPod touch" NSString *strLocModel = [[UIDevice currentDevice] localizedModel]; NSLog(@"本地設備模式:%@", strLocModel);// localized version of model

常用方法列舉:

//取得當前設備單例 + (UIDevice *)currentDevice; //取得當前設備名稱 @property(nonatomic,readonly,strong) NSString *name; // e.g. "My iPhone" //取得當前設備模式 @property(nonatomic,readonly,strong) NSString *model; // e.g. @"iPhone", @"iPod touch" //取得本地化的當前設備模式 @property(nonatomic,readonly,strong) NSString *localizedModel; // localized version of model //取得系統名稱 @property(nonatomic,readonly,strong) NSString *systemName; // e.g. @"iOS" //取得系統版本 @property(nonatomic,readonly,strong) NSString *systemVersion; // e.g. @"4.0" //取得設備方向 @property(nonatomic,readonly) UIDeviceOrientation orientation; //取得設備 UUID 物件 @property(nullable, nonatomic,readonly,strong) NSUUID *identifierForVendor; //是否開啟監測電池狀態,開啟後才可以正常取得電池狀態 @property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NO //取得電池狀態 @property(nonatomic,readonly) UIDeviceBatteryState batteryState NS_AVAILABLE_IOS(3_0); //取得電量 @property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0);

設備方向的列舉如下:

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 // 充電已充滿 };

下面的方法關於監測螢幕狀態:

//取得是否開啟螢幕狀態更改通知 @property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications; //開始監測通知 - (void)beginGeneratingDeviceOrientationNotifications; //結束監測通知 - (void)endGeneratingDeviceOrientationNotifications;

下面兩個屬性與距離感測器應用相關:

@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); //開啟距離感測器 //是否觸發了距離感測器 @property(nonatomic,readonly) BOOL proximityState

相關通知:

//設備方向改變時送出的通知 UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification; //電池狀態改變時送出的通知 UIKIT_EXTERN NSString *const UIDeviceBatteryStateDidChangeNotification NS_AVAILABLE_IOS(3_0); //電量改變時送出的通知 UIKIT_EXTERN NSString *const UIDeviceBatteryLevelDidChangeNotification NS_AVAILABLE_IOS(3_0); //距離感測器狀態改變時送出的通知 UIKIT_EXTERN NSString *const UIDeviceProximityStateDidChangeNotification NS_AVAILABLE_IOS(3_0);

總的來說,UIDevice 涵蓋了設備名稱、系統版本、螢幕方向、電池狀態、電量與距離感測等豐富資訊,並搭配對應的狀態通知,讓開發者能即時掌握設備變化並做出反應。使用電池相關資訊前,別忘了先將 batteryMonitoringEnabled 設為 YES;若要監聽方向變化,也需先呼叫 beginGeneratingDeviceOrientationNotifications 開啟監測。

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