7

I want to update my app on the App Store. When the app is first opened after the update, I want it to sync some stuff. Therefore I need a way to see if it's the first launch after the update.

The solution I thought of is: storing the app version in the NSUserDefaults like this:

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

now I have the oldVersion and the currentVersion and all I need to do is to compare them. I want to know if the oldVersion is smaller the currentVersion. But they are strings. how can I check if oldVersion < currentVersion?

I know I can just check if they are not equal. But I want to be prepared for future updates. Because maybe the syncing I want to perform for this 2 will be different for version 3 and so on.

Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
bobsacameno
  • 765
  • 3
  • 10
  • 25
  • change to float value after compare or what ever you can do... – Dharma Dec 16 '14 at 12:03
  • There is a very nice library on GitHub for solving your problem: https://github.com/mysterioustrousers/MTMigration – Clafou Dec 16 '14 at 12:06
  • but this number can be like this: `1.0.5` - which is not a float. – bobsacameno Dec 16 '14 at 12:06
  • Does this answer your question? [Compare version numbers in Objective-C](https://stackoverflow.com/questions/1978456/compare-version-numbers-in-objective-c) – Cœur Mar 20 '23 at 18:31

3 Answers3

11

You can compare numeric version numbers using natural sort order (which will consider 1.10 to come after 1.1, unlike lexicographic sort order) as follows:

BOOL isNewer = ([currentVersion compare:oldVersion options:NSNumericSearch] == NSOrderedDescending)
Clafou
  • 15,250
  • 7
  • 58
  • 89
1

Swift 4

if cuurentVersionString.compare(forceUpdate, options: .numeric) == .orderedAscending {
            print("force")
            state = .force
        }

That means that the force update version is higher then the current

ironRoei
  • 2,049
  • 24
  • 45
0

Code in Swift 3.0 as String extension, that works with versions that contains extra zeros.(Ex: 1.0.0 & 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 }

```

Paolo Musolino
  • 614
  • 7
  • 9