Ruslan Serebriakov
Ruslan Serebriakov

Reputation: 642

Comparing versions in xcode 7

I have this code to compare current and available version:

NSString *currentVersion = [[NSString alloc]initWithFormat:@"%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];

    NSError *error = nil;
    NSData *avaliableVersionData = [NSData dataWithContentsOfURL:[NSURL URLWithString:
                                                  @"http://update.blockade.tech/current-version"] options:0 error:&error];
    NSString *avaliableVersion = [[NSString alloc]initWithData:avaliableVersionData encoding:NSUTF8StringEncoding];
    if ([avaliableVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {
        NSLog(@"lower");
    } else {
        NSLog(@"ok");
    }

But when those versions are same(0.1.2 and 0.1.2), it write than current version is lower. Why is it?

Upvotes: 0

Views: 73

Answers (1)

Danny Daglas
Danny Daglas

Reputation: 1491

The web result contains a newline. Strip out the newline after the initWithData:... call and before the if statement with:

avaliableVersion = [avaliableVersion stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Upvotes: 1

Related Questions