简体   繁体   中英

How to detect First launch of app on every new version upgrade?

I have a requirement of detecting the first launch of app after the user upgrades the app to a newer version. I need to perform certain task only on first launch of app after the user upgrades the app to a new version. Many links available online but none answer clearly to my query. How to achieve this in Swift 2 , iOS 9.

Most of the answers available says to maintain a key in NSUserDefaults and set its value to false and after first launch make it true. But the problem is after I upgrade my app the variable still will be true and thus my scenario fails on app upgrade. Any help would be much appreciated. Thanks!

Try this:

    let existingVersion = NSUserDefaults.standardUserDefaults().objectForKey("CurrentVersionNumber") as? String
    let appVersionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String

    if existingVersion !=  appVersionNumber {
        NSUserDefaults.standardUserDefaults().setObject(appVersionNumber, forKey: "CurrentVersionNumber")
        NSUserDefaults.standardUserDefaults().synchronize()
       //You can handle your code here
    }

updating Yogesh's perfect, yet simple solution to swift 4

    let existingVersion = UserDefaults.standard.object(forKey: "CurrentVersionNumber") as? String
    let appVersionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

    if existingVersion != appVersionNumber {
        print("existingVersion = \(String(describing: existingVersion))")
        UserDefaults.standard.set(appVersionNumber, forKey: "CurrentVersionNumber")

 // run code here.

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM