iOS的UIDevice,NSBundle,NSLocale

來源:互聯網
上載者:User

標籤:

 iOS的APP的應用開發的過程中,有時為了bug跟蹤或者擷取用反饋的需要自動收集用 戶裝置、系統資訊、應用資訊等等,這些資訊方便開發人員診斷問題,當然這些資訊是使用者的非隱私資訊,是通過開發api可以擷取到的。那麼通過那些api可以 擷取這些資訊呢,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 

iOS的UIDevice,NSBundle,NSLocale

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.