iOS 版本更新迭代

开发中我们可能会遇到这样的需求,当AppStore中有新版本迭代更新,在用户点开APP的时候弹框提醒客户去AppStore更新APP。这里面就有个关键点,判断当前APP与AppStore中的版本高低,若一样,则无需进行提示;反之则弹框提示(用户使用版本不会比AppStore版本高~)。

下面就说一下如何获取APP在AppStore中的版本,直接上代码。

#define KEY @"CFBundleShortVersionString"
- (void)judgeCurrentAppStoreVersion
{
    // 1.通过session请求
    NSString *str = @"http://itunes.apple.com/lookup?id=414478124";
    NSURL *urlStr = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlStr];
    
    // 2.初始化session
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSDictionary *appInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        
        NSArray *infoContent = [appInfo objectForKey:@"result"];
        // 2.1商店版本号
        NSString *storeVersion = [[infoContent objectAtIndex:0] objectForKey:KEY];
        NSLog(@"商店的版本号是%@", storeVersion);
        
        // 2.2当前客户端的版本号
        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[KEY];
        NSLog(@"当前版本是%@", currentVersion);
        
        // 2.3比较当前版本号和商店版本号
        if (![currentVersion isEqualToString:storeVersion]) { // 新版本
            
            // 2.4弹窗提示更新
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有最新版本了,请及时更新" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_STRING]];
            }];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            
            
            [alertController addAction:OKAction];
            [alertController addAction:cancelAction];
            dispatch_async(dispatch_get_main_queue(),{
              [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
            });
            // 2.5存储新版本号
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:KEY forKey:currentVersion];
            [defaults synchronize];
            
        }else if ([currentVersion isEqualToString:storeVersion]){ // 旧版本
            
        }

    }];
    // 3.开启任务
    [sessionTask resume];
}
http://itunes.apple.com/lookup?id=414478124 id为APP在AppStore中的一个序号。可以打开appstore 点击一个app 复制链接即可看到。id 是在你提交信息后,先不要提交审核,
就可以看到id。(PS:我是这样做的,有更好的可以指正)然后拿到这个id后可以去代码里面写了。

解释:
1.CFBundleShortVersionString 表示应用程序的发布版本号,
该版本号一般由三个整数构成,第一个整数表示有重大的修改版本,例如增加新的功能或重大变化。第二个版本表示修订版本,实现较为突出的特点。第三个版本表示维护的版本。
该值不同于 "CFBundleVersion" 标识
2.CFBundleVersion 标识应用的内部版本号

  这个版本是内部自己团队使用的一个版本号,一般不对外公开。

这两个的区别:

   1. CFBundleShortVersionString 对应Xcode里项目的 Version

  2. CFBundleVersion 对应Xcode里项目的 Build

每发布一个新应用或新版本,苹果都要求你输入一个版本号,这个版本号对应的是 CFBundleShortVersionString ,不要写错哦。并且,如果你上传成功后(未审核,或未通过),然后又修复了bug,或改了功能,那么在打包发布时,CFBundleVersion 必须比上一版本更大。

     打个比方,我第一次上传的Version:1.5.1、Build:3.4.2 ,那我这个应用被拒绝,修复好后,我又打包上传时,Version还是1.5.1,但Build必须大于3.4.2,可以是3.4.3 、3.4.5、3.8.5等。 如果Version 1.5.1通过审核后,又发新版本,那个下次上传时,Version要大于1.5.1,但Build可以从新开始,比如1.1.0 。如果Version1.5.1又有问题,我又要上传修改后的应用时,Build必须大于上一个上传成功的Build,即要大于1.1.0。

参考文章:  

1.《ios版本更新判断》;

2.《ios开发之获取APP在AppStore中的版本号》

原文地址:https://www.cnblogs.com/peaker-wu/p/5395248.html