標簽: iOS 技術
在App應用中汽久,我們經常會迭代App鹤竭,而需要添加當前版本的新特性,那么就需要獲取用戶已安裝App的版本信息景醇,如何獲取呢臀稚?請看代碼:
// 獲得版本號
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
在版本對比時,切記不能轉換成float或者double類型去比較三痰,而應該用OC方法吧寺,如代碼:
NSComparisonResult *result = [currentVersion compare:shortVersion];
// NSComparisonResult 是一個枚舉,有3種狀態(tài)散劫,通過狀態(tài)我們就可以得出App當前版本和服務器版本高低
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
上面的比較方法會返回一個枚舉稚机,我們可以封裝一個返回值類型為BOOL
方法,返回值就根據(jù)枚舉狀態(tài)來判斷
獲取其他應用信息
// 兩個獲得應用信息的方法
[[[NSBundle mainBundle] infoDictionary] valueForKey:@"key"];
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"key"];
// 獲得應用名
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
// 獲得版本號
NSString *shortVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
// 獲得build號
NSString *buildViersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
// 獲得bundle id
NSString *dundleId = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
// 獲得應用自定義名稱
NSString *displayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
NSLog(@"\n appName = %@ \n shortVersion = %@ \n buildViersion = %@ \n dundleId = %@ \n displayName = %@", appName, shortVersion, buildViersion, dundleId, displayName);
以上就是通過key來獲取app版本信息的方法