现在的位置: 首页 > 综合 > 正文

更新

2018年01月16日 ⁄ 综合 ⁄ 共 2963字 ⁄ 字号 评论关闭

-(void)gengxin
{
   
   // NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
   // NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
   // NSLog(@"%@",appVersion);
    NSString* num=@"821457534";
   [self checkUpdateWithAPPID:num];
    
   }
//-(void)clearCacheSuccess
//{ [HUD removeFromSuperview];
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"清理完毕" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//    [alert show];
//}
- (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];
    ASIFormDataRequest*versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];
    [versionRequest setRequestMethod:@"GET"];
    [versionRequest setTimeOutSeconds:60];
    [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.dimBackground = NO;
    HUD.labelText = @"正在检查更新...";
    [HUD show:YES];
    
    
    
    [versionRequest setCompletionBlock:^{
        
        [HUD removeFromSuperview];
        
        NSError *error = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];
         NSLog(@"dict %@",dict);
        if (!error) {
            if (dict != nil) {
                
                int resultCount = [[dict objectForKey:@"resultCount"] integerValue];
                if (resultCount == 1) {
                    NSArray *resultArray = [dict objectForKey:@"results"];
                   
                    NSDictionary *resultDict = [resultArray objectAtIndex:0];
                    
                    NSString *newVersion = [resultDict objectForKey:@"version"];
                    
                    if ([newVersion doubleValue] > [currentVersion doubleValue]) {
                       NSString *msg = [NSString stringWithFormat:@"最新版本为%@,是否更新?",newVersion];
                        newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"立即更新",nil];
                        alertView.tag = 1000;
                        [alertView show];
                       
                    }else
                    {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                        alertView.tag = 1001;
                        [alertView show];
                       ;
                    }
                }
            }
        }else
        {
           
        }
    }];
    
    [versionRequest setFailedBlock:^{
        [HUD removeFromSuperview];
        
       
    }];
    
    [versionRequest startSynchronous];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  
    if (buttonIndex) {
        if (alertView.tag == 1000) {
            if(newVersionURlString)
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];
            }
        }
    }

抱歉!评论已关闭.