ios开发之应用程序检查更新功能的实现

前几周,凡是包含了“检查更新”按钮的iOS应用程序均被拒绝上架了,探其原因,发现iOS8之后的系统都可以自动给应用程序升级。大概是苹果方面认为让软件开发商再做一个“检查更新”功能有点多余。不过站在程序员的角度上讲,“检查更新”功能被认为是用户使用次数最少,但偏偏pm们特别钟爱的一个功能。pm非要加,但苹果审核非要拒绝你的app,怎么办呢?其实,苹果现在还没有严苛到不能在程序中出现一丁点和检查更新功能的代码,经过我的大胆尝试,只要不把“检查更新”几个字显示出来就行。那。。。问题来了,没有这个按钮,该如何检查更新呢?傻呀~在app每启动一次,就去检查更新一次不就完了吗?哈哈哈哈,耍个小聪明,成功的将app上线了。
#pragma mark 检查更新
-(void)onCheckVersion:(UIButton *)sender
{
    _actV.hidden = NO;
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    //CFShow((__bridge CFTypeRef)(infoDic));
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    NSString *URL = @"http://itunes.apple.com/lookup?id=518966501";
    
    // 1. 准备url地址
    NSString *urlStr = URL;
    NSURL *url = [NSURL URLWithString:urlStr];
    
    
    // 2. 创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 2.1 请求方式(默认方式为GET,可不写)
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        _actV.hidden = YES;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *infoArray = [dic objectForKey:@"results"];
        if ([infoArray count]) {
            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *lastVersion = [releaseInfo objectForKey:@"version"];
            
            // 有新版本
            if ([lastVersion compare:currentVersion] > 0) {
                // 根据主版本号判断是否需要强制更新
                NSMutableString *cur1 = [NSMutableString stringWithString:currentVersion];
                NSMutableString *cur2 = [NSMutableString stringWithString:currentVersion];
                NSRange range = NSMakeRange(1, 4);
                [cur1 deleteCharactersInRange:range];
                NSInteger curFirst1 = [cur1 integerValue];
                NSRange range3 = NSMakeRange(0, 4);
                [cur2 deleteCharactersInRange:range3];
                NSInteger curLast = [cur2 intValue];
                
                
                NSMutableString *last1 = [NSMutableString stringWithString:lastVersion];
                NSMutableString *last2 = [NSMutableString stringWithString:lastVersion];
                [last1 deleteCharactersInRange:range];
                NSInteger lastFirst1 = [last1 integerValue];
                [last2 deleteCharactersInRange:range3];
                NSInteger lastLast = [last2 integerValue];
                
                
                // 主版本号更新大于1,强制更新
                if (lastFirst1 - curFirst1 >=1 || (curFirst1 == lastFirst1 && curLast != lastLast)) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"发现新版本,强烈建议您更新到最新版本,否则会影响您的正常使用" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                    alert.tag = 100001;
                    [alert show];
                }
                // 祝版本号没有更新,不强制更新
                else {
                    //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
                    alert.tag = 100002;
                    [alert show];
                }
            }
            
            // 没有新版本
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 100003;
                [alert show];
            }
        }
        
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 强制更新
    if (alertView.tag == 100001) {
        if (buttonIndex == 0) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/id518966501"]];
        }
        
    }
    // 非强制更新
    else if (alertView.tag == 100002) {
        if (buttonIndex == 0) {
            
        } else if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/lan-bi-tou/id935232659?mt=8"]];
        }
        
    }
    // 不需要更新
    else if (alertView.tag == 100003) {
        
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值