iOS的APP验证版本更新方法

本文介绍了一种在iOS应用中实现版本更新检测的方法。通过发送HTTP请求获取远程服务器上的最新版本信息,并将其与本地应用程序的版本进行比较。如果发现新版本,则会提示用户更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个老项目没有集成自动检测及提示用户更新新版本的功能,自己在网上查资料捣鼓出自己的方法,可能比较粗陋,希望大家多多指导,我们的项目的版本号是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];
    }
}

  

转载于:https://www.cnblogs.com/magiccoding/p/5467951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值