iOS - 比较应用程序版本

7

我希望在App Store上更新我的应用。当应用程序在更新后首次打开时,我想要同步一些内容。因此,我需要一种方法来判断是否是更新后的第一次启动。

我想到的解决方案是:将应用程序版本存储在NSUserDefaults中,如下所示:

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

现在我有oldVersioncurrentVersion,我需要比较它们。我想知道oldVersion是否小于currentVersion。但是它们都是字符串。我该如何检查oldVersion < currentVersion
我知道我可以检查它们是否不相等。但我希望为将来的更新做好准备。因为也许我想要对这两个版本执行的同步将来会与第3个版本等不同。

在比较后转换为浮点值或者您能做的任何操作... - Dharma
有一个非常好的GitHub库可以解决你的问题:https://github.com/mysterioustrousers/MTMigration - Clafou
但是这个数字可以像这样:1.0.5 - 这不是一个浮点数。 - bobsacameno
这个回答解决了你的问题吗?在Objective-C中比较版本号 - Cœur
3个回答

11
你可以使用自然排序(与词典排序不同,它将认为1.10在1.1之后)来比较数字版本号,方法如下:
BOOL isNewer = ([currentVersion compare:oldVersion options:NSNumericSearch] == NSOrderedDescending)

这个也会处理像1.0.5和1.0.10这样的数字吗? - bobsacameno
2
是的...但不要相信我,写一个测试吧! - Clafou
它运行得非常好,谢谢。我已经寻找这个很长时间了。 - Mohit

1

Swift 4

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

这意味着强制更新版本高于当前版本。

0
作为字符串扩展的 Swift 3.0 代码,适用于包含额外零的版本。(例如:1.0.01.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 }

```


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接