一、 UIDevice 简介
UIDevice类提供了一个单例实例代表当前的设备。从这个实例中可以获得的信息设备,比如操作系统名称、电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)。
二、UIDevice 使用
1、获取 UIDevice 实例
UIDevice *device = [UIDevice currentDevice];
2、UIDevice 常用属性
通过 UIDevice
相关属性,可用获取设备信息
//获取当前设备名称
@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);
//是否支持多任务
@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);
3、UIDevice 通知
UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:
1. 设备旋转
UIDeviceOrientationDidChangeNotification
2. 电池状态改变
UIDeviceBatteryStateDidChangeNotification
3. 电池电量改变
UIDeviceBatteryLevelDidChangeNotification
4. 近距离传感器(比如设备贴近了使用者的脸部)
UIDeviceProximityStateDidChangeNotification
三、UIDevice 通知的应用
1、监测屏幕旋转
- 屏幕方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown, //未知
UIDeviceOrientationPortrait, //home键在下
UIDeviceOrientationPortraitUpsideDown, //home键在上
UIDeviceOrientationLandscapeLeft, //home键在右
UIDeviceOrientationLandscapeRight, //home键在左
UIDeviceOrientationFaceUp, //屏幕朝上
UIDeviceOrientationFaceDown //屏幕朝下
} __TVOS_PROHIBITED;
- 读取屏幕方向
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
- 开启和监听屏幕旋转的通知
当设备方向变化时候,发出UIDeviceOrientationDidChangeNotification
通知;注册监听该通知,可以针对不同的设备方向处理视图展示。
//开启和监听 设备旋转的通知(不开启的话,设备方向一直是UIInterfaceOrientationUnknown)
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
- 屏幕旋转处理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
switch (deviceOrientation) {
case UIDeviceOrientationPortrait:
NSLog(@"home键在下");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"home键在上");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"home键在右");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"home键在左");
break;
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕朝上");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕朝下");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知方向");
break;
default:
NSLog(@"无法辨识");
break;
}
}
- 销毁通知
//销毁 设备旋转 通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil
];
//结束 设备旋转通知
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
2、监测电池信息
- 电池状态的类型枚举
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged, // 放电状态
UIDeviceBatteryStateCharging, // 充电未充满状态
UIDeviceBatteryStateFull, // 充电已充满
};
- 获取电池状态
NSLog(@"电池状态 %zi",[UIDevice currentDevice].batteryState);
- 获取电池电量百分比
NSLog(@"电池电量等级百分比0-1 : %f",[UIDevice currentDevice].batteryLevel);
- 获取电池状态的时候,需要先把监视电池的功能打开,然后再开始检查状态
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
NSLog(@"%f",[UIDevice currentDevice].batteryLevel);
- 添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateDidChange) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelDidChange) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
3、距离传感器
手机距离传感器的作用就像比如接电话的时候靠近耳朵的话,手机熄灭屏幕等。
首先需要打开手机距离传感器
[UIDevice currentDevice].proximityMonitoringEnabled=YES;
然后添加当距离开始变化的时候的通知和函数调用即可
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice) name:UIDeviceProximityStateDidChangeNotification object:nil];
-(void)notice{
if ([UIDevice currentDevice].proximityState) {
NSLog(@"近距离");
}else{
NSLog(@"远距离");
}
}
网友评论