iOS获取当前AppStore版本号与更新

时间:2023-03-09 19:29:19
iOS获取当前AppStore版本号与更新
 - (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion ,NSString * newVersion , NSString * currentVersion))success failure:(void (^)(NSError *error))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.requestSerializer=[AFHTTPRequestSerializerserializer];
manager.responseSerializer=[AFHTTPResponseSerializerserializer]; //
NSString *encodingUrl=[[@"http://itunes.apple.com/lookup?id=" stringByAppendingString:@"APP的唯一ID"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [manager GET:encodingUrl parameters:nilprogress:^(NSProgress *_Nonnull downloadProgress) { } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) { NSDictionary *resultDic=[NSJSONSerializationJSONObjectWithData:responseObject options:NSJSONReadingMutableLeaveserror:nil]; //获取AppStore的版本号
NSString * versionStr =[[[resultDic objectForKey:@"results"]objectAtIndex:]valueForKey:@"version"]; NSString *versionStr_int=[versionStr stringByReplacingOccurrencesOfString:@"."withString:@""]; int version=[versionStr_intintValue]; //获取本地的版本号
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary]; NSString * currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"]; NSString *currentVersion_int=[currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""]; int current=[currentVersion_int intValue]; if(version>current){
success(resultDic,YES, versionStr,currentVersion);
}else{
success(resultDic,NO ,versionStr,currentVersion);
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
failure(error);
}];
} // 调用 VersionUpdate *tool=[[VersionUpdatealloc]init];
__weaktypeof(self)weakSelf=self;
[tool checkUpdateWithAppID:@""success:^(NSDictionary *resultDic,BOOL isNewVersion, NSString *newVersion , NSString * currentVersion) {
if (isNewVersion) {//表示要更新版本
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
NSString *key=[NSString stringWithFormat:@"%@%@",currentVersion,newVersion];
NSString *value=[s.versionUpdateMessage objectForKey:key];
int time =[value intValue];
if (time<&&_versionUpdateVC==nil) {//小于三次可以继续弹框
time+=;
[dict setValue:[NSString stringWithFormat:@"%d",time]forKey:key];
s.versionUpdateMessage=[NSDictionary dictionaryWithDictionary:dict];
_versionUpdateVC = [[VersionUpdateController alloc]initWithContent:[[[resultDic objectForKey:@"results"]firstObject]objectForKey:@"releaseNotes"]];//releaseNotes,AppStore上面的APP更新内容
[_versionUpdateVC addSelfFromSuperWithVersionStr:newVersion];
}else{//大于三次不做任何操作
[weakSelf joinGroupView];
}
}
} failure:^(NSError *error) {
[weakSelf joinGroupView];
}];