iOS 版本更新

版本号比较,如下:

- (BOOL)compareVersionWithRemoteV:(NSString *)remoteStr localV:(NSString *)localStr {
    // 如果版本号相同,直接返回NO
    if ([remoteStr isEqualToString:localStr]) {
        return NO;
    }
    
    NSMutableArray *remoteArr = [[NSMutableArray alloc] init];
    NSMutableArray *locatArr = [[NSMutableArray alloc] init];
    [remoteArr addObjectsFromArray:[remoteStr componentsSeparatedByString:@"."]];
    [locatArr addObjectsFromArray:[localStr componentsSeparatedByString:@"."]];
    
    NSInteger maxLen = remoteArr.count;
    if (remoteArr.count < locatArr.count) {
        maxLen = locatArr.count;
    }
    
    while (remoteArr.count < maxLen) {
        [remoteArr addObject:@"0"];
    }
    while (locatArr.count < maxLen) {
        [locatArr addObject:@"0"];
    }
    
    for (int i = 0; i < maxLen; i++) {
        NSString *remote = remoteArr[i];
        NSString *local = locatArr[i];
        
        if (remote.intValue > local.intValue) {
            return YES;
        } else if (remote.intValue < local.intValue) {
            return NO;
        }
    }
    return NO;
}

 

调用判断

1     BOOL isUpdate = [self compareVersionWithRemoteV:@"2.0" localV:@"2.0.1"];
2     NSLog(@"%@",isUpdate ? @"有更新":@"无更新");

 

posted @ 2020-10-09 15:26  Louis~Lu  阅读(128)  评论(0编辑  收藏  举报