设为首页 加入收藏

TOP

iOS的APP验证版本更新方法
2017-10-13 10:33:36 】 浏览:3318
Tags: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开发系列--Objective-C之协议.. 下一篇iPhone越狱的利与弊

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目