163

I would like to be able to get the current version of my iOS project/app as an NSString object without having to define a constant in a file somewhere. I don't want to change my version value in 2 places.

The value needs to be updated when I bump my version in the Project target summary.

8 Answers 8

404

You can get the version and build numbers as follows:

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String

or in Objective-C

NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];

I have the following methods in a category on UIApplication:

extension UIApplication {

    static var appVersion: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
    }

    static var appBuild: String {
        return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
    }

    static var versionBuild: String {
        let version = appVersion, build = appBuild            
        return version == build ? "v\(version)" : "v\(version)(\(build))"
    }
}

Gist: https://gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b


Here's the equivalent in Objective-C:

+ (NSString *) appVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];    
}

+ (NSString *) build
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
}

+ (NSString *) versionBuild
{
    NSString * version = [self appVersion];
    NSString * build = [self build];

    NSString * versionBuild = [NSString stringWithFormat: @"v%@", version];

    if (![version isEqualToString: build]) {
        versionBuild = [NSString stringWithFormat: @"%@(%@)", versionBuild, build];
    }

    return versionBuild;
}

Gist: https://gist.github.com/ashleymills/c37efb46c9dbef73d5dd

Sign up to request clarification or add additional context in comments.

6 Comments

You could use this [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleVersion"] instead of [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey].The Former seems to rhyme well with [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"]; so Now it has become easy to keep in memory
@Duraiamuthan I'd recommend using (NSString *)kCFBundleVersionKey rather than @"CFBundleVersion". Even though it's longer to type (auto complete will help here), as it's a constant it's checked at compile time.
Btw, it would be more accurate to make this extension on NSBundle instead of UIApplication this will also allow to replace static calls with the instance ones.
@Zapko - I respectfully disagree. Conceptually this fits with UIApplication, as the information returned is about the application. Also, this only applies to the main bundle - it would make no sense to make this an instance method on any bundle.
Did you mean build when you said appBundle (in the 2nd line of your code) ?
|
14

Here's what worked on Xcode 8, Swift 3:

let gAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let gAppBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"

print("Version: \(gAppVersion)")
print("Build: \(gAppBuild)")

Comments

3

In Objective C:

1)For getting App version you have to use a:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

2)For getting Build version you have to use a:

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 

Comments

2

[Swift version: 5.2]

All iOS apps must store an app version number in their Info.plist file, but there’s no build-in way to get that as a string you can use in your code.

I have created one small extension to UIApplication that reads the Info.plist file and returns a version number automatically.

Here’s the code:

extension UIApplication {
    static var appVersion: String? {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
    }
}

Inside Your Controller

@IBOutlet weak var tv_version: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
   
    //Display Version
    setUpCurrentVersion()
}


func setUpCurrentVersion(){
    tv_version.text = "v" + UIApplication.appVersion!
}

Comments

1

In Swift, you can get bundle version by using:

let info:NSDictionary = NSBundle.mainBundle().infoDictionary!

let version:String = info.objectForKey("CFBundleShortVersionString") as! String

versionLabel.text = "Version:" + version

Comments

0

An open source project of mine, App Update Tracker, offers this functionality (and more) in the form of class methods:

  • + (NSString *)getShortVersionString
  • + (NSString *)getLongVersionString

You would use it like so:

#import "AppUpdateTracker.h"

NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]);
NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]);

Comments

-1

So heres a Swift version for both of these separately:

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

Its included in this repo, check it out:

https://github.com/goktugyil/EZSwiftExtensions

2 Comments

CFBundleVersionKey no longer works on Xcode 8 and Swift 3. Do you know the new key?
-1

Just for note

To obtain localized value of any key you should use CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), "CFBundleShortVersionString" as CFString)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.