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

前端 未结 30 859
暖寄归人
暖寄归人 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
        }
    }
    
    0 讨论(0)
  • 2020-12-22 15:41

    Having looked at the documentation, I believe that the following is cleaner:

    let version = 
    NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") 
    as? String
    

    Source: "Use of this method is preferred over other access methods because it returns the localized value of a key when one is available."

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

    I know this has already been answered but personally I think this is a little cleaner:

    Swift 3.0:

     if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
        self.labelVersion.text = version
    }
    

    Swift <2.3

    if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
        self.labelVersion.text = version
    }
    

    This way, the if let version takes care of the conditional processing (setting the label text in my case) and if infoDictionary or CFBundleShortVersionString are nil the optional unwrapping will cause the code to be skipped.

    0 讨论(0)
  • 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.

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

    Bundle+Extensions.swift

    import Foundation
    
    extension Bundle {
        var versionNumber: String? {
            return infoDictionary?["CFBundleShortVersionString"] as? String
        }
    
        var buildNumber: String? {
            return infoDictionary?["CFBundleVersion"] as? String
        }
    
        var bundleName: String? {
            return infoDictionary?["CFBundleName"] as? String
        }
    }
    

    Usage:

    someLabel.text = Bundle.main.versionNumber
    
    0 讨论(0)
  • 2020-12-22 15:45

    OP asked for both version number and build number. Unfortunately most of the answers don't provide both of those options. Additionally, others add unnecessary extension methods. Here's one that's pretty simple and solves OP's problem:

    // Example output: "1.0 (234)"
    private func versionAndBuildNumber() -> String {
      let versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
      let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
      if let versionNumber = versionNumber, let buildNumber = buildNumber {
        return "\(versionNumber) (\(buildNumber))"
      } else if let versionNumber = versionNumber {
        return versionNumber
      } else if let buildNumber = buildNumber {
        return buildNumber
      } else {
        return ""
      }
    }
    
    0 讨论(0)
提交回复
热议问题