0

Sometimes cache cleanup is needed after a big update, so I'd like to check once the user updates the app if the app version is above or below the one that needs cache cleanup.

For example, I currently have this to check when it's a new app update or install:

import BundleInfoVersioning

func displayUpdateNotification() {
    let bundleInfoVersioning = BundleInfoVersioning(bundle: .main)
    
    bundleInfoVersioning.check(forKeyPath: "CFBundleShortVersionString") { (old: String? , newVersion: String?) in
        if old == nil {
            // You can place some analytics here ...
            print("🔥 New App Installed")
        }
        else {
            // You can place some analytics here ...
            print("🎉 New App Update")
            if(showUpdate) {
                self.shouldShowUpdatePopUp = true
            }
        }
    }
}

So if the user does an app update compare the installed version with the version defined by me that needs cache cleanup, how can I compare that string with the new version? Like:

if after update the user is in version 2.5.2 or below, clean cache once

1 Answer 1

2

So, I had a similar issue some time back, we needed to be able to compare the installed version with cloud based information and do "things"

The first thing I did was built a concept of a "version", which is based on "major.minor.patch" and "build" properties.

This allowed the cloud to send us a structure which we could understand.

struct Version: Comparable, CustomStringConvertible {
    
    let major: Int
    let minor: Int
    let patch: Int
    
    let build: Int
    
    init(major: Int, minor: Int, patch: Int, build: Int) {
        self.major = major
        self.minor = minor
        self.patch = patch
        self.build = build
    }
    
    init(from: String, build: String = "0")  {
        let buildNumber = Int(build) ?? 0
        let parts = from.split(separator: ".")
        let major = Int(parts[0]) ?? 0
        var minor = 0
        var patch = 0
        if parts.count >= 2 {
            minor = Int(parts[1]) ?? 0
        }
        if parts.count >= 3 {
            patch = Int(parts[2]) ?? 0
        }
        
        self.init(major: major, minor: minor, patch: patch, build: buildNumber)
    }
    
    var description: String {
        return String(format: "%02d.%02d.%02d build %02d", major, minor, patch, build)
    }
    

    static func <(lhs: Version, rhs: Version) -> Bool {
        return lhs.description.compare(rhs.description, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending
    }

    static func ==(lhs: Version, rhs: Version) -> Bool {
        return lhs.description.compare(rhs.description, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedSame
    }

}

Add in a simple extensions to get it from the app itself...

extension Version {
    
    static var app: Version = {
        let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        let build = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
        return Version(from: version, build: build)
    }()
    
}

And then you can do things like

if Version.app == Version(from: "2.5.2") {
    // And now do some stuff
}

Now, you will need to play around with this, as my needs might not be exactly the same as yours, but this is basically what I was able to use and it worked well, for my needs

But aren't you comparing 2 strings there? What if the user is in version 3.0.0 and I want all users with version 2.5.2 and below to clean cache? Can I do: if Version.app <= Version(from: "2.5.2") { } ?

This is one of those nice little features of Swift (actually ObjC 🤫). Have a look at what NSString.CompareOptions.numeric does for the comparison

For example...

let userVersion = Version(from: "3.0.0")
let targetVersion = Version(from: "2.5.2")

userVersion == targetVersion // 3.0.0 == 2.5.2 - false ✅
userVersion > targetVersion // 3.0.0 > 2.5.2 - true ✅
userVersion < targetVersion // 3.0.0 < 2.5.2 - false ✅
userVersion >= targetVersion // 3.0.0 >= 2.5.2 - true ✅
userVersion <= targetVersion // 3.0.0 <= 2.5.2 - false ✅
9
  • But aren't you comparing 2 strings there? What if the user is in version 3.0.0 and I want all users with version 2.5.2 and below to clean cache? Can I do: if Version.app <= Version(from: "2.5.2") { } ?
    – Arturo
    Feb 3, 2022 at 0:13
  • @Arturo You'd think so wouldn't you, but do some research into NSString.CompareOptions.numeric Feb 3, 2022 at 0:14
  • @Arturo This is probably where I stole ... I mean learnt it from 😉 Feb 3, 2022 at 0:20
  • It looks good, I'm testing it and thanks for the link, I'm looking at it too
    – Arturo
    Feb 3, 2022 at 0:21
  • 1
    @Arturo For me, the point was to get a "abstract" concept of the version which was independent of the cloud and the app, so I could then bind them together, because the cloud always has a different concept of how these things should be represented 😓 Feb 3, 2022 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.