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


iOS的SDK中提供了UIDevice。 NSBundle, NSLocale。


UIDevice

       UIDevice提供了多種屬性、類函數及狀態通知,幫助我們全方位了解設備狀況。

從檢測電池電量到定位設備與臨近感應。UIDevice所做的工作就是為應用程序提供用戶及設備的一些信息。UIDevice類還能夠收集關於設備的各種詳細細節。比如機型及iOS版本號等。

當中大部分屬性都對開發工作具有積極的輔助作用。

以下的代碼簡單的使用UIDevice獲取手機屬性。


  1. //設備相關信息的獲取  
  2.  NSString *strName = [[UIDevice currentDevice] name];  
  3.  NSLog(@"設備名稱:%@", strName);//e.g. "My iPhone"  
  4.    
  5.  NSString *strId = [[UIDevice currentDevice] uniqueIdentifier];  
  6.  NSLog(@"設備唯一標識:%@", strId);//UUID,5.0后不可用  
  7.    
  8.  NSString *strSysName = [[UIDevice currentDevice] systemName];  
  9.  NSLog(@"系統名稱:%@", strSysName);// e.g. @"iOS"  
  10.    
  11.  NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];  
  12.  NSLog(@"系統版本號號:%@", strSysVersion);// e.g. @"4.0"  
  13.    
  14.  NSString *strModel = [[UIDevice currentDevice] model];  
  15.  NSLog(@"設備模式:%@", strModel);// e.g. @"iPhone", @"iPod touch"  
  16.    
  17.  NSString *strLocModel = [[UIDevice currentDevice] localizedModel];  
  18.  NSLog(@"本地設備模式:%@", strLocModel);// localized version of model  


NSBundle

    bundle是一個文件夾,當中包括了程序會使用到的資源. 這些資源包括了如圖像,聲音,編譯好的代碼,nib文件(用戶也會把bundle稱為plug-in). 相應bundle,cocoa提供了類NSBundle.一個應用程序看上去和其它文件沒有什么差別. 可是實際上它是一個包括了nib文件,編譯代碼,以及其它資源的文件夾. 我們把這個文件夾叫做程序的main bundle。通過這個路徑能夠獲取到應用的信息,比如應用名、版本號號等。



  1. //app應用相關信息的獲取  
  2.     NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];  
  3.     //    CFShow(dicInfo);  
  4.       
  5.     NSString *strAppName = [dicInfo objectForKey:@"CFBundleDisplayName"];  
  6.     NSLog(@"App應用名稱:%@", strAppName);  
  7.       
  8.     NSString *strAppVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"];  
  9.     NSLog(@"App應用版本號:%@", strAppVersion);  
  10.       
  11.     NSString *strAppBuild = [dicInfo objectForKey:@"CFBundleVersion"];  
  12.     NSLog(@"App應用Build版本號:%@", strAppBuild);  


NSLocale

     NSLocale能夠獲取用戶的本地化信息設置。比如貨幣類型。國家。語言,數字,日期格式的格式化。提供正確的地理位置顯示等等。以下的代碼獲取機器當前語言和國家代碼。

  1. //Getting the User’s Language  
  2.    NSArray *languageArray = [NSLocale preferredLanguages];  
  3.    NSString *language = [languageArray objectAtIndex:0];  
  4.    NSLog(@"語言:%@", language);//en  
  5.      
  6.    NSLocale *locale = [NSLocale currentLocale];  
  7.    NSString *country = [locale localeIdentifier];  
  8.    NSLog(@"國家:%@", country); //en_US  
  9.     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM