iOS 获取当前app的 App Store 版本号
2020-12-16 本文已影响0人
污嘿
// 1、填写自己App的ID
NSString * strurl = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=1491881948"];//替换为自己App的ID
NSURLSession *session=[NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:strurl];
//2.创建可变的请求对象
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
//4.解析数据
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray * results = dict[@"results"];
NSDictionary * dic = results.firstObject;
NSString * lineVersion = dic[@"version"];//版本号
NSString * releaseNotes = dic[@"releaseNotes"];//更新说明
NSString * trackViewUrl = dic[@"trackViewUrl"];//链接
DSLog(@"App store版本号:%@",lineVersion);
DSLog(@"更新说明:%@",releaseNotes);
DSLog(@"App下载链接:%@",trackViewUrl);
// 5、获取本地版本
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// 本地app版本
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
// 6、比较版本信息
if ([lineVersion floatValue] > [app_Version floatValue]) {
// 7、回到主线程进行后续操作
dispatch_async(dispatch_get_main_queue(), ^{
[self setGengxinUI];
});
DSLog(@"^^%@",app_Version);
}else{
// 7、回到主线程进行后续操作
dispatch_async(dispatch_get_main_queue(), ^{
[SOSVC SOSToolTipShow:@"提示" message:@"已是最新版本" cancelTitle:@"确定" otherTitle:nil];
});
}
}];
//3.执行任务
[dataTask resume];