ios开发-获取手机相关信息

今天在做客户端的时候,里面有个意见反馈功能。

调用系统带的邮件功能,发送邮件到指定邮箱。

然后我就想,应该在邮件正文部分添加手机相关内容,比如型号,版本,应用程序的版本等等,这样不仅使用者方便,开发者也能更好的分析。

于是,学习了相关的知识,在这里与大家分享。


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


在次之前,补充个内容。UIDevice是无法获得具体的设备型号的。

要获得设备型号,比如(iphone 4s, iphone5)这样的,要通过这样的办法。

1.引入头文件。

#include <sys/types.h>

#include <sys/sysctl.h>

2.获取型号

            //手机型号。

            size_t size;

            sysctlbyname("hw.machine"NULL, &size, NULL0);

            char *machine = (char*)malloc(size);

            sysctlbyname("hw.machine", machine, &size, NULL0);

            NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];


这里得到的 platform是个设备型号。  比如iphone5,2.

所以如果想更完美点,可以自己根据字符串判断。

比如: if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";

一。UIDevice


    UIDevice提供了多 种属性、类函数及状态通知,帮助我们全方位了解设备状况。从检测电池电量到定位设备与临近感应,UIDevice所做的工作就是为应用程序提供用户及设备的一些信息。UIDevice类还能够收集关于设备的各种具体细节,例如机型及iOS版本等。其中大部分属性都对开发工作具有积极的辅助作用。下面的代码简单的使用UIDevice获取手机属性。

[cpp]  view plain copy
  1. //    [[UIDevice currentDevice] systemName]; // 系统名  
  2. //    [[UIDevice currentDevice] systemVersion]; //版本号  
  3. //    [[UIDevice currentDevice] model]; //类型,模拟器,真机  
  4. //    [[UIDevice currentDevice] uniqueIdentifier]; //唯一识别码  
  5. //    [[UIDevice currentDevice] name]; //设备名称  
  6. //    [[UIDevice currentDevice] localizedModel]; // 本地模式  
  7.     //设备相关信息的获取  
  8.     NSString *strName = [[UIDevice currentDevice] name];  
  9.     NSLog(@"设备名称:%@", strName);//e.g. "My iPhone"  
  10.       
  11.     NSString *strId = [[UIDevice currentDevice] uniqueIdentifier];  
  12.     NSLog(@"设备唯一标识:%@", strId);//UUID,5.0后不可用  
  13.       
  14.     NSString *strSysName = [[UIDevice currentDevice] systemName];  
  15.     NSLog(@"系统名称:%@", strSysName);// e.g. @"iOS"  
  16.       
  17.     NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];  
  18.     NSLog(@"系统版本号:%@", strSysVersion);// e.g. @"4.0"  
  19.       
  20.     NSString *strModel = [[UIDevice currentDevice] model];  
  21.     NSLog(@"设备模式:%@", strModel);// e.g. @"iPhone", @"iPod touch"  
  22.       
  23.     NSString *strLocModel = [[UIDevice currentDevice] localizedModel];  
  24.     NSLog(@"本地设备模式:%@", strLocModel);// localized version of model //地方型号  (国际化区域名称)  
  25.       
  26.     NSString* phoneModel = [[UIDevice currentDevice] model];  
  27.     NSLog(@"手机型号: %@",phoneModel );   //手机型号  

二。NSBundle


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

[cpp]  view plain copy
  1. //app应用相关信息的获取  
  2. NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];  
  3. // CFShow(dicInfo);  
  4.   
  5. NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"];  
  6. NSLog(@"App应用名称:%@", strAppName);   // 当前应用名称  
  7.   
  8. NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];  
  9. NSLog(@"App应用版本:%@", strAppVersion);    // 当前应用软件版本  比如:1.0.1    
  10.   
  11. NSString *strAppBuild = [dicInfo objectForKey:@"CFBundleVersion"];  
  12. NSLog(@"App应用Build版本:%@", strAppBuild);      // 当前应用版本号码   int类型  

三。NSLocale


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

[cpp]  view plain copy
  1. //Getting the User’s Language  
  2. NSArray *languageArray = [NSLocale preferredLanguages];  
  3. NSString *language = [languageArray objectAtIndex:0];  
  4. NSLog(@"语言:%@", language);//en  
  5.   
  6. NSLocale *locale = [NSLocale currentLocale];  
  7. NSString *country = [locale localeIdentifier];  
  8. NSLog(@"国家:%@", country); //en_US  



下面简单的展示下我在邮件中加入的相关内容。 不过因为是在模拟器跑的,一些内容显示的不是很具体。

具体的可以自己移植到真机去试试。

下面是发送邮件代码:

[cpp]  view plain copy
  1.    //选中发送邮件形式  
  2. if (buttonIndex == 0)  
  3.    {  
  4.     // mail note  
  5.          
  6.        if ([MFMailComposeViewController canSendMail])  
  7.        {  
  8.            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];  
  9.            picker.mailComposeDelegate = self;  
  10.              
  11.            //设置收件人,可以设置多人  
  12.            [picker setToRecipients:[NSArray arrayWithObjects:@"hitwhylz@163.com",nil]];  
  13.            //设置主题  
  14.            //设备相关信息的获取  
  15.            NSString *strName = [[UIDevice currentDevice] name];  //e.g. "My iPhone"  
  16.            NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];  // e.g. @"4.0"  
  17.            NSString *strModel = [[UIDevice currentDevice] model];  // e.g. @"iPhone", @"iPod touch"  
  18.            NSString* phoneModel = [[UIDevice currentDevice] model];   //手机型号  
  19.            //app应用相关信息的获取  
  20.            NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];  
  21.            NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"];  // 当前应用名称  
  22.            NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];  
  23.            // 当前应用软件版本  比如:1.0.1  
  24.   
  25.            [picker setSubject: @"客户端意见反馈"];  
  26.            [picker setMessageBody:[NSString stringWithFormat:@"设备名称:%@ \n系统版本号:%@\n设备模式:%@\n手机型号: %@\nApp应用名称:%@\nApp应用版本:%@\n",strName,strSysVersion,strModel,phoneModel,strAppName,strAppVersion] isHTML:NO];  
  27.      
  28.            [self presentModalViewController:picker animated:YES];  
  29.        }  
  30.          
  31.        else  
  32.        {  
  33.            msg = @"无法正常发送邮件,请重新设置。";  
  34.            [self alertWithTitle:nil msg:msg];  
  35.              
  36.        }  
  37. }  

下面是运行截图。

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值