检查app版本是否有更新
当你的 app 版本更新之后,一般情况下用户是不会知道的,只有等到 App Store 的图片上有一个大大的"1"的时候,强迫症的用户才会去看看有什么 app 更新了版本.那么这个时候,我们就需要在用户打开你的 app 的时候,提示用户:"我们的 app 已经更新版本啦,快点下载最新版本吧".那么该如何实现这个功能呢?今天就来说下我的实现方法.
先来一张demo效果图:
再来看看斗鱼TV更新提示
/**
检查版本
*/
- (void)checkVersion {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// app版本
self.currentVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:APP_URL]];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
__weak typeof (self) weakSelf = self;
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSError *err;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
NSLog(@"%@\n%@",dict.description,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSArray *dateArr = dict[@"results"];
if (dateArr && dateArr.count) {
//appStore里面的版本号
NSDictionary *releaseInfo = [dateArr objectAtIndex:0];
weakSelf.lastVersion = [releaseInfo objectForKey:@"version"];
//当前版本小于AppStore的版本 比如1.2.1<1.2.2 提示更新
if ([weakSelf version:weakSelf.currentVersion lessthan:weakSelf.lastVersion]) {
weakSelf.releaseNotes = [releaseInfo objectForKey:@"releaseNotes"]?:@"1.修复已知bug\n2.优化app性能";
dispatch_async(dispatch_get_main_queue(), ^{
//弹出升级提示
[weakSelf showUpdateView];
});
}
}
}
}];
//开始请求
[task resume];
}
//比较数字相关字符串 1.0.1 和 1.0.2
- (BOOL)version:(NSString *)oldver lessthan:(NSString *)newver //系统api
{
if ([oldver compare:newver options:NSNumericSearch] == NSOrderedAscending)
{
return YES;
}
return NO;
}