美文网首页
UIDevice 常用参数

UIDevice 常用参数

作者: Zhang_yD | 来源:发表于2016-02-24 11:22 被阅读476次

有些APP设计的用户提交意见建议或者投诉信息是通过接口返回服务器,而有些设计的是通过邮件。或者程序在执行某些特定操作的时候,我们希望APP自动反馈回来一些有利于我们优化或者解决问题的信息。

不管是哪种情况哪种方式,在获取到用户信息的时候我们通常还希望获取到用户手机的一些信息。
这些信息存在哪里?当然不会在你婶婶的脑海里。

从UIDevice里面我们可以获取到不少设备方面的内容。
比如设备类型,系统版本,电池状态,屏幕状态等等等等。
下图是用模拟器运行了一些参数的显示结果


58F64A34-318E-46AB-9FFA-E584FAA0ADDD.png

其中设备方向和电池状态是枚举,有如下几种

UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,            // 竖屏,Home按键在下
UIDeviceOrientationPortraitUpsideDown,  // 竖屏,Home按键在上
UIDeviceOrientationLandscapeLeft,       // 横屏,Home按键在右
UIDeviceOrientationLandscapeRight,      // 横屏,Home按键在左
UIDeviceOrientationFaceUp,              // 屏幕面朝上
UIDeviceOrientationFaceDown             // 屏幕面朝下
 UIDeviceBatteryStateUnknown,     // 未知状态
 UIDeviceBatteryStateUnplugged,   // 电池使用中(未充电)
 UIDeviceBatteryStateCharging,    // 连接充电器,电量没达到100%
 UIDeviceBatteryStateFull,        // 连接充电器,电量达到100%

如果需要监控电池状态需要将batteryMonitoringEnabled设置为YES,默认是NO。

常用参数获取方式如下:

    // 设备名称 iPhone iPad iWatch...
    NSString * name = [UIDevice currentDevice].name;
    // 设备模型
    NSString * model = [UIDevice currentDevice].model;
    // 本地版本
    NSString * localizedModel = [UIDevice currentDevice].localizedModel;
    // 系统名称
    NSString * systemName = [UIDevice currentDevice].systemName;
    // 系统版本
    NSString * systemVersion = [UIDevice currentDevice].systemVersion;
    // 方向
    int orientation = [UIDevice currentDevice].orientation;
    // 设备标识符UUID
    NSUUID * uuid = [UIDevice currentDevice].identifierForVendor;
    NSString * uuidStr = uuid.UUIDString;
    // 电池状态
    int batteryState = [UIDevice currentDevice].batteryState;
    // 电池电量 0~1 -1表示未知
    float batteryLevel = [UIDevice currentDevice].batteryLevel;

除此之外,UIDevice还提供了4个通知用的Key

UIDeviceOrientationDidChangeNotification     // 设备方向
UIDeviceBatteryStateDidChangeNotification    // 电池状态
UIDeviceBatteryLevelDidChangeNotification    // 电池电量
UIDeviceProximityStateDidChangeNotification  // 接近状态

接近状态,就是比如用户接打电话时候靠近和离开手机屏幕的状态变化。

相关文章

网友评论

      本文标题:UIDevice 常用参数

      本文链接:https://www.haomeiwen.com/subject/vqhikttx.html