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
swift version with an important improvement over the accepted answer:
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 changeCode:
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()
}