检查更新功能

检查更新这个功能,在iOS开发中也是比较常见的,下面总结一下我自己的实现:
  1. #pragma mark - 检查更新   
  2. - (void)checkUpdateWithAPPID:(NSString *)APPID  
  3. {  
  4.     //获取当前应用版本号   
  5.     NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];      
  6.     NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];  
  7.       
  8.     NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];  
  9.     NSURL *updateUrl = [NSURL URLWithString:updateUrlString];  
  10.     versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];  
  11.     [versionRequest setRequestMethod:@"GET"];  
  12.     [versionRequest setTimeOutSeconds:60];  
  13.     [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];  
  14.       
  15.     //loading view   
  16.     CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在检查更新..."];  
  17.     checkingAlertView.userInteractionEnabled = YES;  
  18.     [self.navigationController.view addSubview:checkingAlertView];  
  19.     [checkingAlertView release];  
  20.       
  21.     [versionRequest setCompletionBlock:^{  
  22.           
  23.         [checkingAlertView removeFromSuperview];  
  24.           
  25.         NSError *error = nil;  
  26.         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];  
  27.         if (!error) {  
  28.             if (dict != nil) {  
  29.                 //            DLog(@"dict %@",dict);   
  30.                 int resultCount = [[dict objectForKey:@"resultCount"] integerValue];  
  31.                 if (resultCount == 1) {  
  32.                     NSArray *resultArray = [dict objectForKey:@"results"];  
  33.                     //                DLog(@"version %@",[resultArray objectAtIndex:0]);   
  34.                     NSDictionary *resultDict = [resultArray objectAtIndex:0];  
  35.                     //                DLog(@"version is %@",[resultDict objectForKey:@"version"]);   
  36.                     NSString *newVersion = [resultDict objectForKey:@"version"];  
  37.                       
  38.                     if ([newVersion doubleValue] > [currentVersion doubleValue]) {  
  39.                         NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];  
  40.                         newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];  
  41.                         DLog(@"newVersionUrl is %@",newVersionURlString);  
  42.                         //                    if ([newVersionURlString hasPrefix:@"https"]) {   
  43.                         //                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];   
  44.                         //                    }   
  45.                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新", nil nil];  
  46.                         alertView.tag = 1000;  
  47.                         [alertView show];  
  48.                         [alertView release];  
  49.                     }else  
  50.                     {  
  51.                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil nil];  
  52.                         alertView.tag = 1001;  
  53.                         [alertView show];  
  54.                         [alertView release];  
  55.                     }  
  56.                 }  
  57.             }  
  58.         }else  
  59.         {  
  60.             DLog("error is %@",[error debugDescription]);  
  61.         }  
  62.     }];  
  63.       
  64.     [versionRequest setFailedBlock:^{  
  65.         [checkingAlertView removeFromSuperview];  
  66.           
  67.         CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失败,请稍候再试!"];  
  68.         [self.navigationController.view addSubview:alertView];  
  69.         [alertView release];  
  70.         [alertView selfRemoveFromSuperviewAfterSeconds:1.0];  
  71.     }];  
  72.       
  73.     [versionRequest startSynchronous];    
  74. }  
  75.   
  76. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  77. {  
  78.     DLog(@"newVersionUrl  is %@",newVersionURlString);  
  79.     if (buttonIndex) {  
  80.         if (alertView.tag == 1000) {  
  81.             if(newVersionURlString)  
  82.             {  
  83.                 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];  
  84.             }  
  85.         }  
  86.     }  
  87. }  
#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]];
            }
        }
    }
}
posted on 2014-08-05 15:08  我靠是时候那个了  阅读(388)  评论(0编辑  收藏  举报