检查版本更新的方法

中心点:比较线上的版本号的每一位 与现在程序中的每一位是否相同  从后到前不同位置更新的程度不同。提示的语言也不同。中间的参数kStoreAppId 是APPID。


#pragma mark 检查版本更新

+(void)checkAppUpdate

{

//线程

    dispatch_queue_t appleQueue = dispatch_queue_create("apple", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(appleQueue, ^{

        //进入appleQueue的子线程了,在这儿爱干嘛干嘛,记得干完了回主线程

        NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

        

        NSString *nowVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kStoreAppId]];

        NSString * file =  [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

        if (file==nil||file.length==0) {

            return;

        }

        NSDictionary *dicFile=[self dictionaryWithJsonString:file];

        if (dicFile==nil) {

            return;

        }

        NSArray *arrResult=[dicFile objectForKey:@"results"];

        if (arrResult==nil||arrResult.count==0) {

            return;

        }

        NSDictionary *dicResult=[arrResult objectAtIndex:0];

        if (dicResult==nil) {

            return;

        }

        if (![[dicResult allKeys]containsObject:@"version"]) {

            return;

        }

        ///appStore版本

        NSString *newVersion =[dicResult objectForKey:@"version"];

        NSInteger lastVersionF = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];

        NSInteger lastVersionS = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:1] integerValue];

        NSInteger lastVersionT = [[[newVersion componentsSeparatedByString:@"."] objectAtIndex:2] integerValue];

        ///本地版本

        NSInteger nowVersionF=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];

        NSInteger nowVersionS=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:1] integerValue];

        NSInteger nowVersionT=[[[nowVersion componentsSeparatedByString:@"."] objectAtIndex:2] integerValue];

        NSString *str_Title=@"";

        NSString *newDes=@"";

        if (nowVersionF < lastVersionF) {

            str_Title=@"软件全面更新提示";

            newDes=[NSString stringWithFormat:@"系统全面升级\n请重新下载新版本应用!\n 更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];

            alert.tag=tag_AlertView_CheckVersion;

            [alert show];

            return;

        }

        if (nowVersionF <= lastVersionF && nowVersionS < lastVersionS) {

            str_Title=@"软件重要更新提示";

            newDes=[NSString stringWithFormat:@"有新的版本更新,是否前往更新?\n更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];

            alert.tag=tag_AlertView_CheckVersion;

            [alert show];

            return;

        }

        if (nowVersionF <= lastVersionF && nowVersionS <= lastVersionS && nowVersionT < lastVersionT) {

            str_Title=@"软件优化更新提示";

            newDes=[NSString stringWithFormat:@"有新的版本更新,是否前往更新?\n 更新内容:\n %@",[dicResult objectForKey:@"releaseNotes"]];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str_Title message:newDes delegate:self cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];

            alert.tag=tag_AlertView_CheckVersion;

            [alert show];

            return;

        }


        //完事儿了,回主线程

        dispatch_async(dispatch_get_main_queue(), ^{

        });

    });

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值