星空网 > 软件开发 > 操作系统

iOS的APP验证版本更新方法

一个老项目没有集成自动检测及提示用户更新新版本的功能,自己在网上查资料捣鼓出自己的方法,可能比较粗陋,希望大家多多指导,我们的项目的版本号是X.X.X的格式,所以直接把字符串转换成float类型的值无效,自己先将字符串做了处理,后来转成float类型后对版本做比较,以下贴上代码

#pragma mark - 检查版本更新- (void)checkNewVersion {  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  [manager GET:APP_URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {      } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 请求成功    // 字典接收请求到的JSon    NSDictionary *responseDict = [NSDictionary dictionaryWithDictionary:responseObject];    // 解析请求到的JSon    NSArray *results = [responseDict objectForKey:@"results"];    NSDictionary *finalDict = [results firstObject];    // 获取APP下载地址    NSString *trackViewUrl = [finalDict objectForKey:@"trackViewUrl"];    updateUrl = [NSURL URLWithString:trackViewUrl];    // 获取官网APP的版本号    NSString *str1 = [finalDict objectForKey:@"version"];    NSString *version1 = [str1 substringFromIndex:2];    // 将版本号字符串转换成float类型    float newVersion = [version1 floatValue];        // 获取本地项目版本号    // 拿到项目的infoPlist文件中所有内容    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];    // 获取到当前工程版本号    NSString *str2 = [infoDict objectForKey:@"CFBundleVersion"];    NSString *version2 = [str2 substringFromIndex:2];    // 将版本号字符串转换成float类型    float localVersion = [version2 floatValue];    // 对比两处版本号    if (newVersion > localVersion) {      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"有新版本可用,请安装更新" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil];      alert.tag = 1;      [alert show];    }      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {// 请求失败    // 返回请求失败的原因    NSLog(@"NSError:%@", error);  }];}

  以下是UIAlertView的代理方法,用于监听用户的点击后做响应,跳转到商店下载页面

#pragma mark - UIAlertViewDelegate- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  if (alertView.tag == 1) {    // 跳转到下载页面    [[UIApplication sharedApplication] openURL:updateUrl];  }}

  




原标题:iOS的APP验证版本更新方法

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

马来西亚国际快递查询:https://www.goluckyvip.com/tag/91127.html
吉隆坡物流:https://www.goluckyvip.com/tag/91128.html
马来小包物流:https://www.goluckyvip.com/tag/91129.html
马来西亚航空快递:https://www.goluckyvip.com/tag/91130.html
ems到马来快递费:https://www.goluckyvip.com/tag/91131.html
马来cod物流:https://www.goluckyvip.com/tag/91132.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流