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);