iOS - 项目中App版本更新逻辑 , 操作
2016-10-21 21:03
435 查看
iOS本身自带自动更新 , 前提是用户设置了更新 .
1. 但是有用户可能暂时性的 , 或者无意关掉了更新 . 那么当app已经有新版本时 , 应当给用户一个友好的提示 , 表示我们版本有新的了 , 让其选择更新或者忽略 .
2. 本地对版本号进行存储 , 当用户更新版本后 , 展示新版本特性 , 而新版本特别 一般只需在更新完版本后 展现依次 .
所以 , 逻辑如下:
//方法一: 请求自身服务器
//方法二 : 请求苹果商店
//弹框
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]; }
相关文章推荐
- iOS 检查app版本更新操作
- ios的app,有新版本时必须先更新。
- 上传IOS项目和版本更新流程图
- app内含有版本更新操作被拒
- 工具链无效。新 App 和 App 更新必须使用公共(正式)版 Xcode 6 或更高版本以及 iOS 8 SDK 或更高版本来构建。请勿提交 Beta 版软件构建的 App。
- iOS 项目如何实现版本的更新?
- ios项目如何实现版本更新?
- ios app 版本更新
- iOS 开发APP时版本更新,版本新特性的设置
- iOS app版本更新的加急审核
- iOS版本更新的App提交审核发布流程
- ios项目如何实现版本更新?
- [ios]获取app版本更新检测的不同语言信息
- iOS App的版本检测更新功能(最新版)
- 提交至APP STORE的APP和APP更新必须使用Xcode5.1.1(或更高级版本)及ios 7 sdk进行构建
- iOS学习笔记31—appStore检查版本更新与从app里跳到appstore评论页面
- ios项目如何实现版本更新?
- 苹果要求2015年2月起所有新app以及版本更新必须支持iOS 8 SDK和64-bit
- 苹果要求所有新app以及版本更新必须支持iOS 8 SDK和64-bit
- iOS中app在iTunes中更新版本流程