iOS —— 检查版本更新
2015-05-05 15:43
375 查看
iOS 检查版本更新
#pragma mark - 检查更新
- (void)checkUpdateWithAPPID:(NSString *)APPID
{
//获取当前应用版本号
NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];
NSURL *updateUrl = [NSURL URLWithString:updateUrlString];
versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];
[versionRequest setRequestMethod:@"GET"];
[versionRequest setTimeOutSeconds:60];
[versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];
//loading view
CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在检查更新..."];
checkingAlertView.userInteractionEnabled = YES;
[self.navigationController.view addSubview:checkingAlertView];
[checkingAlertView release];
[versionRequest setCompletionBlock:^{
[checkingAlertView removeFromSuperview];
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];
if (!error) {
if (dict != nil) {
// DLog(@"dict %@",dict);
int resultCount = [[dict objectForKey:@"resultCount"] integerValue];
if (resultCount == 1) {
NSArray *resultArray = [dict objectForKey:@"results"];
// DLog(@"version %@",[resultArray objectAtIndex:0]);
NSDictionary *resultDict = [resultArray objectAtIndex:0];
// DLog(@"version is %@",[resultDict objectForKey:@"version"]);
NSString *newVersion = [resultDict objectForKey:@"version"];
if ([newVersion doubleValue] > [currentVersion doubleValue]) {
NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];
newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];
DLog(@"newVersionUrl is %@",newVersionURlString);
// if ([newVersionURlString hasPrefix:@"https"]) {
// [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];
// }
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新", nil nil];
alertView.tag = 1000;
[alertView show];
[alertView release];
}else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil nil];
alertView.tag = 1001;
[alertView show];
[alertView release];
}
}
}
}else
{
DLog("error is %@",[error debugDescription]);
}
}];
[versionRequest setFailedBlock:^{
[checkingAlertView removeFromSuperview];
CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失败,请稍候再试!"];
[self.navigationController.view addSubview:alertView];
[alertView release];
[alertView selfRemoveFromSuperviewAfterSeconds:1.0];
}];
[versionRequest startSynchronous];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
DLog(@"newVersionUrl is %@",newVersionURlString);
if (buttonIndex) {
if (alertView.tag == 1000) {
if(newVersionURlString)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];
}
}
}
}
#pragma mark - 检查更新
- (void)checkUpdateWithAPPID:(NSString *)APPID
{
//获取当前应用版本号
NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];
NSURL *updateUrl = [NSURL URLWithString:updateUrlString];
versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];
[versionRequest setRequestMethod:@"GET"];
[versionRequest setTimeOutSeconds:60];
[versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];
//loading view
CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在检查更新..."];
checkingAlertView.userInteractionEnabled = YES;
[self.navigationController.view addSubview:checkingAlertView];
[checkingAlertView release];
[versionRequest setCompletionBlock:^{
[checkingAlertView removeFromSuperview];
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];
if (!error) {
if (dict != nil) {
// DLog(@"dict %@",dict);
int resultCount = [[dict objectForKey:@"resultCount"] integerValue];
if (resultCount == 1) {
NSArray *resultArray = [dict objectForKey:@"results"];
// DLog(@"version %@",[resultArray objectAtIndex:0]);
NSDictionary *resultDict = [resultArray objectAtIndex:0];
// DLog(@"version is %@",[resultDict objectForKey:@"version"]);
NSString *newVersion = [resultDict objectForKey:@"version"];
if ([newVersion doubleValue] > [currentVersion doubleValue]) {
NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];
newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];
DLog(@"newVersionUrl is %@",newVersionURlString);
// if ([newVersionURlString hasPrefix:@"https"]) {
// [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];
// }
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新", nil nil];
alertView.tag = 1000;
[alertView show];
[alertView release];
}else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil nil];
alertView.tag = 1001;
[alertView show];
[alertView release];
}
}
}
}else
{
DLog("error is %@",[error debugDescription]);
}
}];
[versionRequest setFailedBlock:^{
[checkingAlertView removeFromSuperview];
CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失败,请稍候再试!"];
[self.navigationController.view addSubview:alertView];
[alertView release];
[alertView selfRemoveFromSuperviewAfterSeconds:1.0];
}];
[versionRequest startSynchronous];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
DLog(@"newVersionUrl is %@",newVersionURlString);
if (buttonIndex) {
if (alertView.tag == 1000) {
if(newVersionURlString)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];
}
}
}
}
相关文章推荐
- iOS 版本更新检查
- ios检查版本更新
- IOS应用版本更新如果保留检查更新入口审核时将被拒绝
- ios检查版本更新
- iOS开发之检查APP版本更新
- ios检查版本更新
- IOS 版本检查更新
- IOS 检查版本更新
- ios 实现版本更新检查
- iOS检查版本更新、APPStore更新地址和评价地址
- iOS开发之检查APP版本更新
- ios检查版本更新
- ios 实现版本更新检查
- ios 检查版本更新
- ios检查版本更新
- iOS检查版本更新,以及获取不到版本信息问题
- iOS【 APP检查版本更新 】
- iOS学习笔记18- iOS检查版本更新
- iOS学习笔记31—appStore检查版本更新与从app里跳到appstore评论页面
- iOS 【获取 AppStore 的上架信息 - 检查版本更新】