你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> iOS獲取手機信息(UIDevice、NSBundle、NSLocale)

iOS獲取手機信息(UIDevice、NSBundle、NSLocale)

編輯:IOS技巧綜合
[摘要]本文是對iOS獲取手機信息(UIDevice、NSBundle、NSLocale)的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
/*
iOS的APP的應用開發的過程中,有時為了bug跟蹤或者獲取用反饋的需要自動收集用戶設備、系統信息、應用信息等等,這些信息方便開發者診斷問題,當然這些信息是用戶的非隱私信息,是通過開發api可以獲取到的。那麼通過那些api可以獲取這些信息呢,iOS的SDK中提供了UIDevice,NSBundle,NSLocale。
UIDevice

UIDevice提供了多種屬性、類函數及狀態通知,幫助我們全方位了解設備狀況。從檢測電池電量到定位設備與臨近感應,UIDevice所做的工作就是為應用程序提供用戶及設備的一些信息。UIDevice類還能夠收集關於設備的各種具體細節,例如機型及iOS版本等。其中大部分屬性都對開發工作具有積極的輔助作用。下面的代碼簡單的使用UIDevice獲取手機屬性。
*/
//獲取手機信息
NSString *strName = [[UIDevice currentDevice] name];
NSLog(@"設備名稱:%@",strName);

/*
NSString *strId = [[UIDevice currentDevice] uniqueIdentifier];
NSLog(@"設備唯一標識:%@", strId);//UUID,5.0後不可用
*/
// 獲取設備相關信息
NSString *strSysName = [[UIDevice currentDevice] systemName];
NSLog(@"系統名稱:%@",strSysName);

NSString *strSysVersion = [[UIDevice currentDevice]systemVersion];
NSLog(@"系統版本號:%@",strSysVersion);

NSString *strModel = [[UIDevice currentDevice] model];
NSLog(@"設備模式:%@",strModel);

NSString *strLocModel = [[UIDevice currentDevice] localizedModel];
NSLog(@"本地設備模式:%@",strLocModel);
/*
bundle是一個目錄,其中包含了程序會使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會把bundle稱為plug-in). 對應bundle,cocoa提供了類NSBundle.一個應用程序看上去和其他文件沒有什麼區別. 但是實際上它是一個包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程序的main bundle。通過這個路徑可以獲取到應用的信息,例如應用名、版本號等。
*/

NSDictionary *dict = [[NSBundle mainBundle]infoDictionary];

NSString *strAppName = [dict objectForKey:@"CFBundleDisplayName"];
NSLog(@"App應用名稱:%@",strAppName);

NSString *strAppVersion = [dict objectForKey:@"CFBundleShortVersionString"];
NSLog(@"App應用版本:%@",strAppVersion);

NSString *strAppbuild = [dict objectForKey:@"CFBundleVersion"];
NSLog(@"APP應用Build版本: %@",strAppbuild);

/*
NSLocale
NSLocale可以獲取用戶的本地化信息設置,例如貨幣類型,國家,語言,數字,日期格式的格式化,提供正確的地理位置顯示等等。下面的代碼獲取機器當前語言和國家代碼。
*/
//Getting the User’s Language
NSArray *languageArray = [NSLocale preferredLanguages];
NSString *language = [languageArray objectAtIndex:0];
NSLog(@"語言:%@", language);

NSLocale *locale = [NSLocale currentLocale];
NSString *country = [locale localeIdentifier];
NSLog(@"國家:%@", country); //en_US
PS:我們的宏定義 本質上是一樣的
NS_INLINE NSString* UDID() {
#if kUseTestUDID
return kTestUDID;
#else 
return [[UIDevice currentDevice] serialNumber];
#endif
}

NS_INLINE NSString * DeviceType() {
return [[UIDevice currentDevice] model];
}

NS_INLINE NSString * DeviceOS() {
return [[UIDevice currentDevice] systemName];
}

NS_INLINE NSString * DeviceOSVersion() {
return [[UIDevice currentDevice] systemVersion];
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved