UIDevice提供了多種屬性、類函數及狀態通知,幫助我們全方位了解設備狀況。從檢測電池電量到定位設備與臨近感應,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);