版本适配

下面是关于版本检测更新的介绍:

首先,我们要获得等待上传或正在写的app项目的版本号显示在view上

[objc]  view plain copy print ?
  1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
  2.    CFShow((__bridge CFTypeRef)(infoDic));  
  3.    NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];  
我们得到的NSString就是版本号了


然后我们要获得上个版本在app store 上的信息:

[objc]  view plain copy print ?
  1. //检查是否需要更新  
  2. -(void)examineAppUpdate  
  3. {  
  4.     NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
  5.     //CFShow((__bridge CFTypeRef)(infoDic));  
  6.     NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];  
  7.       
  8.     NSString *URL = @"http://itunes.apple.com/lookup?id=<span style="color:#cc33cc;">ID号</span>";  
  9.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  
  10.     [request setURL:[NSURL URLWithString:URL]];  
  11.     [request setHTTPMethod:@"POST"];  
  12.     NSHTTPURLResponse *urlResponse = nil;  
  13.     NSError *error = nil;  
  14.     NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
  15.     NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];  
  16.     NSLog(@"dic = %@",dic);  

然后我们通过json解析得到的数据为:

[objc]  view plain copy print ?
  1. );  
  2.          trackCensoredName = "\U6367\U8179\U7b11\U8bdd";  
  3.          trackContentRating = "17+";  
  4.          trackId = 493760196;  
  5.          trackName = "\U6367\U8179\U7b11\U8bdd";  
  6.          trackViewUrl = "https://itunes.apple.com/us/app/peng-fu-xiao-hua/id<span style="color:#ff0000;">APP的id</span>?mt=8&uo=4";  
  7.          userRatingCount = 5;  
  8.          userRatingCountForCurrentVersion = 3;  
  9.          version = "V1.08";  
  10.          wrapperType = software;  
  11.      }  

我们就得到了我们要的app的信息,然后继上边的dic之后进行比较和操作:

[objc]  view plain copy print ?
  1. NSArray *infoArray = [dic objectForKey:@"results"];  
  2.    if ([infoArray count]) {  
  3.        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
  4.        NSString *lastVersion = [releaseInfo objectForKey:@"version"];  
  5.        NSLog(@"%@",lastVersion);  
  6.          
  7.        if (![lastVersion isEqualToString:currentVersion]) {  
  8.            //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];  
  9.            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil nil];  
  10.            alert.tag = 10000;  
  11.            [alert show];  
  12.        }  
  13.        else  
  14.        {  
  15.            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  16.            alert.tag = 10001;  
  17.            [alert show];  
  18.        }  
  19.    }  

最后点击确定进入此app所在的app store上的页面:

[objc]  view plain copy print ?
  1. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     if (alertView.tag==10000) {  
  4.         if (buttonIndex==1) {  
  5.             NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=<span style="color:#cc0000;">app的ID</span>"];  
  6.             [[UIApplication sharedApplication]openURL:url];  
  7.         }  
  8.     }  
  9.   
  10. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值