iOS获取设备信息

jopen 9年前

获取iOS设备信息需要用到UIDevice类,UIDevice.h文件定义了这些属性:


@property(nonatomic,readonly,retain) NSString    *name;              // e.g. "My iPhone"  @property(nonatomic,readonly,retain) NSString    *model;             // e.g. @"iPhone", @"iPod touch"  @property(nonatomic,readonly,retain) NSString    *localizedModel;    // localized version of model  @property(nonatomic,readonly,retain) NSString    *systemName;        // e.g. @"iOS"  @property(nonatomic,readonly,retain) NSString    *systemVersion;     // e.g. @"4.0"  @property(nonatomic,readonly) UIDeviceOrientation orientation;       // return current device orientation.  this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.    @property(nonatomic,readonly,retain) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);      // a UUID that may be used to uniquely identify the device, same across apps from a single vendor.    @property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications;    @property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0);  // default is NO  @property(nonatomic,readonly) UIDeviceBatteryState          batteryState NS_AVAILABLE_IOS(3_0);  // UIDeviceBatteryStateUnknown if monitoring disabled  @property(nonatomic,readonly) float                         batteryLevel NS_AVAILABLE_IOS(3_0);  // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown    @property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NO  @property(nonatomic,readonly)                            BOOL proximityState NS_AVAILABLE_IOS(3_0);  // always returns NO if no proximity detector    @property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);    @property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2);

下面通过简单例子介绍这些属性!


环境:Xcode 6.1,iOS 8.1

设备:iPhone 4s(iOS 7.1.1)

代码如下:


// 获取设备信息  - (void) getDevInfo  {      UIDevice* myDevice = [UIDevice currentDevice];      NSLog(@"设备名称 - %@", myDevice.name);      NSLog(@"设备模式 - %@", myDevice.model);      NSLog(@"设备本地模式 - %@", myDevice.localizedModel);      NSLog(@"系统名称 - %@", myDevice.systemName);      NSLog(@"系统版本 - %@", myDevice.systemVersion);        //    typedef NS_ENUM(NSInteger, UIDeviceOrientation) {  //        UIDeviceOrientationUnknown,  //        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom  //        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top  //        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right  //        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left  //        UIDeviceOrientationFaceUp,              // Device oriented flat, face up  //        UIDeviceOrientationFaceDown             // Device oriented flat, face down  //    };      NSLog(@"设备朝向 - %d", myDevice.orientation);  // 详见UIDeviceOrientation            NSLog(@"设备UUID - %@", myDevice.identifierForVendor);            // 当前设备是否有转向通知      NSLog(@"设备转向通知(BOOL) - %hhd", myDevice.generatesDeviceOrientationNotifications);            // 是否启动电池监控,默认为NO      NSLog(@"电池监控启用状态(BOOL) - %hhd", myDevice.batteryMonitoringEnabled);        //    typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {  //        UIDeviceBatteryStateUnknown,  //        UIDeviceBatteryStateUnplugged,   // on battery, discharging  //        UIDeviceBatteryStateCharging,    // plugged in, less than 100%  //        UIDeviceBatteryStateFull,        // plugged in, at 100%  //    };              // available in iPhone 3.0      NSLog(@"电池状态 - %d", myDevice.batteryState);  // 详见UIDeviceBatteryState            // 电量百分比, 0到1.0,如果电池状态为UIDeviceBatteryStateUnknown,则百分比为-1.0      NSLog(@"电池电量 - %f", myDevice.batteryLevel);            // 是否启动接近监控(比如脸接近手机屏),默认为NO      NSLog(@"接近监控启用状态(BOOL) - %hhd", myDevice.proximityMonitoringEnabled);            // 如果没有接近感应器,则返回NO      NSLog(@"接近状态(BOOL) - %hhd", myDevice.proximityState);            NSLog(@"是否支持多任务(BOOL) - %hhd", myDevice.multitaskingSupported);        //    typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {  //        UIUserInterfaceIdiomUnspecified = -1,  //#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED  //        UIUserInterfaceIdiomPhone,           // iPhone and iPod touch style UI  //        UIUserInterfaceIdiomPad,             // iPad style UI  //#endif  //    };      NSLog(@"用户界面模式 - %d", myDevice.userInterfaceIdiom);  // 详见UIUserInterfaceIdiom  }

打印结果:



2015-01-29 11:35:23.294 TestProject[261:60b] 设备名称 - iPhone  2015-01-29 11:35:23.297 TestProject[261:60b] 设备模式 - iPhone  2015-01-29 11:35:23.298 TestProject[261:60b] 设备本地模式 - iPhone  2015-01-29 11:35:23.300 TestProject[261:60b] 系统名称 - iPhone OS  2015-01-29 11:35:23.302 TestProject[261:60b] 系统版本 - 7.1.1  2015-01-29 11:35:23.303 TestProject[261:60b] 设备朝向 - 0  2015-01-29 11:35:23.311 TestProject[261:60b] 设备UUID - <__NSConcreteUUID 0x16565a90> 7737D97A-35F6-4C0E-B0EC-DF1D711D99E1  2015-01-29 11:35:23.313 TestProject[261:60b] 设备转向通知(BOOL) - 0  2015-01-29 11:35:23.314 TestProject[261:60b] 电池监控启用状态(BOOL) - 0  2015-01-29 11:35:23.316 TestProject[261:60b] 电池状态 - 0  2015-01-29 11:35:23.317 TestProject[261:60b] 电池电量 - -1.000000  2015-01-29 11:35:23.319 TestProject[261:60b] 接近监控启用状态(BOOL) - 0  2015-01-29 11:35:23.321 TestProject[261:60b] 接近状态(BOOL) - 0  2015-01-29 11:35:23.322 TestProject[261:60b] 是否支持多任务(BOOL) - 1  2015-01-29 11:35:23.324 TestProject[261:60b] 用户界面模式 - 0