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

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

    Xcode 9.4.1 Swift 4.1

    Note the use of localizedInfoDictionary to pick up the right language version of the bundle display name.

    var displayName: String?
    var version: String?
    var build: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Get display name, version and build
    
        if let displayName = Bundle.main.localizedInfoDictionary?["CFBundleDisplayName"] as? String {
            self.displayName = displayName
        }
        if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
            self.version = version
        }
        if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
            self.build = build
        }
    }
    

提交回复
热议问题