博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)
阅读量:6983 次
发布时间:2019-06-27

本文共 2780 字,大约阅读时间需要 9 分钟。

hot3.png

       iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备、系统信息、应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到的。那么通过那些api可以获取这些信息呢,iOS的SDK中提供了UIDevice,
NSBundle,
NSLocale。

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

NSBundle

    bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle。通过这个路径可以获取到应用的信息,例如应用名、版本号等。

//app应用相关信息的获取    NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];    //    CFShow(dicInfo);        NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"];    NSLog(@"App应用名称:%@", strAppName);        NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];    NSLog(@"App应用版本:%@", strAppVersion);        NSString *strAppBuild = [dicInfo objectForKey:@"CFBundleVersion"];    NSLog(@"App应用Build版本:%@", strAppBuild);

NSLocale

     NSLocale可以获取用户的本地化信息设置,例如货币类型,国家,语言,数字,日期格式的格式化,提供正确的地理位置显示等等。下面的代码获取机器当前语言和国家代码。

//Getting the User’s Language    NSArray *languageArray = [NSLocale preferredLanguages];    NSString *language = [languageArray objectAtIndex:0];    NSLog(@"语言:%@", language);//en        NSLocale *locale = [NSLocale currentLocale];    NSString *country = [locale localeIdentifier];    NSLog(@"国家:%@", country); //en_US

/**
* 张兴业
*  
*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:
*/
参考:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-accessing-device-data-with-uidevice-and-nslocale/

转载于:https://my.oschina.net/201003674/blog/289010

你可能感兴趣的文章
需求获取常见的方法是进行客户访谈,结合你的实践谈谈会遇到什么问题,你是怎么解决的?...
查看>>
windows programming
查看>>
a标签的link、visited、hover、active的顺序
查看>>
SQL系列(四)—— 唯一值(distinct)
查看>>
《数据结构》例1.3
查看>>
堆和栈的区别 (转贴)
查看>>
OpenSSL s_server / s_client 应用实例
查看>>
微信小程序开发工具(0.9.092300)下载地址,分享给没有公众号的小伙伴
查看>>
项目中的常见算法
查看>>
(转)GCT之逻辑经验总结(拿来主义)
查看>>
虚拟继承中子类和父类的构造函数顺序1
查看>>
js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决
查看>>
(实践篇)剖析最近项目使用的一个框架
查看>>
usaco Typo
查看>>
字符串
查看>>
创建对象的三种方式
查看>>
spring学习之spring 插件 for eclipse
查看>>
js-sha256源码
查看>>
运维笔试题
查看>>
dispaly、position、float之间的关系与相互作用
查看>>