iOS开发笔记 --- UIDevice

2019-10-14  本文已影响0人  Rui_ai

一、 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;
//开启和监听 设备旋转的通知(不开启的话,设备方向一直是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(@"远距离");
    }
}
上一篇下一篇

猜你喜欢

热点阅读