app版本更新全过程ios

1.工具类,主要是发送数据请求的,因为你需要从apple那请求到你当前的版本信息

.h

typedef void (^successBlock)(id responseObject, NSUInteger backCode);


typedef void (^failureBlock)(NSError *error);
 

+ (void)POST:(NSString *)URLString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure;

//从apple请求app版本

+(void)versionUpdateRemindingCompleted:(void(^)(id responseObject))completion;

 

.m


//从网络上请求版本

+(void)versionUpdateRemindingCompleted:(void(^)(id responseObject))completion{

    NSString *urlString = @"https://itunes.apple.com/CN/lookup?id=1262952xxx";
    [self POST:urlString parameters:nil success:^(id responseObject, NSUInteger backCode) {
        if (responseObject) {
            
            NSArray *results = responseObject[@"results"];
            if (results.count > 0) {
                NSDictionary *appInfo = [results objectAtIndex:0];
                NSString *version= appInfo[@"version"];
                if (version) {
                    completion(version);
                    NSLog(@"%@",version);
                }else{
                    completion(nil);
                }
                
            }
            completion(nil);
            
        }else{
        
            completion(nil);
        }

        
    } failure:^(NSError *error) {
       
        [MBProgressHUD showError:@"请求数据失败"];
        
    }];

}

 

+ (void)POST:(NSString *)URLString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure {
    [self showActivitevie:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//        NSUInteger backCode = [json[@"code"] unsignedIntegerValue];
        if (success) {
            success(json, nil);
            [self showActivitevie:NO];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
            [self showActivitevie:NO];
        }
    }];

}

 

 

 

 

2.在VC中具体的升级跳转操作

#pragma mark - 升级操作

-(void)versionChanged{
    
    @weakify(self);
    [WJRequstTool  versionUpdateRemindingCompleted:^(id responseObject) {
        @strongify(self);
        if (responseObject) {
            NSString *onlineVersion = responseObject;
            //获取当前版本
            NSString *currentVersion = [self getCurrentVersion];
            //接下来需要网上和和本地做对比
                
            BOOL needUpdate = [self needUpdate:onlineVersion compareCurrentVersion:currentVersion];
            if (needUpdate) {
                  //显示弹出框
                  [self showAlertViewToRemindUserUpdateApp];
            }else{
                   return;
            }
                  
          }   
      } 
  }];

}

//获取当前版本
-(NSString *)getCurrentVersion{
    
    
    NSString *version = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSLog(@"%@",version);
    return version;

}
//判断是否需要更新
-(BOOL)needUpdate:(NSString *)serverVersion compareCurrentVersion:(NSString *)currentVersion{
    
    NSArray *arrayNewVersion = [serverVersion componentsSeparatedByString:@"."];
    NSArray *arrayOldVersion = [currentVersion componentsSeparatedByString:@"."];
    NSInteger count = arrayOldVersion.count > arrayNewVersion.count ? arrayNewVersion.count:arrayOldVersion.count;
    
    for (int i =0; i< count; i++) {
        
        NSString *newVersionStr = arrayNewVersion[i];
        NSString *oldVersionStr = arrayOldVersion[i];
        if ([newVersionStr integerValue] > [oldVersionStr integerValue]) {
            return YES;
        }else if ([newVersionStr integerValue] < [oldVersionStr integerValue]){
            return NO;
        }
    }
    
    if (arrayOldVersion.count < arrayNewVersion.count) {
        return YES;
    }
    return NO;
    
}





-(void)showAlertViewToRemindUserUpdateApp{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"升级提醒" message:@"全新的云后勤已经准备完毕,此版本需要重新下载和安装" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"立即更新" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"立即更新");
        NSString *urlStr = @"https://itunes.apple.com/cn/app/%E4%BA%91%E5%90%8E%E5%8B%A4/id1262952xxx?mt=8";
        
        
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr]];
        
        
        
    }];
    
    [alertVC addAction:cancelAction];
    [alertVC addAction:updateAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
    
    
}

 

3.最后在需要的地方调用[self versionChanged];就可以了

 

 

 

 

你可能感兴趣的:(ios)