iOS 强制版本更新

版本强制进行更新

NSString *url = UPDATEVERSION(@"0");
    [BaseRequest be_baseRequestModel:nil url:url finishBlock:^(id responseObject, NSError *error) {
        if ([responseObject[@"status"] integerValue] == 200) {
            if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dataDic = responseObject[@"data"];
                int backgroundVerison ;
                if ([dataDic[@"version"] isKindOfClass:[NSNull class]]) {
                    backgroundVerison = 100;
                }else {
                    //后台返回的版本号
                    backgroundVerison = [self versionExchangeType:dataDic[@"version"]];
                }
                //版本更新的url
                _updateUrl = dataDic[@"url"];
                //本地的版本号
                int localVersion = [self versionExchangeType:[self getSystemVersion]];
                //判断两个版本是否相同
                if ( localVersion< backgroundVerison) {
                    NSLog(@"需要跳转去更新版本:%@",dataDic[@"version"]);
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"发现新版本" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                    alertView.tag = 100;
                    [alertView show];
                }else {
                    NSLog(@"无版本可更新。。");
                }
            }
        }
    }];

//获取当前设备中应用的版本号
- (NSString *)getSystemVersion {
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    return currentVersion;
}
//版本号去点
- (int)versionExchangeType:(NSString *)version {
    
    //modify dingyouyou 2017-05-06
    NSArray * subArr = [version componentsSeparatedByString:@"."];
    if (subArr.count > 0) {
        NSString * value = @"";
        for (int i = 0; i< subArr.count;i++ ) {
            value = [value stringByAppendingString:subArr[i]];
        }
        return value.intValue;
    }else{
        return 0;
    }
}
强制更新需要在app将要进入前台的时候再一次,调用网络请求的方法
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    //版本检测
    [self checkVerisonRequest];
}

你可能感兴趣的:(iOS 强制版本更新)