ios 使用ASIHTTPRequest来检查版本更新

 1 - (void) alertWithTitle: (NSString *)_title_  msg:(NSString *)msg delegate:(id)_delegate cancelButtonTitle:(NSString*)_cancelTitle otherButtonTitles:(NSString*)_otherTitles{
 2     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
 3                                                     message:msg
 4                                                    delegate:_delegate
 5                                           cancelButtonTitle:_cancelTitle
 6                                           otherButtonTitles:_otherTitles,nil];
 7     [alert show];
 8 }
 9 
10 #pragma mark - update
11 -(void)checkUpdate{
12     MBKAppDelegate *mbkApp = (MBKAppDelegate *)[[UIApplication sharedApplication] delegate];
13     if (![mbkApp networkIsReach]) {
14         return;
15     }
16     
17     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kAppId]];
18     ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url];   
19     [request setDelegate:self];
20     [request setDidFinishSelector:@selector(requestDone:)];
21     [request setDidFailSelector:@selector(requestWentWrong:)];
22     [request startSynchronous];
23 }
24 
25 #pragma mark - asihttprequest 
26 - (void)requestDone:(ASIHTTPRequest *)request
27 {
28     NSError *err = nil;
29     NSDictionary *dictionary =
30     [[CJSONDeserializer deserializer] deserializeAsDictionary:[request responseData] error:&err];
31     if (err != nil) {
32         [self failure];
33         return;
34     }
35     NSArray *a = [dictionary objectForKey:@"results"];
36     if (a.count == 0) {
37         [self failure];
38         return;
39     }
40     NSDictionary *d = [a objectAtIndex:0];
41     NSString *newVersion = [d objectForKey:@"version"];
42     NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
43     NSString *nowVersion = [infoDict objectForKey:@"CFBundleVersion"];
44     NSString *msg = @"";
45     NSString *cancelTitle = @"";
46     NSString *otherTitles = @"";
47     if(![nowVersion isEqualToString:newVersion])
48     {
49         msg = @"版本有更新";
50         cancelTitle = @"取消";
51         otherTitles = @"更新";
52     }else{        
53         msg = @"已经是最新版本啦";
54         cancelTitle = @"";
55         otherTitles = nil;
56     }
57     [self alertWithTitle:nil msg:msg delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:otherTitles];
58 }
59 
60 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
61     if(buttonIndex==1){
62         NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/qun-xiang-dao/id%@?ls=1&mt=8",kAppId]];
63         [[UIApplication sharedApplication]openURL:url];
64     }
65 }
66 
67 - (void)requestWentWrong:(ASIHTTPRequest *)request
68 {
69     [self failure];
70 }
71 
72 - (void)failure{    
73     [self alertWithTitle:nil msg:@"检查失败了" delegate:self cancelButtonTitle:@"" otherButtonTitles:nil];
74 }

转:http://my.oschina.net/brucezcq/blog/148905

原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3285117.html