iOS版本更新判断

上线时版本更新判断:上线时需要后端返回更新与否的标志、强制更新与否的标志、然后需要App端做相应的处理、需要更新时、方法如下。

方法一、根据本地版本号和AppStore请求的版本好字符串作分割处理、将分割的字符串进行循环比较、比较结果返回更新与否。

    //通过网络获取AppStore版本号

    NSString *appStoreVersion = @"1.0.1";

    //本地项目中的版本号

 NSString * localVersion = [[[NSBundle mainBundleinfoDictionaryobjectForKey:@"CFBundleShortVersionString"];

    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];

    NSArray *versionArray = [appStoreVersion componentsSeparatedByString:@"."];

    //取最短数据的那个、以防数组越界

    NSInteger minArray = MIN(localArray.count, versionArray.count);

    //是否更新标志

    NSInteger isUpdate = NO;

    for(NSInteger i = 0; i < minArray;i++){

        //取出每个部分的字符串值,比较数值大小

        NSInteger localValue = [localArray[i] integerValue];

        NSInteger versionValue = [versionArray[i]integerValue];

        if (localValue > versionValue) {

            break;

        }else if(localValue < versionValue) {

            isUpdate = YES;

            break;

        }else{

            continue;

        }

    }

    //软硬更新、弹框提醒、更新应用、跳转至AppStore

    //Method Or AlertView

    if (isUpdate) {

        

    }


方法二、获取到本地版本号和AppStore版本号后直接字符串间取数字比较、不用分割

    NSString *localVersion = @"2.1.1";

    NSString *appStoreVersion = @"1.5";

    //NSNumericSearch = 64,

    /* Added in 10.2; Numbers within strings are compared using numeric value,

     that is, Foo2.txt < Foo7.txt < Foo25.txt;

     only applies to compare methods, not find */

    if ([localVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedDescending) { //弹出提示更新弹框

        NSLog(@"更新");

        

    }


方法三:如果完全由接口返回值管控、不需要App端做其他操作的情况下、直接根据更新与否的标志就可版本强制更新了、

    BOOL isUpdate = YES;//该字段值由接口请求而来。

    //当两者不想等时、进入更新与否判断

    if (![localVersion isEqualToString:appStoreVersion]) {

        if (isUpdate) {

            //更新操作;

        }

    }


猜你喜欢

转载自blog.csdn.net/SharkToping/article/details/80204413