iOS 检查更新

 1 #pragma mark - 检查更新

 2 

 3 - (void)checkUpdateWithAPPID:(NSString *)APPID

 4 {

 5     //获取当前应用版本号

 6     NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];

 7     NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];

 8     

 9     NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];

10     NSURL *updateUrl = [NSURL URLWithString:updateUrlString];

11     versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];

12     [versionRequest setRequestMethod:@"GET"];

13     [versionRequest setTimeOutSeconds:60];

14     [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];

15     

16     //loading view

17     CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在检查更新..."];

18     checkingAlertView.userInteractionEnabled = YES;

19     [self.navigationController.view addSubview:checkingAlertView];

20     [checkingAlertView release];

21     

22     [versionRequest setCompletionBlock:^{

23         

24         [checkingAlertView removeFromSuperview];

25         

26         NSError *error = nil;

27         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];

28         if (!error) {

29             if (dict != nil) {

30                 //            DLog(@"dict %@",dict);

31                 int resultCount = [[dict objectForKey:@"resultCount"] integerValue];

32                 if (resultCount == 1) {

33                     NSArray *resultArray = [dict objectForKey:@"results"];

34                     //                DLog(@"version %@",[resultArray objectAtIndex:0]);

35                     NSDictionary *resultDict = [resultArray objectAtIndex:0];

36                     //                DLog(@"version is %@",[resultDict objectForKey:@"version"]);

37                     NSString *newVersion = [resultDict objectForKey:@"version"];

38                     

39                     if ([newVersion doubleValue] > [currentVersion doubleValue]) {

40                         NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];

41                         newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];

42                         DLog(@"newVersionUrl is %@",newVersionURlString);

43                         //                    if ([newVersionURlString hasPrefix:@"https"]) {

44                         //                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];

45                         //                    }

46                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新", nil];

47                         alertView.tag = 1000;

48                         [alertView show];

49                         [alertView release];

50                     }else

51                     {

52                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

53                         alertView.tag = 1001;

54                         [alertView show];

55                         [alertView release];

56                     }

57                 }

58             }

59         }else

60         {

61             DLog("error is %@",[error debugDescription]);

62         }

63     }];

64     

65     [versionRequest setFailedBlock:^{

66         [checkingAlertView removeFromSuperview];

67         

68         CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失败,请稍候再试!"];

69         [self.navigationController.view addSubview:alertView];

70         [alertView release];

71         [alertView selfRemoveFromSuperviewAfterSeconds:1.0];

72     }];

73     

74     [versionRequest startSynchronous];

75 }

76 

77 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

78 {

79     DLog(@"newVersionUrl  is %@",newVersionURlString);

80     if (buttonIndex) {

81         if (alertView.tag == 1000) {

82             if(newVersionURlString)

83             {

84                 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];

85             }

86         }

87     }

88 }

 

你可能感兴趣的:(ios)