赞
踩
今天在做客户端的时候,里面有个意见反馈功能。
调用系统带的邮件功能,发送邮件到指定邮箱。
然后我就想,应该在邮件正文部分添加手机相关内容,比如型号,版本,应用程序的版本等等,这样不仅使用者方便,开发者也能更好的分析。
于是,学习了相关的知识,在这里与大家分享。
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, NULL, 0);
char *machine = (char*)malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
所以如果想更完美点,可以自己根据字符串判断。
比如: if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
- // [[UIDevice currentDevice] systemName]; // 系统名
- // [[UIDevice currentDevice] systemVersion]; //版本号
- // [[UIDevice currentDevice] model]; //类型,模拟器,真机
- // [[UIDevice currentDevice] uniqueIdentifier]; //唯一识别码
- // [[UIDevice currentDevice] name]; //设备名称
- // [[UIDevice currentDevice] localizedModel]; // 本地模式
- //设备相关信息的获取
- 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 //地方型号 (国际化区域名称)
-
- NSString* phoneModel = [[UIDevice currentDevice] model];
- NSLog(@"手机型号: %@",phoneModel ); //手机型号
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- //app应用相关信息的获取
- NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];
- // CFShow(dicInfo);
-
- NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"];
- NSLog(@"App应用名称:%@", strAppName); // 当前应用名称
-
- NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];
- NSLog(@"App应用版本:%@", strAppVersion); // 当前应用软件版本 比如:1.0.1
-
- NSString *strAppBuild = [dicInfo objectForKey:@"CFBundleVersion"];
- NSLog(@"App应用Build版本:%@", strAppBuild); // 当前应用版本号码 int类型
- //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
下面简单的展示下我在邮件中加入的相关内容。 不过因为是在模拟器跑的,一些内容显示的不是很具体。
具体的可以自己移植到真机去试试。
下面是发送邮件代码:
- //选中发送邮件形式
- if (buttonIndex == 0)
- {
- // mail note
-
- if ([MFMailComposeViewController canSendMail])
- {
- MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
- picker.mailComposeDelegate = self;
-
- //设置收件人,可以设置多人
- [picker setToRecipients:[NSArray arrayWithObjects:@"hitwhylz@163.com",nil]];
- //设置主题
- //设备相关信息的获取
- NSString *strName = [[UIDevice currentDevice] name]; //e.g. "My iPhone"
- NSString *strSysVersion = [[UIDevice currentDevice] systemVersion]; // e.g. @"4.0"
- NSString *strModel = [[UIDevice currentDevice] model]; // e.g. @"iPhone", @"iPod touch"
- NSString* phoneModel = [[UIDevice currentDevice] model]; //手机型号
- //app应用相关信息的获取
- NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];
- NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"]; // 当前应用名称
- NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];
- // 当前应用软件版本 比如:1.0.1
-
- [picker setSubject: @"客户端意见反馈"];
- [picker setMessageBody:[NSString stringWithFormat:@"设备名称:%@ \n系统版本号:%@\n设备模式:%@\n手机型号: %@\nApp应用名称:%@\nApp应用版本:%@\n",strName,strSysVersion,strModel,phoneModel,strAppName,strAppVersion] isHTML:NO];
-
- [self presentModalViewController:picker animated:YES];
- }
-
- else
- {
- msg = @"无法正常发送邮件,请重新设置。";
- [self alertWithTitle:nil msg:msg];
-
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
学习的路上,与君共勉。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。