搞定APP版本更新控制

来源:互联网 发布:可视化布局系统 源码 编辑:程序博客网 时间:2024/06/15 05:20

做iOS开发App很久了,今天想写写博客,把自己的一些想法、技术点、遇到问题写出来,供大家参考;

app版本更新是不能自己来检测的,否则上线的话会被拒;那如果还想自己控制版本更新的提醒怎么办呢,我用到了两种方法来解决这个问题:一种是完全由前端自己控制,首先获取到当前工程项目的版本号currentVersion,然后通过配置自己项目在商店的APPID,获取到商店新更新的项目版本号;最后两者做比较;一种是前端获取用户手机上当前版本,然后再通过调后台接口获取服务器上的版本号,两者进行比较;第二种方法的好处是后台可以给前端传一个字段,然后控制强制更新还是选择更新;废话少说,上代码。

第一种:

-(void)XFUpdateVersionApp

{

    //1.先获取当前工程项目版本号

   NSDictionary *proInfoDic=[[NSBundlemainBundle]infoDictionary];

    NSLog(@"%@",proInfoDic);

    NSString *currentVersion=infoDic[@"BundleShortVersionString"];//BundleShortVersionString在Info.plist里

//2.要先配置自己项目在商店的APPID

    //3从网络获取appStore版本号

    NSError *error;

    NSData *response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]returningResponse:nilerror:nil];//STOREAPPID项目的appid

    if (response ==nil) {

        NSLog(@"你没有连接网络哦");

        return;

    }

    NSDictionary *appInfoDic = [NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];

    if (error) {

        NSLog(@"hsUpdateAppError:%@",error);

        return;

    }

//    NSLog(@"%@",appInfoDic);

    NSArray *array = appInfoDic[@"results"];

    

    if (array.count <1) {

        NSLog(@"App未提交");

        return;

    }

    NSDictionary *dic = array[0];

    NSString *appStoreVersion = dic[@"version"];

    //打印版本号

    NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);

    //设置版本号

    currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

    if (currentVersion.length==2) {

        currentVersion  = [currentVersion stringByAppendingString:@"0"];

    }elseif (currentVersion.length==1){

        currentVersion  = [currentVersion stringByAppendingString:@"00"];

    }

    appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

    if (appStoreVersion.length==2) {

        appStoreVersion  = [appStoreVersion stringByAppendingString:@"0"];

    }elseif (appStoreVersion.length==1){

        appStoreVersion  = [appStoreVersion stringByAppendingString:@"00"];

    }

    

    //4当前版本号小于商店版本号,就更新

    if([currentVersionfloatValue] < [appStoreVersionfloatValue])

    {

        UIAlertController *alercConteoller = [UIAlertControlleralertControllerWithTitle:@"版本有更新"message:[NSStringstringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]]preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *actionYes = [UIAlertActionactionWithTitle:@"更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

            //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下

            NSURL *url = [NSURLURLWithString:[NSStringstringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8",STOREAPPID]];

            [[UIApplicationsharedApplication]openURL:url];

        }];

        UIAlertAction *actionNo = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

            

        }];

        [alercConteoller addAction:actionYes];

        [alercConteoller addAction:actionNo];

        [selfpresentViewController:alercConteolleranimated:YEScompletion:nil];

    }else{

        NSLog(@"版本号好像比商店大");

    }

}

最后调用一下

-(void)viewDidAppear:(BOOL)animated{

    [superviewDidAppear:animated];

    //一句代码实现检测更新,很简单哦 一定要在这个方法里面调用

    [selfXFUpdateVersionApp];

}

这里说明一下上面的比较版本时版本号格式是1.0.3这种单位数的格式,如果是遇到01.01.01这种格式的就可以用下面这种比较方法:


第二种:

#pragma mark - 更新版本

- (void)VersionCheck

{

    //1.当前手机app版本号AppVersion本地宏定义一个,每次发版时手动改 01.01.01格式的

    //2.请求服务器上的最新版本号 这是网络请求

    [JiangServiceApiVersionCheck:2

     UsingCallback:^(DCServiceContext* context,VersionCheck* sm) {

         if (context.isSucceeded && sm.result ==1) { //请求成功

             NSMutableArray* versionArray = [NSMutableArrayarray];

             for (VersionCheckData* objin sm.data) {

                 self.downStr = obj.path_add;

                if (sm.data ==nil) {

                    

                 }else {

                     [versionArrayaddObject:obj];

//当前手机自定义app版本号与服务器上的最新版本号进行比较

                     if ([AppVersion2compare:obj.versionnooptions:NSNumericSearch] ==NSOrderedAscending) { //升序AppVersion<obj.versionno

                        //提示用户升级

                        if ([obj.ppgradetypeisEqualToString:@"2"]) {//选择更新

                            UIAlertView* alter = [[UIAlertViewalloc]

                                                  initWithTitle:TipTitle

                                                  message:@"您当前版本过低请及时更新"

                                                  delegate:nil

                                                  cancelButtonTitle:[selfcancelBtnTitle]

                                                  otherButtonTitles:[selfotherBtnTitle],nil];

                             alter.delegate =self;

                             [altershow];

                         }

                        else {// 1强制更新

                            UIAlertView* alter = [[UIAlertViewalloc]

                                                  initWithTitle:TipTitle

                                                  message:@"当前版本过低请更新"

                                                  delegate:nil

                                                  cancelButtonTitle:[selfcancelBtnTitle]

                                                  otherButtonTitles:nil,nil];

                             alter.delegate =self;

                             [altershow];

                         }

                     }

                     else if ([AppVersion2compare:obj.versionno

                                          options:NSNumericSearch]

                              ==NSOrderedDescending) {//降序AppVersion>obj.versionno

                        NSLog(@"已是高版本不需要操作");

                     }

                     else { // AppVersion=obj.versionno

                        NSLog(@"相等");不作操作

                     }

                 }

             }

         }

         else {//失败

             //            [MBProgressHUD showError:@"请稍后重试"

             //          toView:self.view];

         }

     }];

}


1 0
原创粉丝点击