IOS UIDevice类


通过UIDevice类可以获取iOS设备的状态信息,例如设备的名称、操作系统版本号、UUID等基本信息,同时还能够获取诸如电池状态、设备朝向等信息,借助通知机制,当系统状态发生改变时,可以通知应用做出对应的响应动作。
某些代码在某些系统版本上无法运行,所以UIDevice类很有用。

获取设备的基本信息

在这里插入代码片

通过UIDevice类的对象,可以获取iOS设备如下的基本信息。

设备的名称,该名称可以在iOS设备的设置->通用->关于本机中设置,如shixin的iPhone6s

@property(nonatomic,readonly,strong) NSString    *name; 

设备的类型,例如.iPhone,iPod touch

@property(nonatomic,readonly,strong) NSString    *model;  
@property(nonatomic,readonly,strong) NSString    *localizedModel; // localized version of model

操作系统版本号. 如:8.3, 9.2,10.0等

@property(nonatomic,readonly,strong) NSString    *systemVersion;

每个iPhone或iPod Touch都有一个唯一的设备标识符(UUID),由40个字符或数字构成。通过UIDevice类可以获取到设备的UUID。

NSLog(@"identifierForVendor: %@", device.identifierForVendor.UUIDString);

获取电池信息batteryState

通过UIDevice类,可以获取当前iOS设备电池的相关信息,包括当前电池的状态(充电中、未充电、已充满)以及电池的当前电量。

UIDevice中与电池相关的属性

当需要获取iOS设备的电池信息时,需要首先打开batteryMonitoringEnabled属性,然后就可以通过batteryState以及batteryLevel来获取电池的状态信息。

是否开启电池监控,默认为NO

@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled ;

电池电量,取值0~1.0

@property(nonatomic,readonly) float  batteryLevel ; 

电池状态。其中,UIDeviceBatteryState有4种状态,分别为UIDeviceBatteryStateUnplugged(未充电)、UIDeviceBatteryStateCharging(充电中)、UIDeviceBatteryStateFull(已充满)、UIDeviceBatteryStateUnknown(未知状态)

@property(nonatomic,readonly) UIDeviceBatteryState  batteryState;

电池状态更新

在UIKit框架中,提供了用于电池状态变更时的通知(UIDeviceBatteryStateDidChangeNotification) , 当电池状态发生变更时,系统会给观察者发送通知。需要注意的是,在使用通知之前,需要预先在通知中心注册。

/获取电池信息
-(void)battery{
    UIDevice *device=[UIDevice currentDevice];
    //开启电池检测
    device.batteryMonitoringEnabled=YES;
    //注册通知,当电池状态改变时调用batteryStateChange方法
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(batteryStateChange) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}


