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

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

    Swift 5 as UIApplication extension

    extension UIApplication {
        static var release: String {
            return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? ?? "x.x"
        }
        static var build: String {
            return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String? ?? "x"
        }
        static var version: String {
            return "\(release).\(build)"
        }
    }
    

    example use:

    print("release: \(UIApplication.release)")
    print("build: \(UIApplication.build)")
    print("version: \(UIApplication.version)")
    

提交回复
热议问题