iOS 版本检测更新

2D7EC396-8BC8-42DB-B713-72FB4F3A909D.png
    //1 获取当前项目版本号:
    //[self toast:@"当前已经是最新版本"];
    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];//currentVersion为当前工程项目版本号
    
    
    //2 联网检测项目在AppStore上的版本号
    NSString *storeAppID = @"1171012077";//配置自己项目在商店的ID
    NSError *error;
    NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",storeAppID]]] returningResponse:nil error:nil];
    if (response == nil) {
        NSLog(@"你可能没有连接网络哦");
        return;
    }
    NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    if (error) {
        NSLog(@"hsUpdateAppError:%@",error);
        return;
    }
    NSLog(@"可输出一下看看%@",appInfoDic);
    NSArray *array = appInfoDic[@"results"];
    if (array.count < 1) {
        NSLog(@"此APPID为未上架的APP或者查询不到");
        return;
    }
    NSDictionary *dic = array[0];
    //商店版本号
    NSString *appStoreVersion = dic[@"version"];
    
    
    //3 比较版本号,跳转到手机自带的AppStore项目页面供用户下载:
    
    NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
    //设置版本号,主要是为了区分不同的版本,比如有1.2.1、1.2、1.31各种类型
    currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (currentVersion.length==2) {
        currentVersion  = [currentVersion stringByAppendingString:@"0"];
    }else if (currentVersion.length==1){
        currentVersion  = [currentVersion stringByAppendingString:@"00"];
    }
    appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (appStoreVersion.length==2) {
        appStoreVersion  = [appStoreVersion stringByAppendingString:@"0"];
    }else if (appStoreVersion.length==1){
        appStoreVersion  = [appStoreVersion stringByAppendingString:@"00"];
    }
    
    //4当前版本号小于商店版本号,就更新
    if([currentVersion floatValue] < [appStoreVersion floatValue])
    {
        UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", storeAppID]];
            [[UIApplication sharedApplication] openURL:url];
        }];
        UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alercConteoller addAction:actionYes];
        [alercConteoller addAction:actionNo];
        [self presentViewController:alercConteoller animated:YES completion:nil];
    }else{
        NSLog(@"版本号好像比商店大噢!检测到不需要更新");
    }

iOS7开始就添加了应用自动更新,该提示更新适用于用户关闭了自动更新应用功能的情况下,现实中很多iPhone用户都不会关闭手机的自动更新应用功能,故基本不用考虑应用有新版本而用户不知道这种问题。因为手机会自动下载新版本的应用替换旧的版本。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 每个正在迭代更新的项目,都必不可少的要检测是否发布了最新的版本,来保证以最优化的版本服务用户。其实我并不赞成一打开...
    南方_H阅读 1,758评论 4 23
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,257评论 4 61
  • 入坑的时间是16年的5月12日... 记得很清楚那天快递到后发现快递盒子皱到不行,还好里面东西是好的.... 马赛...
    白色花开梦里花落阅读 795评论 4 8
  • 上一节中我们获取并编译了GLFW和GLEW这两个开源库,现在我们就可以使用它们来创建一个OpenGL绘图窗口了。 ...
    IceMJ阅读 1,967评论 0 1
  • 一别经年又一轮,明月清辉空映身。长忆旧时私语事,相思迢递金陵城。何年江畔笑谈中,簧竹长风无故人。
    眠月_121阅读 216评论 0 0