iOS开发之检查更新

检查更新这个功能,在iOS开发中也是比较常见的,下面总结一下我自己的实现:
#pragma mark - 检查更新
- (void)checkUpdateWithAPPID:(NSString *)APPID
{
    //获取当前应用版本号
    NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];    
    NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
    
    NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];
    NSURL *updateUrl = [NSURL URLWithString:updateUrlString];
    versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];
    [versionRequest setRequestMethod:@"GET"];
    [versionRequest setTimeOutSeconds:60];
    [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];
    
    //loading view
    CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在检查更新..."];
    checkingAlertView.userInteractionEnabled = YES;
    [self.navigationController.view addSubview:checkingAlertView];
    [checkingAlertView release];
    
    [versionRequest setCompletionBlock:^{
        
        [checkingAlertView removeFromSuperview];
        
        NSError *error = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];
        if (!error) {
            if (dict != nil) {
                //            DLog(@"dict %@",dict);
                int resultCount = [[dict objectForKey:@"resultCount"] integerValue];
                if (resultCount == 1) {
                    NSArray *resultArray = [dict objectForKey:@"results"];
                    //                DLog(@"version %@",[resultArray objectAtIndex:0]);
                    NSDictionary *resultDict = [resultArray objectAtIndex:0];
                    //                DLog(@"version is %@",[resultDict objectForKey:@"version"]);
                    NSString *newVersion = [resultDict objectForKey:@"version"];
                    
                    if ([newVersion doubleValue] > [currentVersion doubleValue]) {
                        NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];
                        newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];
                        DLog(@"newVersionUrl is %@",newVersionURlString);
                        //                    if ([newVersionURlString hasPrefix:@"https"]) {
                        //                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];
                        //                    }
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新", nil];
                        alertView.tag = 1000;
                        [alertView show];
                        [alertView release];
                    }else
                    {
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                        alertView.tag = 1001;
                        [alertView show];
                        [alertView release];
                    }
                }
            }
        }else
        {
            DLog("error is %@",[error debugDescription]);
        }
    }];
    
    [versionRequest setFailedBlock:^{
        [checkingAlertView removeFromSuperview];
        
        CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失败,请稍候再试!"];
        [self.navigationController.view addSubview:alertView];
        [alertView release];
        [alertView selfRemoveFromSuperviewAfterSeconds:1.0];
    }];
    
    [versionRequest startSynchronous];  
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    DLog(@"newVersionUrl  is %@",newVersionURlString);
    if (buttonIndex) {
        if (alertView.tag == 1000) {
            if(newVersionURlString)
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值