---
title: "检查更新  "
author: "Firepix"
date: "2014-05-30T16:09:56Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2014-02-18T17:29:32Z"
id: 2018783
url: "https://firepix.iteye.com/blog/2018783"
---

01. #pragma mark - 检查更新
02. - (void)checkUpdateWithAPPID:(NSString \*)APPID
03. {
04. //获取当前应用版本号
05. NSDictionary \*appInfo = \[\[NSBundle mainBundle] infoDictionary];
06. NSString \*currentVersion = \[appInfo objectForKey:@"CFBundleVersion"];
07. NSString \*updateUrlString = \[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];
08. NSURL \*updateUrl = \[NSURL URLWithString:updateUrlString];
09. 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] &gt; \[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. }