您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 取得裝置與應用程式資訊:UIDevice 與 infoDictionary 常用參數整理

編輯:iOS 開發綜合

應用程式的名稱、版本號等資訊都存在 mainBundle 的資訊字典裡,而裝置相關資訊則要透過 UIDevice 取得。以下把常用的幾項整理在一起。

從 infoDictionary 讀取 App 資訊

NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *versionNum = [infoDict objectForKey:@"CFBundleVersion"];
NSString *appName    = [infoDict objectForKey:@"CFBundleDisplayName"];
NSString *text = [NSString stringWithFormat:@"%@ %@", appName, versionNum];

裝置與應用程式資訊一覽

// 裝置名稱(使用者自訂的暱稱)
NSString *userPhoneName = [[UIDevice currentDevice] name];
NSLog(@"手機別名: %@", userPhoneName);

// 系統名稱
NSString *deviceName = [[UIDevice currentDevice] systemName];
NSLog(@"設備名稱: %@", deviceName);

// 系統版本
NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
NSLog(@"手機系統版本: %@", phoneVersion);

// 機型
NSString *phoneModel = [[UIDevice currentDevice] model];
NSLog(@"手機型號: %@", phoneModel);

// 本地化的機型名稱
NSString *localPhoneModel = [[UIDevice currentDevice] localizedModel];
NSLog(@"國際化區域名稱: %@", localPhoneModel);

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

// 應用程式名稱
NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
NSLog(@"當前應用名稱:%@", appCurName);

// 應用程式版本,例如 1.0.1
NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSLog(@"當前應用軟體版本: %@", appCurVersion);

// 應用程式 Build 號
NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];
NSLog(@"當前應用版本號碼:%@", appCurVersionNum);

實際輸出範例

手機別名: "spring sky"的iPod
設備名稱: iPhone OS
手機系統版本: 5.1.1
手機型號: iPod touch
國際化區域名稱: iPod touch
當前應用名稱:myDemo
當前應用軟體版本: 1.0.1
當前應用版本號碼:1

幾個實務上必須知道的限制

uniqueIdentifier 早已不能用。早期可以透過 [[UIDevice currentDevice] uniqueIdentifier] 取得裝置唯一識別碼(UDID),但 Apple 基於隱私考量已經將該 API 移除,利用其返回值不但無法上架,也違反審核條款。現在要識別裝置,請改用 identifierForVendor(同一開發者的 App 之間一致,卸載全部 App 後會重置)、advertisingIdentifier(需處理使用者關閉追蹤的情況),或自行產生 UUID 並保存到鑰匙串。

model 回傳的是類別而非精確機型。它只會告訴你「iPhone」或「iPod touch」,若需要精準辨識(例如區分 Plus 與非 Plus 以調整版面),得自行解析 utsname 的 machine 欄位並建立對照表,且這張表需要隨新機型推出而維護。

版本號與 Build 號用途不同。CFBundleShortVersionString 是給使用者看的版本,CFBundleVersion 則是上傳時用來判斷是否為新版本的遞增號碼,兩者不要混用。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved