1. 程式人生 > >版本號判斷應用是否更新

版本號判斷應用是否更新

版本號對比

- (BOOL)compareVersionWithServerVersin:(NSString *)serverVersion {
    //獲取當前程式版本號
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
 
    DLog(@"currentVersion:%@",currentVersion)
    DLog(@"ServerVersion:%@",serverVersion)
    NSArray *currentV = [currentVersion componentsSeparatedByString:@"."];
    NSArray *serverV = [serverVersion componentsSeparatedByString:@"."];
    NSMutableArray *currentVerArray = [NSMutableArray arrayWithArray:currentV];
    NSMutableArray *serverVerArray = [NSMutableArray arrayWithArray:serverV];
    
    if (currentVerArray.count != serverVerArray.count) {
        NSInteger max = (currentVerArray.count > serverVerArray.count)? currentVerArray.count : serverVerArray.count;
        if (currentVerArray.count < max) {
            for (NSInteger i = currentVerArray.count; i < max; i++) {
                [currentVerArray addObject:@"0"];
            }
        }else {
            for (NSInteger i = serverVerArray.count; i < max; i++) {
                [serverVerArray addObject:@"0"];
            }
        }
    }
    
    for (NSInteger i = 0; i < currentVerArray.count; i++) {
        NSInteger currentChar = [currentVerArray[i] integerValue];
        NSInteger serverChar = [serverVerArray[i] integerValue];
        if (currentChar < serverChar) {
            return YES;
        }else if (currentChar > serverChar) {
            return NO;
        }
    }
    return NO;
}