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

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/91589.html
新西兰 物流:https://www.goluckyvip.com/tag/91590.html
利比亚物流:https://www.goluckyvip.com/tag/91592.html
肯尼亚国际物流:https://www.goluckyvip.com/tag/91593.html
卡拉奇物流:https://www.goluckyvip.com/tag/91594.html
肯尼亚物流:https://www.goluckyvip.com/tag/91595.html
皇帝的皇宫=:https://www.vstour.cn/a/363188.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流