ios 比对应用版本号,确定是否需要显示更新

  //将版本号按照.切割后存入数组中


//假设:

NSString * local_version = @"1.2.3"; 手机上app的版本;

            NSString * app_version = @"1.1"; 最新的版本号;


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

            NSArray *appArray = [app_version componentsSeparatedByString:@"."];

            NSInteger minArrayLength = MIN(localArray.count, appArray.count);

            

            for(int i=0;i<minArrayLength;i++){//以最短的数组长度为遍历次数,防止数组越界

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

                NSString *localElement = localArray[i];

                NSString *appElement = appArray[i];

                

                NSInteger  localValue =  localElement.integerValue;

                NSInteger  appValue = appElement.integerValue;

                

                if(localValue<appValue) {

                    //从前往后比较数字大小,一旦分出大小,跳出循环

                    needUpDate = YES;

                    break;

                }else{

                    needUpDate = NO;

                }

            }

            if (!needUpDate && [appArray count] > minArrayLength)

            {

                for (int i = minArrayLength; i < [appArray count]; i++)

                {

                    if ([appArray[i] integerValue] > 0) {

                        needUpDate = YES;

                        break;

                    }

                }

            }



猜你喜欢

转载自blog.csdn.net/klong27/article/details/73467767