iOS获取当前AppStore版本号与更新
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS获取当前AppStore版本号与更新相关的知识,希望对你有一定的参考价值。
1 - (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion ,NSString * newVersion , NSString * currentVersion))success failure:(void (^)(NSError *error))failure{ 2 AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager]; 3 manager.requestSerializer=[AFHTTPRequestSerializerserializer]; 4 manager.responseSerializer=[AFHTTPResponseSerializerserializer]; 5 6 // 7 NSString *encodingUrl=[[@"http://itunes.apple.com/lookup?id=" stringByAppendingString:@"APP的唯一ID"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 8 9 [manager GET:encodingUrl parameters:nilprogress:^(NSProgress *_Nonnull downloadProgress) { 10 11 } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) { 12 13 NSDictionary *resultDic=[NSJSONSerializationJSONObjectWithData:responseObject options:NSJSONReadingMutableLeaveserror:nil]; 14 15 //获取AppStore的版本号 16 NSString * versionStr =[[[resultDic objectForKey:@"results"]objectAtIndex:0]valueForKey:@"version"]; 17 18 NSString *versionStr_int=[versionStr stringByReplacingOccurrencesOfString:@"."withString:@""]; 19 20 int version=[versionStr_intintValue]; 21 22 //获取本地的版本号 23 NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary]; 24 25 NSString * currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"]; 26 27 NSString *currentVersion_int=[currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""]; 28 29 int current=[currentVersion_int intValue]; 30 31 if(version>current){ 32 success(resultDic,YES, versionStr,currentVersion); 33 }else{ 34 success(resultDic,NO ,versionStr,currentVersion); 35 } 36 } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { 37 failure(error); 38 }]; 39 } 40 41 42 // 调用 43 44 45 VersionUpdate *tool=[[VersionUpdatealloc]init]; 46 __weaktypeof(self)weakSelf=self; 47 [tool checkUpdateWithAppID:@""success:^(NSDictionary *resultDic,BOOL isNewVersion, NSString *newVersion , NSString * currentVersion) { 48 if (isNewVersion) {//表示要更新版本 49 NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; 50 NSString *key=[NSString stringWithFormat:@"%@%@",currentVersion,newVersion]; 51 NSString *value=[s.versionUpdateMessage objectForKey:key]; 52 int time =[value intValue]; 53 if (time<3&&_versionUpdateVC==nil) {//小于三次可以继续弹框 54 time+=1; 55 [dict setValue:[NSString stringWithFormat:@"%d",time]forKey:key]; 56 s.versionUpdateMessage=[NSDictionary dictionaryWithDictionary:dict]; 57 _versionUpdateVC = [[VersionUpdateController alloc]initWithContent:[[[resultDic objectForKey:@"results"]firstObject]objectForKey:@"releaseNotes"]];//releaseNotes,AppStore上面的APP更新内容 58 [_versionUpdateVC addSelfFromSuperWithVersionStr:newVersion]; 59 }else{//大于三次不做任何操作 60 [weakSelf joinGroupView]; 61 } 62 } 63 } failure:^(NSError *error) { 64 [weakSelf joinGroupView]; 65 }];
以上是关于iOS获取当前AppStore版本号与更新的主要内容,如果未能解决你的问题,请参考以下文章