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

2020-01-15 16:55:02王冬梅

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;