/// @brief 获取到得最新版本号
@property (nonatomic ,copy) NSString *latestVersion;
/// @brief 获取到的APP下载地址
@property (nonatomic ,copy) NSString *trackViewUrl;
/// @brief 获取到得APP名称
@property (nonatomic ,copy) NSString *trackName;
/// @brief 本地APP的版本号
@property (nonatomic ,copy) NSString *currentVersion;
#pragma mark - 检查APP版本
- (void)checkAPPVersion {
//主要思路,对AppStore上的版本号与本地APP版本号进行对比
//从iTunes地址请求数据
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"1051232511"];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//appInfoDic中存放的是AppStore中APP的信息
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:responseoptions:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"error:%@",[error description]);
return ;
}
NSArray *resultsArray = [appInfoDic objectForKey:@"results"];
if (![resultsArray count]) {
NSLog(@"error: reslutsArray == nil");
return ;
}
//获取最新版本信息
NSDictionary *infoDic = [resultsArray objectAtIndex:0];
self.latestVersion = [infoDic objectForKey:@"version"];
self.trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];
self.trackName = [infoDic objectForKey:@"trackName"];
//获取本地APP最新版本信息
//infoDict中存放的是本地APP的info.plist信息
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
self.currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
if (![self.currentVersion isEqualToString:self.latestVersion]) {
NSString *messageStr = [NSString stringWithFormat:@"发现新版本(%@)",self.latestVersion];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:messageStr message:nil delegate:selfcancelButtonTitle:@"暂不升级" otherButtonTitles:@"立即升级", nil];
[alert show];
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
//跳转到AppStore相关下载页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
}
}