IOS 检查更新

#pragma mark -检查更新

- (void)checkUpdateWithAPPID:(NSString*)APPID

{

//获取当前应用版本号

NSDictionary*appInfo = [[NSBundlemainBundle]infoDictionary];

NSString*currentVersion = [appInfoobjectForKey:@"CFBundleVersion"];

NSString*updateUrlString = [NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];

NSURL*updateUrl = [NSURLURLWithString:updateUrlString];

versionRequest = [ASIFormDataRequestrequestWithURL:updateUrl];

[versionRequestsetRequestMethod:@"GET"];

[versionRequestsetTimeOutSeconds:60];

[versionRequestaddRequestHeader:@"Content-Type"value:@"application/json"];

//loading view

CustomAlertView*checkingAlertView = [[CustomAlertViewalloc]initWithFrame:NAVIGATION_FRAMEstyle:CustomAlertViewStyleDefaultnoticeText:@"正在检查更新..."];

checkingAlertView.userInteractionEnabled =YES;

[self.navigationController.viewaddSubview:checkingAlertView];

[checkingAlertViewrelease];

[versionRequestsetCompletionBlock:^{

[checkingAlertViewremoveFromSuperview];

NSError*error =nil;

NSDictionary*dict = [NSJSONSerializationJSONObjectWithData:[versionRequestresponseData]options:NSJSONReadingMutableContainerserror:&error];

if(!error) {

if(dict !=nil) {

//            DLog(@"dict %@",dict);

intresultCount = [[dictobjectForKey:@"resultCount"]integerValue];

if(resultCount == 1) {

NSArray*resultArray = [dictobjectForKey:@"results"];

//                DLog(@"version %@",[resultArray objectAtIndex:0]);

NSDictionary*resultDict = [resultArrayobjectAtIndex:0];

//                DLog(@"version is %@",[resultDict objectForKey:@"version"]);

NSString*newVersion = [resultDictobjectForKey:@"version"];

if([newVersiondoubleValue] > [currentVersiondoubleValue]) {

NSString*msg = [NSStringstringWithFormat:@"最新版本为%@,是否更新?",newVersion];

newVersionURlString = [[resultDictobjectForKey:@"trackViewUrl"]copy];

DLog(@"newVersionUrl is %@",newVersionURlString);

//                    if ([newVersionURlString hasPrefix:@"https"]) {

//                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];

//                    }

UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"暂不"otherButtonTitles:@"立即更新",nilnil];

alertView.tag = 1000;

[alertViewshow];

[alertViewrelease];

}else

{

UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"您使用的是最新版本!"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定",nilnil];

alertView.tag = 1001;

[alertViewshow];

[alertViewrelease];

}

}

}

}else

{

DLog("error is %@",[errordebugDescription]);

}

}];

[versionRequestsetFailedBlock:^{

[checkingAlertViewremoveFromSuperview];

CustomAlertView*alertView = [[CustomAlertViewalloc]initWithFrame:NAVIGATION_FRAMEstyle:CustomAlertViewStyleWarningnoticeText:@"操作失败,请稍候再试!"];

[self.navigationController.viewaddSubview:alertView];

[alertViewrelease];

[alertViewselfRemoveFromSuperviewAfterSeconds:1.0];

}];

[versionRequeststartSynchronous];

}

- (void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex

{

DLog(@"newVersionUrl  is %@",newVersionURlString);

if(buttonIndex) {

if(alertView.tag == 1000) {

if(newVersionURlString)

{

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:newVersionURlString]];

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容