UIDevice
UIDevice提供了多種屬性、類函數及狀態通知,幫助我們全方位了解設備狀況。
從檢測電池電量到定位設備與臨近感應。UIDevice所做的工作就是為應用程序提供用戶及設備的一些信息。UIDevice類還能夠收集關於設備的各種詳細細節。比如機型及iOS版本號等。
當中大部分屬性都對開發工作具有積極的輔助作用。
以下的代碼簡單的使用UIDevice獲取手機屬性。
- //設備相關信息的獲取
- 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
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);
- NSString *strAppBuild = [dicInfo objectForKey:@"CFBundleVersion"];
- NSLog(@"App應用Build版本號:%@", strAppBuild);
NSLocale
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
-