`

检查更新

    博客分类:
  • 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. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics