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