2023-05-12 开启多语言插件支持……

OC基础知识-获取手机信息

ios 苏 demo 2832℃ 0评论

iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备、系统信息、应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到的。那么通过那些api可以获取这些信息呢,iOS的SDK中提供了UIDevice,NSBundle,NSLocale。
UIDevice
UIDevice提供了多种属性、类函数及状态通知,帮助我们全方位了解设备状况。从检测电池电量到定位设备与临近感应,UIDevice所做的工作就是为应用程序提供用户及设备的一些信息。UIDevice类还能够收集关于设备的各种具体细节,例如机型及iOS版本等。其中大部分属性都对开发工作具有积极的辅助作用。下面的代码简单的使用UIDevice获取手机属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//获取手机信息
    NSString *strName = [[UIDevice currentDevice] name];
    NSLog(@"设备名称:%@",strName);
       
    /*
    NSString *strId = [[UIDevice currentDevice] uniqueIdentifier];
    NSLog(@"设备唯一标识:%@", strId);//UUID,5.0后不可用
    */
    // 获取设备相关信息
    NSString *strSysName = [[UIDevice currentDevice] systemName];
    NSLog(@"系统名称:%@",strSysName);
       
    NSString *strSysVersion = [[UIDevice currentDevice]systemVersion];
    NSLog(@"系统版本号:%@",strSysVersion);
       
    NSString *strModel = [[UIDevice currentDevice] model];
    NSLog(@"设备模式:%@",strModel);
       
    NSString *strLocModel = [[UIDevice currentDevice] localizedModel];
    NSLog(@"本地设备模式:%@",strLocModel);
    /*
   bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle。通过这个路径可以获取到应用的信息,例如应用名、版本号等。
    */
       
    NSDictionary *dict = [[NSBundle mainBundle]infoDictionary];
       
    NSString *strAppName = [dict objectForKey:@"CFBundleDisplayName"];
    NSLog(@"App应用名称:%@",strAppName);
   
    NSString *strAppVersion = [dict objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"App应用版本:%@",strAppVersion);
       
    NSString *strAppbuild = [dict objectForKey:@"CFBundleVersion"];
    NSLog(@"APP应用Build版本: %@",strAppbuild);
       
    /*
    NSLocale
    NSLocale可以获取用户的本地化信息设置,例如货币类型,国家,语言,数字,日期格式的格式化,提供正确的地理位置显示等等。下面的代码获取机器当前语言和国家代码。
     */
    //Getting the User’s Language
    NSArray *languageArray =  [NSLocale preferredLanguages];
    NSString *language = [languageArray objectAtIndex:0];
    NSLog(@"语言:%@", language);
       
    NSLocale *locale = [NSLocale currentLocale];
    NSString *country = [locale localeIdentifier];
    NSLog(@"国家:%@", country); //en_US
   
PS:我们的宏定义 本质上是一样的
   
NS_INLINE NSString* UDID() {
#if kUseTestUDID
  return kTestUDID;
#else
  return [[UIDevice currentDevice] serialNumber];
#endif
}
   
NS_INLINE NSString * DeviceType() {
  return [[UIDevice currentDevice] model];
}
   
NS_INLINE NSString * DeviceOS() {
  return [[UIDevice currentDevice] systemName];
}
   
NS_INLINE NSString * DeviceOSVersion() {
  return [[UIDevice currentDevice] systemVersion];
}
打赏

转载请注明:苏demo的别样人生 » OC基础知识-获取手机信息

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)