今天在做客户端的时候,里面有个意见反馈功能。
调用系统带的邮件功能,发送邮件到指定邮箱。
然后我就想,应该在邮件正文部分添加手机相关内容,比如型号,版本,应用程序的版本等等,这样不仅使用者方便,开发者也能更好的分析。
于是,学习了相关的知识,在这里与大家分享。
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];
这里得到的platform是个设备型号。 比如iphone5,2.
所以如果想更完美点,可以自己根据字符串判断。
比如: if ([platform isequaltostring:@"iphone3,1"]) return @"iphone 4";
uidevice提供了多种属性、类函数及状态通知,帮助我们全方位了解设备状况。从检测电池电量到定位设备与临近感应,uidevice所做的工作就是为应用程序提供用户及设备的一些信息。uidevice类还能够收集关于设备的各种具体细节,例如机型及ios版本等。其中大部分属性都对开发工作具有积极的辅助作用。下面的代码简单的使用uidevice获取手机属性。
// [[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 ); //手机型号
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); // 当前应用软件版本 比如:1.0.1
nsstring *strappbuild = [dicinfo objectforkey:@"cfbundleversion"];
nslog(@"app应用build版本:%@", strappbuild); // 当前应用版本号码 int类型
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
下面简单的展示下我在邮件中加入的相关内容。 不过因为是在模拟器跑的,一些内容显示的不是很具体。
具体的可以自己移植到真机去试试。
下面是发送邮件代码:
//选中发送邮件形式
if (buttonindex == 0)
{
// mail note
if ([mfmailcomposeviewcontroller cansendmail])
{
mfmailcomposeviewcontroller *picker = [[mfmailcomposeviewcontroller alloc] init];
picker.mailcomposedelegate = self;
//设置收件人,可以设置多人
[picker settorecipients:[nsarray arraywithobjects:@"[email protected]",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];
}
下面是运行截图。