iOS - 项目中App版本更新逻辑 , 操作

本文探讨了在iOS应用中如何处理版本更新的逻辑。当用户关闭自动更新时,应用应提供友好提示通知用户有新版本可用。同时,介绍了如何在用户更新后存储和展示新版本特性。更新逻辑包括请求服务器检查更新和查询苹果商店信息。

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

iOS本身自带自动更新 , 前提是用户设置了更新 .

这里写图片描述

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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值