Check if my IOS application is updated

前端 未结 8 1071
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 18:07

I need to check when my app launches if it was being updated, because i need to make a view that only appears when the app is firstly installed to appear again after being

8条回答
  •  有刺的猬
    2020-12-05 18:35

    swift version with an important improvement over the accepted answer:

    • using infoDictionary instead of objectForInfoDictionaryKey guaranties that the result is independent from device language, otherwise you may end up in some rare cases believing that there is an upgrade when in reality it is just a device language change
    • using a UserDefaults key identical to the main Bundle infoDictionary for clarity on what is exactly stored
    • factoring setting currentVersion code
    • Swift 3 syntax

    Code:

        let standardUserDefaults = UserDefaults.standard
        let shortVersionKey = "CFBundleShortVersionString"
        let currentVersion = Bundle.main.infoDictionary![shortVersionKey] as! String
        let previousVersion = standardUserDefaults.object(forKey: shortVersionKey) as? String
        if previousVersion == currentVersion {
            // same version
        } else {
            // replace with `if let previousVersion = previousVersion {` if you need the exact value
            if previousVersion != nil {
                // new version
            } else {
                // first launch
            }
            standardUserDefaults.set(currentVersion, forKey: shortVersionKey)
            standardUserDefaults.synchronize()
        }
    

提交回复
热议问题