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

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

    Swift 3:

    Version Number

    if let versionNumberString = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String { // do something }
    

    Build number

    if let buildNumberString = Bundle.main.infoDictionary?["CFBundleVersion"] as? String { // do something }
    
    0 讨论(0)
  • 2020-12-22 15:32
    extension UIApplication {
    
        static var appVersion: String {
            if let appVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") {
                return "\(appVersion)"
            } else {
                return ""
            }
        }
    
        static var build: String {
            if let buildVersion = NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) {
                return "\(buildVersion)"
            } else {
                return ""
            }
        }
    
        static var versionBuild: String {
            let version = UIApplication.appVersion
            let build = UIApplication.build
    
            var versionAndBuild = "v\(version)"
    
            if version != build {
                versionAndBuild = "v\(version)(\(build))"
            }
    
            return versionAndBuild
        }
    
    }
    

    Attention: You should use if let here in case that the app version or build is not set which will lead to crash if you try to use ! to unwrap.

    0 讨论(0)
  • 2020-12-22 15:32

    For Swift 5.0 :

    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
    
    0 讨论(0)
  • 2020-12-22 15:33

    I created an extension for UIApplication.

    extension UIApplication {
        static var appVersion: String {
            let versionNumber = Bundle.main.infoDictionary?[IdentifierConstants.InfoPlist.versionNumber] as? String
            let buildNumber = Bundle.main.infoDictionary?[IdentifierConstants.InfoPlist.buildNumber] as? String
            
            let formattedBuildNumber = buildNumber.map {
                return "(\($0))"
            }
    
            return [versionNumber,formattedBuildNumber].compactMap { $0 }.joined(separator: " ")
        }
    }
    
    enum Constants {
        enum InfoPlist {
            static let versionNumber = "CFBundleShortVersionString"
            static let buildNumber = "CFBundleVersion"
        }
    }
    
    0 讨论(0)
  • 2020-12-22 15:34

    My answer (as at Aug 2015), given Swift keeps evolving:

    let version = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
    
    0 讨论(0)
  • 2020-12-22 15:34

    For Swift 1.2 it's:

    let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
    let build = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
    
    0 讨论(0)
提交回复
热议问题