iOS App开发中通过UIDevice类获取设备信息的方法
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);