iOS 版本更新

一、根据AppStore上的数据进行判断是否更新

苹果给了我们一个接口,可以根据应用的id请求应用的一些信息,取出其中的版本与当前运行的应用的版本号比较。

//同步请求获取应用的信息字典
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *releaseInfo = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSArray *resultArr = releaseInfo[@"results"];
NSDictionary *resultDict = resultArr[0];
//获取需要的version,trackViewUrl(更新应用的地址),trackName
NSString *latestVersion = [resultDict objectForKey:@"version"];
NSString *trackViewUrl1 = [resultDict objectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString *trackName = [resultDict objectForKey:@"trackName"];//trackName
//获取应用当前的版本号
        NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];  
        double doubleCurrentVersion = [currentVersion doubleValue]; 
        double doubleUpdateVersion = [latestVersion doubleValue]; 
//根据版本号比较判断是否更新           
        if (doubleCurrentVersion < doubleUpdateVersion) {  

            UIAlertView *alert;  
            alert = [[UIAlertView alloc] initWithTitle:trackName                                                 message:@"有新版本,是否升级!"    delegate: self     cancelButtonTitle:@"取消"  otherButtonTitles: @"升级", nil];  
            alert.tag = 1001;  
            [alert show];  
        }  
        else{  
            UIAlertView *alert;  
            alert = [[UIAlertView alloc] initWithTitle:trackName  
                                               message:@"暂无新版本"  
                                              delegate: nil  
                                     cancelButtonTitle:@"好的"  
                                     otherButtonTitles: nil, nil];  
            [alert show];  
        } 
 //如果需要更新,就跳转到下载页面,trackViewUrl是全路径,直接请求
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]]; 

二、根据后台返回的数据进行判断是否更新

这种方法可以用在每次启动应用程序的时候做一次判断是否更新,可以根据后台返回的数据里取出版本号,与运行的应用当前的版本号做对比,或者当版本更新的时候后台返回一个bool类型的字段,判断下是否需要更新。

PS:注意:当ios做更新版本的时候,如果有更新版本的按钮,如果只是显示版本号而没有点击响应事件的时候最好按钮置灰,否则审核的时候可能会审核不通过,若可以点击响应,则该更新按钮要和当前页面的其他按钮界面保持一致。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值