iOS本身自带自动更新 , 前提是用户设置了更新 .
1. 但是有用户可能暂时性的 , 或者无意关掉了更新 . 那么当app已经有新版本时 , 应当给用户一个友好的提示 , 表示我们版本有新的了 , 让其选择更新或者忽略 .
2. 本地对版本号进行存储 , 当用户更新版本后 , 展示新版本特性 , 而新版本特别 一般只需在更新完版本后 展现依次 .
所以 , 逻辑如下:
//1.获取上次登录版本号
NSString *lastVersion = [[NSUserDefaults standardUserDefaults]objectForKey:@"lastVersion"];
//2.获取当前登录版本号
NSString *currentVersion = [[NSBundle mainBundle]infoDictionary][@"CFBundleShortVersion"]; //也可自行在info.plist中添加键值对 ,key可自拟
//3.版本对比
//基本情况就是 , 现在版本高于之前版本 ,那么可以展示一些新版本特性
if (![currentVersion isEqualToString:lastVersion]) {
//此处可做逻辑操作 , 比如引导页 ?
}else{
//逻辑操作
}
//方法一: 请求自身服务器
//4. 获取最新版本 , 两种方式
//方式一: 通过后台服务器返回版本号
//此方法不是很好 , 第一是需要后台人员维护 , 及时更新 . 第二是在app版本更新上架期间 , 所有用户根据提示更新到的app相当于全是落后一个版本的.
//假设后台版本接口为 www.baidu.com/host/AppVersion
//此处使用原生请求 , 项目中当时使用AFN或者封装好的请求即可.
NSString *urlString = @"www.baidu.com/host/AppVersion";
//转义
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//解析json
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//假设键为 version
NSString *newVersion = dict[@"version"];
//判断如果用户使用版本 低于最新版本 , 那么提示用户进行更新
if (currentVersion.floatValue < newVersion.floatValue) {
//弹出提示框更新版本
[self alertShowWithTips:newVersion];
}
}]resume];
//方法二 : 请求苹果商店
//方法二 :
//假设你的appid是 12345
NSInteger appid = 12345;
NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%li",appid];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLSession *session = [NSURLSession sharedSession];
[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:nil error:NULL];
//获取版本号
//返回 version键对应的值
NSString *newVersion = [dict[@"results"]firstObject][@"version"];
//对比版本号
if (newVersion.floatValue > currentVersion.floatValue) {
//弹出框
[self alertShowWithTips:newVersion];
}
}];
// NSString *newVersion = [NSString]
//返回值截取
/*
{
resultCount = 1;
results = (
{
artistId = 开发者 ID;
artistName = 开发者名称;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 审查名称;
trackContentRating = 评级;
trackId = 应用程序 ID;
trackName = 应用程序名称";
trackViewUrl = 应用程序介绍网址;
userRatingCount = 用户评级;
userRatingCountForCurrentVersion = 1;
version = 版本号;
wrapperType = software;
}
);
}
*/
//弹框
- (void)alertShowWithTips:(NSString *)newVersion
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"该App有新版本" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"忽略" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"前往前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//该app在appStore中的地址 例如 :https://itunes.apple.com/....123213
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/....123213"];
//前往苹果商店更新
[[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
//5 .更新本地存储版本号
[[NSUserDefaults standardUserDefaults]setObject:newVersion forKey:@"lastVersion"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}];
[alert addAction:actionOne];
[alert addAction:actionTwo];
[self presentViewController:alert animated:YES completion:nil];
}