ITeye博客

检查更新

Firepix · 2014-05-30 16:09 · ios · 移动开发 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。