How do I get the App version and build number using Swift?

前端 未结 30 1005
暖寄归人
暖寄归人 2020-12-22 15:21

I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running.

How can I return the ver

30条回答
  •  暖寄归人
    2020-12-22 15:44

    Update for Swift 5

    here's a function i'm using to decide whether to show an "the app updated" page or not. It returns the build number, which i'm converting to an Int:

    if let version: String = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
            guard let intVersion = Int(version) else { return }
            
            if UserDefaults.standard.integer(forKey: "lastVersion") < intVersion {
                print("need to show popup")
            } else {
                print("Don't need to show popup")
            }
            
            UserDefaults.standard.set(intVersion, forKey: "lastVersion")
        }
    

    If never used before it will return 0 which is lower than the current build number. To not show such a screen to new users, just add the build number after the first login or when the on-boarding is complete.

提交回复
热议问题