ios開發-獲取手機相關信息

今天在作客戶端的時候,裏面有個意見反饋功能。api

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

而後我就想,應該在郵件正文部分添加手機相關內容,好比型號,版本,應用程序的版本等等,這樣不只使用者方便,開發者也能更好的分析。iphone

因而,學習了相關的知識,在這裏與你們分享。函數

 

iOS的APP的應用開發的過程當中,有時爲了bug跟蹤或者獲取用反饋的須要自動收集用戶設備、系統信息、應用信息等等,這些信息方便開發者診斷問題,固然這些信息是用戶的非隱私信息,是經過開發api能夠獲取到的。那麼經過那些api能夠獲取這些信息呢,iOS的SDK中提供了UIDevice,NSBundle,NSLocale。學習

 

在次以前,補充個內容。UIDevice是沒法得到具體的設備型號的。ui

要得到設備型號,好比(iphone 4s, iphone5)這樣的,要經過這樣的辦法。spa

1.引入頭文件。.net

 

#include <sys/types.h>orm

#include <sys/sysctl.h>blog

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獲取手機屬性。

//    [[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 );   //手機型號

 

二。NSBundle


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:@"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];
            
        }
	}

 下面是運行截圖。

 

學習的路上,與君共勉。

 

轉自:http://blog.csdn.net/hitwhylz/article/details/12714861

相關文章
相關標籤/搜索