- (void)dealloc
{
    //移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)batteryStateChange{
    UIDevice *device=[UIDevice currentDevice];
    //获取当前电量
    float batteryVolume=device.batteryLevel *100;
    //计算电池电量百分比
    NSString *batteryVolumeString=[NSString stringWithFormat:@"当前电量:%.0f%%",batteryVolume];
    NSLog(@"%@",batteryVolumeString);
    //根据电池状态切换时,给出提醒
    switch (device.batteryState) {
        case UIDeviceBatteryStateUnplugged:{
            //提醒
            NSString *string=[NSString stringWithFormat:@"未充电,%@",batteryVolumeString];
            [self showAlert:string];
            break;}
            
        case UIDeviceBatteryStateCharging:{
            //提醒
            NSString *string=[NSString stringWithFormat:@"充电中,%@",batteryVolumeString];
            [self showAlert:string];
            break;}
            
        case UIDeviceBatteryStateFull:{
            //提醒
            NSString *string=[NSString stringWithFormat:@"已充电,%@",batteryVolumeString];
            [self showAlert:string];
            break;}
        case UIDeviceBatteryStateUnknown:{
            [self showAlert:@"未知状态"];
            break;
        }
            
        default:
            break;
    }
}
-(void)showAlert:(NSString *)string{
    //警告框
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"时间" message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

接近传感器proximityState

在iPhone的正面顶部,提供了接近传感器。例如,在接听电话时,当用户的脸部接触手机顶部或者人为使用手指挡住iPhone顶部时,屏幕会自动的熄灭,从而避免误操作。通过UIDevice类中的proximityState属性可以获取当前接近传感器的状态。

接近传感器相关属性

在UIDevice类中,与接近传感器相关的属性如下所示。

接近传感器是否生效,默认情况下不生效

@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled;

获取接近传感器的工作状态

@property(nonatomic,readonly) BOOL proximityState;

系统提供的内置通知,当传感器工作状态发送改变时会触发通知

UIKIT_EXTERN NSString *const UIDeviceProximityStateDidChangeNotification;

例子

//接近传感器
-(void)proximity{
    UIDevice *device=[UIDevice currentDevice];
    //开启接近传感器
    device.proximityMonitoringEnabled=YES;
    //接近传感器通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
-(void)proximityStateChange{
    UIDevice *device=[UIDevice currentDevice];
    if (device.proximityState==YES) {
        NSLog(@"物体靠近");
    }else{
        NSLog(@"物体离开");
    }
}
- (void)dealloc
{
    //移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

方向传感器orientation

由于通过iPhone设备中的运动传感器,可以得到设备当前的加速度、磁力计以及陀螺仪数据,结合这些传感器提供的数据能够获取设备当前的方向信息。在UIDevice类中,对设备的方向信息进行了一次封装,不需要开发者通过计算传感器数据来获取设备朝向,而是直接通过UIDevice类中的orientation属性即可获取设备当前的朝向信息。

UIDevice中与方向相关的属性

UIDevice类中,通过orientation属性可以获知设备六个方向的信息,除了横竖屏四个方向之外,还可以获取到正反放置的信息。

获取设备朝向信息

@property(nonatomic,readonly) UIDeviceOrientation orientation;
/*UIDeviceOrientation枚举类型*/
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // 竖屏,正常状态
    UIDeviceOrientationPortraitUpsideDown,  // 竖屏,倒置
    UIDeviceOrientationLandscapeLeft,       // 向左横屏
    UIDeviceOrientationLandscapeRight,      // 向右横屏
    UIDeviceOrientationFaceUp,              //正面朝上
    UIDeviceOrientationFaceDown             // 正面朝下
} 

获取设备方向改变通知

UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;

当需要获取设备方向改变通知之前,需要提前调用beginGeneratingDeviceOrientationNotifications方法

-(void)beginGeneratingDeviceOrientationNotifications; 

不需要调用设备方向改变通知时,调用该方法关闭通知

- (void)endGeneratingDeviceOrientationNotifications;

举例

//方向传感器
-(void)orientation{
    UIDevice *device=[UIDevice currentDevice];
    //开启方向改变通知
    [device beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)orientationChange{
    UIDevice *device=[UIDevice currentDevice];
    NSString *string;
    switch (device.orientation) {
        case UIDeviceOrientationPortrait:{
            string= [NSString stringWithFormat:@"竖屏/正常"];
            break;}
        case UIDeviceOrientationPortraitUpsideDown:{
            string = [NSString stringWithFormat:@"竖屏/倒置"];
            break;}
        case UIDeviceOrientationLandscapeLeft:{
            string = [NSString stringWithFormat:@"横屏/左侧"];
            break;}
        case UIDeviceOrientationLandscapeRight:{
            string = [NSString stringWithFormat:@"横屏/右侧"];
            break;}
        case UIDeviceOrientationFaceUp:{
            string = [NSString stringWithFormat:@"正面朝上"];
            break;}
        case UIDeviceOrientationFaceDown:{
            string = [NSString stringWithFormat:@"正面朝下"];
            break;}
        default:{
            string = [NSString stringWithFormat:@"未知朝向"];
            break;}
    }
    NSLog(@"%@",string);
}
- (void)dealloc
{
    //移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

代码

https://github.com/ShaeZhuJiu/UIDevice_base.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值