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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值