iOS App开发中通过UIDevice类获取设备信息的方法

所属分类: 软件编程 / IOS 阅读数: 1293
收藏 0 赞 0 分享

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);

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多