移動客戶端進行版本迭代時,需要進行本地版本號和服務器版本號比較,從而實現更新提示。本文祥講怎樣獲取版本號,具體的比較方法。
一、CFBundleShortVersionString和CFBundleVersion
CFBundleShortVersionString:對應Version項
標識應用程序的發佈版本號。該版本的版本號是三個時期分隔的整數組成的字符串。第一個整數代表重大修改的版本,如實現新的功能或重大變化的修訂。第二個整數表示的修訂,實現較突出的特點。第三個整數代表維護版本。該鍵的值不同於“CFBundleVersion”標識。
CFBundleVersion:對應Build項
標識(發佈或未發佈)的內部版本號。這是一個單調增加的字符串,包括一個或多個時期分隔的整數。
NSString *version = [myVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
//當前版本號
NSString *myVersion = [[NSBundlemainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
//獲取到服務器最新版本號,此處用1.1.0測試
NSString *lastVersion =@"1.1.0";
if ([myVersioncompare:lastVersionoptions:NSNumericSearch] ==NSOrderedDescending) {
//當前版本低於最新版本
NSLog(@" the version is low");
}
else
{
NSLog(@" the version is last");
}
三:跳轉到APPStore更新
點擊立即更新跳轉到APPStore:
NSString *urlStr =@"http://itunes.apple.com/cn/app/id1197959303"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];