NSString * strurl = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppIDs];//替换为自己App的ID
NSURLSession *session=[NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:strurl];
//3.创建可变的请求对象
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
//8.解析数据
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"];//链接
NSLog(@"App store版本号:%@",lineVersion);
NSLog(@"更新说明:%@",releaseNotes);
NSLog(@"App下载链接:%@",trackViewUrl);
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
//获取字符串中的数字
self.applocalversion = [[self.applocalversion componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];//本地的app版本
lineVersion = [[lineVersion componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];//App store的app版本
NSLog(@"结果是本地的app版本:%@ \n App store的app版本:%@ ",self.applocalversion,lineVersion);
// 获取NSUserDefaults对象
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:lineVersion forKey:@"App_store_version"];
[defaults synchronize]; //如果要立刻保存就需要这行代码
if ([lineVersion integerValue]>[self.applocalversion integerValue]) {//App store的app版本>//本地的app版本 时要提醒升级
dispatch_async(dispatch_get_main_queue(), ^{
self.nav.tabBarItem.badgeValue = @"new";
});
}else{
NSLog(@"111没有新版本不用更新提示");
}
}];
//7.执行任务
[dataTask resume];