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

前端 未结 30 1034
暖寄归人
暖寄归人 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:46

    Here's an updated version for Swift 3.2:

    extension UIApplication
    {
        static var appVersion:String
        {
            if let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")
            {
                return "\(appVersion)"
            }
            return ""
        }
    
        static var buildNumber:String
        {
            if let buildNum = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String)
            {
                return "\(buildNum)"
            }
            return ""
        }
    
        static var versionString:String
        {
            return "\(appVersion).\(buildNumber)"
        }
    }
    

提交回复
热议问题