#defineKEY @"CFBundleShortVersionString"- (void)judgeCurrentAppStoreVersion
{//1.通過session請求NSString *str =@"http://itunes.apple.com/lookup?id=414478124";
NSURL*urlStr =[NSURL URLWithString:str];
NSURLRequest*request =[NSURLRequest requestWithURL:urlStr];//2.初始化sessionNSURLSession *session =[NSURLSession sharedSession];
NSURLSessionTask*sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError *_Nullable error) {
NSDictionary*appInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray*infoContent = [appInfo objectForKey:@"result"];//2.1商店版本號NSString *storeVersion = [[infoContent objectAtIndex:0] objectForKey:KEY];
NSLog(@"商店的版本號是%@", storeVersion);//2.2當前客戶端的版本號NSString *currentVersion =[NSBundle mainBundle].infoDictionary[KEY];
NSLog(@"當前版本是%@", currentVersion);//2.3比較當前版本號和商店版本號if(![currentVersion isEqualToString:storeVersion]) {//新版本//2.4彈窗提示更新UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有最新版本了,請及時更新"message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction*OKAction = [UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_STRING]];
}];
UIAlertAction*cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[alertController addAction:cancelAction];
dispatch_async(dispatch_get_main_queue(),{
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
});//2.5存儲新版本號NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject:KEY forKey:currentVersion];
[defaults synchronize];
}elseif([currentVersion isEqualToString:storeVersion]){//舊版本}
}];//3.開啟任務[sessionTask resume];
}
ttp://itunes.apple.com/lookup?id=414478124 id為APP在AppStore中的一個序號。可以打開appstore 點擊一個app 復制鏈接即可看到。id 是在你提交信息后,先不要提交審核,
就可以看到id。(PS:我是這樣做的,有更好的可以指正)然后拿到這個id后可以去代碼里面寫了。
解釋:
1.CFBundleShortVersionString表示應用程序的發布版本號,
該版本號一般由三個整數構成,第一個整數表示有重大的修改版本,例如增加新的功能或重大變化。第二個版本表示修訂版本,實現較為突出的特點。第三個版本表示維護的版本。
該值不同于 "CFBundleVersion" 標識
2.CFBundleVersion 標識應用的內部版本號
這個版本是內部自己團隊使用的一個版本號,一般不對外公開。
這兩個的區別:
1. CFBundleShortVersionString對應Xcode里項目的Version
2. CFBundleVersion對應Xcode里項目的Build
每發布一個新應用或新版本,蘋果都要求你輸入一個版本號,這個版本號對應的是CFBundleShortVersionString,不要寫錯哦。并且,如果你上傳成功后(未審核,或未通過),然后又修復了bug,或改了功能,那么在打包發布時,CFBundleVersion必須比上一版本更大。
打個比方,我第一次上傳的Version:1.5.1、Build:3.4.2 ,那我這個應用被拒絕,修復好后,我又打包上傳時,Version還是1.5.1,但Build必須大于3.4.2,可以是3.4.3 、3.4.5、3.8.5等。 如果Version 1.5.1通過審核后,又發新版本,那個下次上傳時,Version要大于1.5.1,但Build可以從新開始,比如1.1.0 。如果Version1.5.1又有問題,我又要上傳修改后的應用時,Build必須大于上一個上傳成功的Build,即要大于1.1.0。