网上很多是通过后台提供接口来判断是否更新的方法,这里就不一一介绍了,今天主要讲的是不需要后台的情况下如何实现自动更新
1.首先获取到当前版本号
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
2.下面是重点,如何获取iTunes上面的版本号
首先需要一个URL:http://itunes.apple.com/cn/lookup?id=1159245816 id改为自己的APP的id
返回的是接送数据,我们需要解析一下,获取版本号
NSArray *resultsArr = successDic[@"results"];
if (resultsArr.count > 0)
{
NSDictionary *dic = resultsArr[0];
//解析得到iTunes上的版本号
NSString *onLineVersoin = dic[@"version"];
// NSLog(@"线上版本号%@",onLineVersoin);
//通过对比来确认需不需要更新
if ([onLineVersoin floatValue] > [version floatValue])
{
UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"新版本%@已发布",onLineVersoin] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* updateAction = [UIAlertAction actionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//需要更新就直接调起Appstore 去更新
NSString *urlStr = @"https://itunes.apple.com/cn/app/zhang-chuang-tian-xia/id1159245816?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertvc addAction:cancelAction];
[alertvc addAction:updateAction];
[self.navigationController presentViewController:alertvc animated:YES completion:nil];
}
else
{
//因为我用的是点击检测更新,所以没有新版本的时候会提示已经是最新版本了
[self showAlertWithPoint:1
text:@"已经是最新版本了"
color:UICYAN];
}
}
网友评论