[IOS开发教程] iOS获取设备信息

北风网AD

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

  1. @property(nonatomic,readonly,retain) NSString *name; // e.g. "My iPhone"
  2. @property(nonatomic,readonly,retain) NSString *model; // e.g. @"iPhone", @"iPod touch"
  3. @property(nonatomic,readonly,retain) NSString *localizedModel; // localized version of modelhttp://www.kmnk03.com/hxpfk/qcd/392.html
  4. @property(nonatomic,readonly,retain) NSString *systemName; // e.g. @"iOS"
  5. @property(nonatomic,readonly,retain) NSString *systemVersion; // e.g. @"4.0"
  6. @property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation. this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
  7. @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.http://www.kmnk03.com/hxpfk/qcd/393.html
  8. @property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications;
  9. @property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NO
  10. @property(nonatomic,readonly) UIDeviceBatteryState batteryState NS_AVAILABLE_IOS(3_0); // UIDeviceBatteryStateUnknown if monitoring disabled
  11. @property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0); // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknownhttp://www.kmnk03.com/hxpfk/qcd/394.html
  12. @property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NOhttp://www.kmnk03.com/hxpfk/qcd/395.html
  13. @property(nonatomic,readonly) BOOL proximityState NS_AVAILABLE_IOS(3_0); // always returns NO if no proximity detector
  14. @property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);
  15. @property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2);
复制代码

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

环境:Xcode 6.1,iOS 8.1

设备:iPhone 4s(iOS 7.1.1)

代码如下:

  1. // 获取设备信息
  2. - (void) getDevInfo
  3. {http://www.kmnk03.com/hxpfk/qcd/396.html
  4. UIDevice* myDevice = [UIDevice currentDevice];
  5. NSLog(@"设备名称 - %@", myDevice.name);
  6. NSLog(@"设备模式 - %@", myDevice.model);
  7. NSLog(@"设备本地模式 - %@", myDevice.localizedModel);
  8. NSLog(@"系统名称 - %@", myDevice.systemName);
  9. NSLog(@"系统版本 - %@", myDevice.systemVersion);
  10. // typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
  11. // UIDeviceOrientationUnknown,http://www.kmnk03.com/hxpfk/qcd/397.html
  12. // UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
  13. // UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
  14. // UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
  15. // UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
  16. // UIDeviceOrientationFaceUp, // Device oriented flat, face up
  17. // UIDeviceOrientationFaceDown // Device oriented flat, face down
  18. // };http://www.kmnk03.com/hxpfk/qcd/398.html
  19. NSLog(@"设备朝向 - %d", myDevice.orientation); // 详见UIDeviceOrientation
  20. NSLog(@"设备UUID - %@", myDevice.identifierForVendor);
  21. // 当前设备是否有转向通知
  22. NSLog(@"设备转向通知(BOOL) - %hhd", myDevice.generatesDeviceOrientationNotifications);
  23. // 是否启动电池监控,默认为NO
  24. NSLog(@"电池监控启用状态(BOOL) - %hhd", myDevice.batteryMonitoringEnabled);
  25. // typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
  26. // UIDeviceBatteryStateUnknown,http://www.kmnk03.com/hxpfk/qcd/399.html
  27. // UIDeviceBatteryStateUnplugged, // on battery, discharging
  28. // UIDeviceBatteryStateCharging, // plugged in, less than 100%
  29. // UIDeviceBatteryStateFull, // plugged in, at 100%
  30. // }; // available in iPhone 3.0
  31. NSLog(@"电池状态 - %d", myDevice.batteryState); // 详见UIDeviceBatteryState
  32. // 电量百分比, 0到1.0,如果电池状态为UIDeviceBatteryStateUnknown,则百分比为-1.0
  33. NSLog(@"电池电量 - %f", myDevice.batteryLevel);
  34. // 是否启动接近监控(比如脸接近手机屏),默认为NO
  35. NSLog(@"接近监控启用状态(BOOL) - %hhd", myDevice.proximityMonitoringEnabled);
  36. // 如果没有接近感应器,则返回NO
  37. NSLog(@"接近状态(BOOL) - %hhd", myDevice.proximityState);
  38. NSLog(@"是否支持多任务(BOOL) -http://www.kmnk03.com/hxpfk/qcd/400.html %hhd", myDevice.multitaskingSupported);
  39. // typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
  40. // UIUserInterfaceIdiomUnspecified = -1,
  41. //#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
  42. // UIUserInterfaceIdiomPhone, // iPhone and iPod touch style UI
  43. // UIUserInterfaceIdiomPad, // iPad style UI
  44. //#endif
  45. // };
  46. NSLog(@"用户界面模式 - %d", myDevice.userInterfaceIdiom); // 详见UIUserInterfaceIdiom
  47. }
复制代码

打印结果:
  1. 2015-01-29 11:35:23.294 TestProject[261:60b] 设备名称 - iPhone
  2. 2015-01-29 11:35:23.297 TestProject[261:60b] 设备模式 - iPhone
  3. 2015-01-29 11:35:23.298 TestProject[261:60b] 设备本地模式 - iPhone
  4. 2015-01-29 11:35:23.300 TestProject[261:60b] 系统名称 - iPhone OS
  5. 2015-01-29 11:35:23.302 TestProject[261:60b] 系统版本 - 7.1.1
  6. 2015-01-29 11:35:23.303 TestProject[261:60b] 设备朝向 - 0
  7. 2015-01-29 11:35:23.311 TestProject[261:60b] 设备UUID - <__NSConcreteUUID 0x16565a90> 7737D97A-35F6-4C0E-B0EC-DF1D711D99E1http://www.kmnk03.com/hxpfk/qcd/401.html
  8. 2015-01-29 11:35:23.313 TestProject[261:60b] 设备转向通知(BOOL) - 0
  9. 2015-01-29 11:35:23.314 TestProject[261:60b] 电池监控启用状态(BOOL) - 0
  10. 2015-01-29 11:35:23.316 TestProject[261:60b] 电池状态 - 0
  11. 2015-01-29 11:35:23.317 TestProject[261:60b] 电池电量 - -1.000000
  12. 2015-01-29 11:35:23.319 TestProject[261:60b] 接近监控启用状态(BOOL) - 0
  13. 2015-01-29 11:35:23.321 TestProject[261:60b] 接近状态(BOOL) - 0
  14. 2015-01-29 11:35:23.322 TestProject[261:60b] 是否支持多任务(BOOL) - 1
  15. 2015-01-29 11:35:23.324 TestProject[261:60b] 用户界面模式 - 0
  16. kmnk03.com
    www.kmnk03.c0m
展开阅读全文

没有更多推荐了,返回首页