iOS版本更新提示

注意:这种方式有延时,会存在如App1.0.1已上线,但是获取的版本号还是1.0.0。所以还是推荐去后台记录版本号

iOS更新提示比较简单,不需要后台记录版本号,直接去App Store获取最新版本即可。

NSString * url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppID];//替换为自己App的ID
// 获取本地版本号
NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
// 网络请求获取最新版本
[[HttpBaseManager getSharedManager] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray * results = responseObject[@"results"];
        if (results && results.count>0)
        {
            NSDictionary * dic = results.firstObject;
            NSString * lineVersion = dic[@"version"];//版本号
            NSString * releaseNotes = dic[@"releaseNotes"];//更新说明
            //NSString * trackViewUrl = dic[@"trackViewUrl"];//链接
            //把版本号转换成数值
            NSArray * array1 = [currentVersion componentsSeparatedByString:@"."];
            NSInteger currentVersionInt = 0;
            if (array1.count == 3)//默认版本号1.0.0类型
            {
                currentVersionInt = [array1[0] integerValue]*100 + [array1[1] integerValue]*10 + [array1[2] integerValue];
            }
            NSArray * array2 = [lineVersion componentsSeparatedByString:@"."];
            NSInteger lineVersionInt = 0;
            if (array2.count == 3)
            {
                lineVersionInt = [array2[0] integerValue]*100 + [array2[1] integerValue]*10 + [array2[2] integerValue];
            }
            if (lineVersionInt > currentVersionInt)//线上版本大于本地版本
            {
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本%@",lineVersion] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction * ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                UIAlertAction * update = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //跳转到App Store
                    NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",AppID];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
                }];
                [alert addAction:ok];
                [alert addAction:update];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"版本更新出错,%@",error.description);
    }];

你可能感兴趣的:(iOS版本更新提示)