iOS App开发中通过UIDevice类获取设备信息的方法
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);