【最新iOS获取设备信息】:UIDevice,NSBundle,NSLocale

iOS开发中获取设备的信息指的是像设备号、应用名称以及国家语言等非用户隐私信息,这些信息多数我们都可以在开发时在Xcode工程中就可以看到,同时我们可以利用提供的UIDevice,NSBundle,NSLocale三个类来获取一些我们开发中常用到的信息。


这里写图片描述

UIDevice

UIDevice可以帮我们获取移动设备的基本信息:设备名称、设备模式、系统名称、系统版本、设备唯一标识符以及设备方向和设备电量等等。

其中的设备方向是个枚举变量UIDeviceOrientation(TV中禁止使用),源代码中的定义为:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // 设备垂直,home键在下   
    UIDeviceOrientationPortraitUpsideDown,  // 设备翻转,home键在上
    UIDeviceOrientationLandscapeLeft,       // 设备水平,home件在左
    UIDeviceOrientationLandscapeRight,      // 设备水平,home件在右
    UIDeviceOrientationFaceUp,              // 设备平放,面朝上    
    UIDeviceOrientationFaceDown             // 设备平放,面朝下
} __TVOS_PROHIBITED;

UIDevice中的信息在SDK源码中定义如下:

@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;    // 本地化的设备模式
@property(nonatomic,readonly,strong) NSString    *systemName;        // e.g. @"iOS"
@property(nonatomic,readonly,strong) NSString    *systemVersion;     // e.g. @"4.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation __TVOS_PROHIBITED;       
// 返回当前的设备方向,如果还没有产生设备方向通知(设备方向未发生改变)的话返回UIDeviceOrientationUnknown

@property(nullable, nonatomic,readonly,strong) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);      
// 可用于唯一标识设备的一个UUID

测试实例代码:

// 1.UIDevice
UIDevice *device = [UIDevice currentDevice]; // 获取当前的device

/** 设备基本信息 **/
NSString *name = [device name];// @"iPhone 5S"
NSString *model = [device model];// @"iPhone"
NSString *localizedModel = [device localizedModel];// @"iPhone"
NSString *systemName = [device systemName];// @"iPhone OS"
NSString *systemVersion = [device systemVersion];// @"9.3.2"

/** 设备方向 **/
UIDeviceOrientation orientation = [device orientation]; // UIDeviceOrientationUnknown

/** 设备电量 **/
device.batteryMonitoringEnabled = YES; // 开启电量监听
float batteryLevel = [UIDevice currentDevice].batteryLevel; // 0~1.0对应电量0~100%,-1.0表示电量未知
device.batteryMonitoringEnabled = NO; // 关闭电量监听

/** 设备ID **/
NSUUID *identifierForVendor = [device identifierForVendor];

NSBundle

我们主要用到类NSBundle的mainbundle,一个包含了nib文件,编译代码等资源的目录,这个目录下有一个infoDictionary,通过这个信息字典我们可以获取有关当前应用的一些基本信息,像应用名称、应用版本等等。

测试示例代码:

// 2.NSBundle
NSDictionary *info_dic = [[NSBundle mainBundle] infoDictionary]; // 获取当前设备的信息字典
NSDictionary *localized_info_dic = [[NSBundle mainBundle] localizedInfoDictionary]; // 本地化的信息字典

NSString *app_name = [info_dic objectForKey:@"CFBundleDisplayName"]; // 应用名称 @"Demo"
NSString *app_version = [info_dic objectForKey:@"CFBundleShortVersionString"]; // 应用版本 @"1.0"
NSString *app_build_version = [info_dic objectForKey:@"CFBundleVersion"]; // 应用build版本 @"1"

NSLocale

顾名思义,NSLocale这个类是用于获取一些本地化信息的,例如:国家、时间日期和语言等等。这里主要展示常用到的获取国家和语言代码的方法。

测试示例代码:

// 3.NSLocale
// 3.1 获取偏好语言
NSArray *languageArray = [NSLocale preferredLanguages];
NSString *language = [languageArray objectAtIndex:0]; // @"en_HK"

// 3.2 获取本地化国家或地区代号
NSLocale *locale = [NSLocale currentLocale]; // 当前本地化对象
NSString *country = [locale localeIdentifier]; // @"en_HK"
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_厚厚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值