直接上代码:
(1)定义属性
NSString *_appID;
NSString *_latestVersion;
NSString *_trackViewUrl;
NSString *_trackName;
(2)//检查更新,版本信息
[self versionUpdateWithAppID:_appID];
(3)#pragma mark 版本信息
-(void)versionUpdateWithAppID:(NSString *)appID{
//通过同步请求,解析json数据,得到数据
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appID];
NSLog(@"请求版本网址 %@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"error:%@",[error description]);
}
NSArray *resultsArray = [appInfoDic objectForKey:@"results"];
if (![resultsArray count]) {
NSLog(@"error: resultsArray == nil");
}
NSDictionary *infoDic = [resultsArray objectAtIndex:0];
//这里需要 version trackName trackViewUrl 三个属性
_latestVersion = [infoDic objectForKey:@"version"];
_trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];
_trackName = [infoDic objectForKey:@"trackName"];
//获取此应用的版本号
NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
NSLog(@"应用版本号:====%@",currentVersion);
double doubleCurrentVersion = [currentVersion doubleValue];
double doubleUpdateVersion = [_latestVersion doubleValue];
//两个点得,最后那个是无效的
if (doubleCurrentVersion < doubleUpdateVersion) {
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",_trackName];
NSString *messageStr = [NSString stringWithFormat:@"发现新版本( %@ ),是否升级?",_latestVersion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alert.tag = [appID intValue];
[alert show];
}else{
// NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",_trackName];
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",@"远程医疗"];
UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:titleStr message:@"暂无新版本" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
// alert3.tag = [appID intValue]+1;
[alert3 show];
}
}
(4)如果有新的版本,在弹出框的确定按钮点击,跳转下载界面
//如果有新的版本,那么就直接跳转到下载页面,这里就用到了trackViewUrl,路径,直接请求即可
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:_trackViewUrl]];
备注:参考资料
从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:
(1)定义属性
NSString *_appID;
NSString *_latestVersion;
NSString *_trackViewUrl;
NSString *_trackName;
(2)//检查更新,版本信息
[self versionUpdateWithAppID:_appID];
(3)#pragma mark 版本信息
-(void)versionUpdateWithAppID:(NSString *)appID{
//通过同步请求,解析json数据,得到数据
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appID];
NSLog(@"请求版本网址 %@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"error:%@",[error description]);
}
NSArray *resultsArray = [appInfoDic objectForKey:@"results"];
if (![resultsArray count]) {
NSLog(@"error: resultsArray == nil");
}
NSDictionary *infoDic = [resultsArray objectAtIndex:0];
//这里需要 version trackName trackViewUrl 三个属性
_latestVersion = [infoDic objectForKey:@"version"];
_trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];
_trackName = [infoDic objectForKey:@"trackName"];
//获取此应用的版本号
NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
NSLog(@"应用版本号:====%@",currentVersion);
double doubleCurrentVersion = [currentVersion doubleValue];
double doubleUpdateVersion = [_latestVersion doubleValue];
//两个点得,最后那个是无效的
if (doubleCurrentVersion < doubleUpdateVersion) {
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",_trackName];
NSString *messageStr = [NSString stringWithFormat:@"发现新版本( %@ ),是否升级?",_latestVersion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alert.tag = [appID intValue];
[alert show];
}else{
// NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",_trackName];
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",@"远程医疗"];
UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:titleStr message:@"暂无新版本" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
// alert3.tag = [appID intValue]+1;
[alert3 show];
}
}
(4)如果有新的版本,在弹出框的确定按钮点击,跳转下载界面
//如果有新的版本,那么就直接跳转到下载页面,这里就用到了trackViewUrl,路径,直接请求即可
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:_trackViewUrl]];
备注:参考资料
从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:
{
resultCount = 1;
results = (
{
artistId = 开发者 ID;
artistName = 开发者名称;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 审查名称;
trackContentRating = 评级;
trackId = 应用程序 ID;
trackName = 应用程序名称;
trackViewUrl = 应用程序介绍网址;
userRatingCount = 用户评级;
userRatingCountForCurrentVersion = 1;
version = 版本号;
wrapperType = software;
}
);
}