您的位置:首页 > 移动开发 > IOS开发

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

2017-09-28 11:11 375 查看
[objc] view
plain copy

前几周,凡是包含了“检查更新”按钮的iOS应用程序均被拒绝上架了,探其原因,发现iOS8之后的系统都可以自动给应用程序升级。大概是苹果方面认为让软件开发商再做一个“检查更新”功能有点多余。不过站在程序员的角度上讲,“检查更新”功能被认为是用户使用次数最少,但偏偏pm们特别钟爱的一个功能。pm非要加,但苹果审核非要拒绝你的app,怎么办呢?其实,苹果现在还没有严苛到不能在程序中出现一丁点和检查更新功能的代码,经过我的大胆尝试,只要不把“检查更新”几个字显示出来就行。那。。。问题来了,没有这个按钮,该如何检查更新呢?傻呀~在app每启动一次,就去检查更新一次不就完了吗?哈哈哈哈,耍个小聪明,成功的将app上线了。  

[objc] view
plain copy

#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 nil];  

                    alert.tag = 100001;  

                    [alert show];  

                }  

                // 祝版本号没有更新,不强制更新  

                else {  

                    //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];  

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil nil];  

                    alert.tag = 100002;  

                    [alert show];  

                }  

            }  

              

            // 没有新版本  

            else  

            {  

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, 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) {  

          

    }  

  

}  



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