美文网首页
iOS 本地版本和服务器版本比较

iOS 本地版本和服务器版本比较

作者: _Waiting_ | 来源:发表于2021-12-24 16:06 被阅读0次

    本地版本和服务版本比较,纯数字比较

    //
    //  HanVersionJudge.h
    //
    //
    //  Created by han on 2021/12/24.
    //
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef enum : NSUInteger {
        VersionError = -1,//版本异常
        VersionLower = 0,//比本地版本低
        VersionHigher,//比本地版本高
        VersionSame,//版本一致
    } VersionState;
    
    @interface HanVersionJudge : NSObject
    
    /// 判断传入的版本和本地版本
    /// 仅适用于x.xx.xxx纯数字版本号
    /// @param version 版本号
    +(VersionState)compareLocalVersionWith:(NSString *)version;
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //
    //  HanVersionJudge.m
    //  HanTest4
    //
    //  Created by han on 2021/12/24.
    //
    
    #import "HanVersionJudge.h"
    
    @implementation HanVersionJudge
    +(VersionState)compareLocalVersionWith:(NSString *)version{
        NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
        NSArray *localVerArr = [localVersion componentsSeparatedByString:@"."];
        NSArray *verArr = [version componentsSeparatedByString:@"."];
        
        BOOL isNum1 = [self isNumber:localVersion];
        BOOL isNum2 = [self isNumber:version];
        
        if (isNum1 == NO || isNum2 == NO) {
            return VersionError;
        }
        
        if ([version isEqualToString:localVersion]) {
            return VersionSame;
        }
        
        if (verArr.count < localVerArr.count) {
            return VersionLower;
        }else if (verArr.count > localVerArr.count) {
            return VersionHigher;
        } else{
            for (int i = 0; i < localVerArr.count ; i++) {
                NSString *locVer = localVerArr[i];
                NSString *ver = verArr[i];
                if ([locVer integerValue] > [ver integerValue] ) {
                    return VersionLower;
                }else if ([locVer integerValue] < [ver integerValue] ) {
                    return VersionHigher;
                }
            }
        }
        
        return VersionLower;
    }
    + (BOOL)isNumber:(NSString *)strValue
    {
        if (strValue == nil || [strValue length] <= 0)
        {
            return NO;
        }
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
        NSString *filtered = [[strValue componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        
        if (![strValue isEqualToString:filtered])
        {
            return NO;
        }
        return YES;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS 本地版本和服务器版本比较

          本文链接:https://www.haomeiwen.com/subject/cmnlqrtx.html