#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]];
}
}
}
}