ios 检测版本更新

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lxlzy/article/details/81736607

//1.先获取当前工程项目版本号
    
    NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
    
    NSString *currentVersion = infoDic[@"CFBundleShortVersionString"];
    
    //2.从网络获取appStore版本号
    
    NSError *error;

NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest  requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",@"1380561588"]]] returningResponse:nil error:nil];
    
    //2.1没有内容
    
    if (response == nil) {
        
        NSLog(@"你没有连接网络哦");
        
        return;
        
    }
    
    //3.序列化解析
    
    NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    
    //3.1数据错误
    
    if (error) {
        
        NSLog(@"hsUpdateAppError:%@",error);
        
        return;
        
    }
    
    //3.2字典解析
    
    NSArray *array = appInfoDic[@"results"];
    
    NSDictionary *dic = array[0];
    
    NSString *appStoreVersion = dic[@"version"];
    
    
    
    //打印版本号
    
    NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
    
    
    
    //4.当前版本号小于商店版本号,就更新
    
    if([currentVersion floatValue] < [appStoreVersion floatValue])
        
    {

 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本有更新"  message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新",nil];
        
        [alert show];
        
    }else{
        
        NSLog(@"检测到不需要更新");
        
    }

猜你喜欢

转载自blog.csdn.net/lxlzy/article/details/81736607