145

How can I programmatically get the value of the target version, like in the image below?

As seen in the Properties window of the target of my Xcode project. I want to display this in the splash screen of my app so I know which version the people are currently using?

Binarian
  • 12,296
  • 8
  • 53
  • 84
slim
  • 4,010
  • 9
  • 35
  • 42

6 Answers6

418

There are 2 Numbers!

The marketing release number is for the customers, called version number. It starts with 1.0 and goes up for major updates to 2.0, 3.0, for minor updates to 1.1, 1.2 and for bug fixes to 1.0.1, 1.0.2 . This number is oriented about releases and new features. It does not have to stop at 9, 1.11.23 is a reasonable version number.

The build number is mostly the internal number of builds that have been made until then. But some use other numbers like the branch number of the repository or its commit number. This number should be unique to distinguish the different builds, which only have minor incremental changes.


To get the version number:

Objective-C:

NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

Swift < 3.0:

let appVersionString: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String

Swift 3.0+ (tested with 5.0):

let appVersionString: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

To get the build number:

Objective-C:

NSString * appBuildString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Swift < 3.0:

let buildNumber: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

Swift 3.0+ (tested until 5.0):

let buildNumber: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String   

If you want both in one:

First use the above lines and then the following one.

Objective-C:

NSString * versionBuildString = [NSString stringWithFormat:@"Version: %@ (%@)", appVersionString, appBuildString];

Swift (tested until 5.0):

let versionAndBuildNumber: String = "\(appVersionString) (\(buildNumber))"

Notes:

The values in the main bundle are not always present, for example in a command line application there is no CFBundleShortVersionString or CFBundleVersion, so the methods will return nil and it will crash because in the code it makes a incorrect downcast. But in normal Cocoa iOS and Mac apps these values are defined and will not be deleted.

This is tested with Xcode Version 7.3 (7D175). The build number is often written in parenthesis / braces. The build number is in hexadecimal or decimal.

buildandversion


In Xcode you can auto-increment the build number as a decimal number by placing the following in the Run script build phase in the project settings

#!/bin/bash    
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For hexadecimal build number use this script

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber)) 
buildNumber=$(($buildNumber + 1)) 
buildNumber=$(printf "%X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For Xcode do the following:

Step 1

step1

Step 2

step2

Step 3

step3

Display Name
  • 4,502
  • 2
  • 47
  • 63
Binarian
  • 12,296
  • 8
  • 53
  • 84
  • 4
    Just to add for the above example, "CFBundleVersion" is the Build=300, "CFBundleShortVersionString" is the Version=0.41. Viktor's answer should be the correct answer. – justinkoh Mar 07 '13 at 06:38
  • 1
    I had problems with hex numbers once. Apple states to use integers: https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-102364 – hashier Oct 14 '13 at 04:08
  • Do you know if there are there any string constants for @"CFBundleShortVersionString" and @"CFBundleVersion" ? – igrek Oct 15 '15 at 07:36
  • Now that CI/CD systems are pretty common, you may want to point out the pitfalls of using build scripts to set version #'s with those. – Sam Ballantyne Mar 26 '16 at 22:33
  • 1
    Things have been renamed for Swift 3.0: it's `Bundle.main.object(forInfoDictionaryKey: "...")` now. – Raphael May 17 '17 at 14:22
  • Warning. The New Build System doesn't copy CFBundleVersion into the app's dictionary. FYI. Tested in Xcode 11.4. – Alex Zavatone Apr 11 '20 at 20:35
  • 1
    @AlexZavatone, are you still seeing the same problems in XCode 12 with CFBundleVersion not being copied into the dictionary? In XCode 12.4 it's working for me, and with my app project I'm able to retrieve CFBundleShortVersionString successfully. XCode12 is the first version I'm using, so perhaps this is related to the change from CFBundleVersion to CFBundleShortVersionString? – Rethunk Jan 28 '21 at 15:10
  • 1
    @Rethunk, I'll have to check it out. Thanks for the heads up. – Alex Zavatone Jan 28 '21 at 22:12
13

You don't need to change anything in your project or Xcode. Here's the Swift version for both separately:

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

It's included in this repo, check it out:

https://github.com/goktugyil/EZSwiftExtensions

shim
  • 9,289
  • 12
  • 69
  • 108
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
5

Here same code for Swift 3:

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
tanaschita
  • 221
  • 4
  • 11
  • `Bundle.main` had an empty `infoDictionary` for me; maybe because I'm doing it from within a framework, not an executable or app? `Bundle(for: MyClass.self)` contains the expected values. – Raphael May 17 '17 at 14:47
4

Programmatically display version and build number - Swift 4.0

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "1.0"

let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "1.0"            

let versionAndBuildNumber = "Ver #\(versionNumber) ( Build #\(buildNumber) )"
Sandip Patel - SM
  • 3,346
  • 29
  • 27
4

I made an extension for Bundle so it will be easy to use

extension Bundle {
    var releaseVersionNumber: String? {
        return infoDictionary?["CFBundleShortVersionString"] as? String
    }

    var buildVersionNumber: String? {
        return infoDictionary?["CFBundleVersion"] as? String
    }

    var applicationName: String {
        return infoDictionary?["CFBundleDisplayName"] as? String ?? "ADKATech"
    }

    var applicationReleaseDate: String {
        return infoDictionary?["ApplicationReleaseDate"] as? String ?? Date().description
    }

    var applicationReleaseNumber: Int {
        return infoDictionary?["ApplicationReleaseNumber"] as? Int ?? 0
    }

    var releaseVersionNumberPretty: String {
        return "\(releaseVersionNumber ?? "1.0.0")"
    }

    var buildVersionNumberPretty: String {
        return "\(buildVersionNumber ?? "1")"
    }
}
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
1

You can also use like

var appVersion: String {
  //versionNumber
  let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "1.0"

  //buildNumber
  let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "1.0"

  return "Version: #\(versionNumber) (#\(buildNumber))"
}

Then you can use anywhere when you declare Globally.

print(appVersion)

Output:

Version: #1.0 (#1.0.3)