IOS - application version comparison

I want to update my application on the App Store. When the application opens first after the update, I want it to sync some things. So I need a way to check if it is the first one after the update.

The solution I was thinking of is this: save the version of the application NSUserDefaultsas follows:

NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"appVersion"];
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"appVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];

I now have oldVersionand currentVersion, and all I have to do is compare them. I want to know if less oldVersion currentVersion. But these are strings. how can i check if oldVersion < currentVersion?

I know that I can just check if they are equal. But I want to be ready for future updates. Because maybe the synchronization I want to do for this 2 will be different for version 3, etc.

+4
source share
2 answers

You can compare the numerical version numbers using the natural sort order (which will consider the value 1.10 after 1.1, as opposed to the lexicographic sort order) as follows:

BOOL isNewer = ([currentVersion compare:oldVersion options:NSNumericSearch] == NSOrderedDescending)
+8
source

Code in Swift 3.0 as a line extension that works with versions containing extra zeros. (Example: 1.0.0and 1.0)

`` ``

/// Inner comparison utility to handle same versions with different length. (Ex: 1.0.0 & 1.0)
private func compare(toVersion targetVersion: String) -> ComparisonResult {

    let versionDelimiter = "."
    var result: ComparisonResult = .orderedSame
    var versionComponents = components(separatedBy: versionDelimiter)
    var targetComponents = targetVersion.components(separatedBy: versionDelimiter)
    let spareCount = versionComponents.count - targetComponents.count

    if spareCount == 0 {
        result = compare(targetVersion, options: .numeric)
    } else {
        let spareZeros = repeatElement("0", count: abs(spareCount))
        if spareCount > 0 {
            targetComponents.append(contentsOf: spareZeros)
        } else {
            versionComponents.append(contentsOf: spareZeros)
        }
        result = versionComponents.joined(separator: versionDelimiter)
            .compare(targetComponents.joined(separator: versionDelimiter), options: .numeric)
    }
    return result
}

public func isVersion(equalTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedSame }
public func isVersion(greaterThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedDescending }
public func isVersion(greaterThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedAscending }
public func isVersion(lessThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedAscending }
public func isVersion(lessThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedDescending }

`` ``

0
source

All Articles