天天看點

ios開發-擷取手機相關資訊 一。UIDevice 二。NSBundle 三。NSLocale

今天在做用戶端的時候,裡面有個意見回報功能。

調用系統帶的郵件功能,發送郵件到指定郵箱。

然後我就想,應該在郵件正文部分添加手機相關内容,比如型号,版本,應用程式的版本等等,這樣不僅使用者友善,開發者也能更好的分析。

于是,學習了相關的知識,在這裡與大家分享。

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];  

}  

下面是運作截圖。

ios開發-擷取手機相關資訊 一。UIDevice 二。NSBundle 三。NSLocale

繼續閱讀