iOS App development method to obtain device information through the UIDevice class

  • 2020-06-19 11:48:05
  • OfStack

UIDevice provides a variety of attributes, class functions, and status notifications to help us understand the full range of device health. What UIDevice does, from detecting battery power to locating devices and proximity sensing, is provide applications with some information about the user and the device. The UIDevice class also collects various details about the device, such as the model and the iOS version. Most of these attributes are positive auxiliaries to development efforts. The following code simply USES UIDevice to get the phone properties.

Simple example: Acquisition of device-related information
 NSString *strName = [[UIDevice currentDevice] name]; 
 NSLog(@"设备名称:%@", strName);//e.g. "My iPhone" 
  
 NSString *strId = [[UIDevice currentDevice] uniqueIdentifier]; 
 NSLog(@"设备唯1标识:%@", 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 

Common methods:
//获取当前设备单例
+ (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);

The device direction is enumerated as follows:
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // home键在下
    UIDeviceOrientationPortraitUpsideDown,  // home键在上
    UIDeviceOrientationLandscapeLeft,       // home键在右
    UIDeviceOrientationLandscapeRight,      // home键在左
    UIDeviceOrientationFaceUp,              // 屏幕朝上
    UIDeviceOrientationFaceDown             // 屏幕朝下
};

The enumeration of battery states is as follows:
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // 放电状态
    UIDeviceBatteryStateCharging,    // 充电未充满状态
    UIDeviceBatteryStateFull,        // 充电已充满
};

Here's how to monitor screen status:
//获取是否开启屏幕状态更改通知
@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications;
//开始监测通知
- (void)beginGeneratingDeviceOrientationNotifications;    
//结束监测通知
- (void)endGeneratingDeviceOrientationNotifications;

The following two amplifications are related to distance sensor applications
@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); //开启距离传感器
//是否触发了距离传感器
@property(nonatomic,readonly)                            BOOL proximityState

Relevant Notice:
//设备方向改变时发送的通知
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);


Related articles: