iOS版本更新

版本更新
#pragma mark - 检查版本更新

- (void)checkNewVersion {

//    NSString *url = @"https://itunes.apple.com/cn/app/ya-zi-kan-hu-ji-qi-ren/id1067342148?mt=8";

     NSString *url = @"http://itunes.apple.com/cn/lookup?id=1067342148";  //  ******************根据自己的应用改URL ******************

   

    [GJWHttpTool getWithURL:url params:nil success:^(id json) {

        // 请求成功

        // 字典接收请求到的JSon

        NSDictionary *reslut = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil];

        NSDictionary *responseDict = [NSDictionary dictionaryWithDictionary:reslut];

        // 解析请求到的JSon

        NSArray *results =[responseDict objectForKey:@"results"];

        NSDictionary *finalDict = [results firstObject];

        // 获取APP下载地址

        NSString *trackViewUrl =[finalDict objectForKey:@"trackViewUrl"];

        updateUrl = [NSURL URLWithString:trackViewUrl];

        // 获取官网APP的版本号

        NSString *str1 = [finalDict objectForKey:@"version"];

        NSString *version1 = [str1 substringFromIndex:2];

        // 将版本号字符串转换成float类型

        float newVersion = [version1floatValue];

       

        // 获取本地项目版本号

        // 拿到项目的infoPlist文件中所有内容

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

        // 获取到当前工程版本号

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

        NSString *version2 = [str2 substringFromIndex:2];

        // 将版本号字符串转换成float类型

        float localVersion =[version2 floatValue];

        // 对比两处版本号

        if (newVersion >localVersion) {

 

        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"新版本 %@ 已发布!",str1] delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        alert.delegate = self;

        [alert addButtonWithTitle:@"前往更新"];

        [alert show];

        alert.tag = 20;

    } else {

        [[[UIAlertView alloc] initWithTitle:nil message:@"已是最高版本" delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil] show];

    }

    

    } failure:^(NSError *error) {

        // 返回请求失败的原因

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

    }];

   

   

}

 

#pragma mark – UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

    if (buttonIndex ==1 & alertView.tag ==20) {


        [[UIApplication sharedApplication] openURL:updateUrl];

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值