18

I would like to add the current version into the "about" section of my app. As seen in this attached screenshot Apple offers versioning.

How do you display these settings in your app?

Screenshot of Target 'myApp' Info

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jantimon
  • 36,840
  • 23
  • 122
  • 185

6 Answers6

25

After further searching and testing, I found the solution myself.

NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSLog(@"%i Keys:  %@", [infoDictionary count],
             [[infoDictionary allKeys] componentsJoinedByString: @" ,"]);

This snipplet gave me the following output:

20 Keys : NSBundleResolvedPath ,CFBundleVersion ,NSBundleInitialPath ,CFBundleIdentifier ,NSMainNibFile ,CFBundleIconFile ,CFBundleInfoPlistURL ,CFBundleExecutable ,DTSDKName ,UIStatusBarStyle ,CFBundleDevelopmentRegion ,DTPlatformName ,CFBundleInfoDictionaryVersion ,CFBundleSupportedPlatforms ,CFBundleExecutablePath ,CFBundleDisplayName ,LSRequiresIPhoneOS ,CFBundlePackageType ,CFBundleSignature ,CFBundleName

So the solution is as simple as:

NSString *version =[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];

However, this is not the Current Project Version as seen in the screenshot but the Bundle Version of the plist file.

Bryan P
  • 4,142
  • 5
  • 41
  • 60
jantimon
  • 36,840
  • 23
  • 122
  • 185
  • 1
    I think then versioning includes adding repositories and setting up SCM for your project in XCode and that's probably not the version you want (although i may be wrong). – Eimantas Sep 29 '09 at 13:53
  • If you set the CFBundleVersion value in your plist file to ${CURRENT_PROJECT_VERSION} it will be replaced with what is in the Current Project Version from build settings. You can then use agvtool bump to increment the build number. – mjk Dec 20 '13 at 02:55
  • 2
    Also, the `CFString` `kCFBundleVersionKey` could be used instead of the hardcoded string `@"CFBundleVersion"`, casted to `NSString` if necessary as would be required in the code above. – trss Feb 11 '14 at 17:19
  • 3
    The key CFBundleVersion is showing the BUILD number, not the VERSION. For the version, use key what Eimantas says : CFBundleShortVersionString. – Jose Manuel Abarca Rodríguez Mar 06 '15 at 22:13
12

Look into your Info.plist file which should have keys like CFBundleVersion and CFBundleShortVersionString

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • 6
    More info: http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/PListKeys.html#//apple_ref/doc/uid/20001431-102364 http://www.dribin.org/dave/blog/archives/2006/08/02/versioning_os_x_apps/ –  Sep 29 '09 at 13:00
  • ghommey - there's a guide for list programming on developer.apple.com. I'm sure you can find that out by yourself now ,) – Eimantas Sep 29 '09 at 13:13
7

Those items in the Build Info are not available to your built app. They are placeholders that you could possibly pull into your app. What is in your app is anything that you place in, say, the Resources folder of your app, like any text files, or plists, or a nice picture of your versioning engineer.

Now, you could pull some of the items in the Build Info window into a info.plist, using special identifiers, such as ${VERSION_INFO_PREFIX} or other token. The tokens are available if you click on any of the items on the left hand side in the window you have included above. For example, click on the word "Current Project Version" and copy the token that you see at the bottom, "CURRENT_PROJECT_VERSION". Then go to your plist file, and add an entry. Give it any name you want or "Current Project Version". Paste in ${CURRENT_PROJECT_VERSION} on the right hand side. Now that value is available to you from your app, programmatically. Of course, someone now has to enter that value into the appropriate place either in the Build Info window or elsewhere. It might just be easier just to manage this and fields like this in the info.plist file. It's up to you how you'd like to handle these things.

Here is how I get version info out of my info.plist:

+ (NSString *) getAppVersionNumber;
{
    NSString    *myVersion,
                *buildNum,
                *versText;

    myVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    buildNum = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
    if (myVersion) {
        if (buildNum)
            versText = [NSString stringWithFormat:@"Version: %@ (%@)", myVersion, buildNum];
        else
            versText = [NSString stringWithFormat:@"Version: %@", myVersion];
    }
    else if (buildNum)
        versText = [NSString stringWithFormat:@"Version: %@", buildNum];
    NSLog(versText);
    return versText;
}
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
mahboudz
  • 39,196
  • 16
  • 97
  • 124
  • "Undefined or garbage value returned to caller"! Delete versText and put a return statement on it's position. At the end return nil. – testing Sep 28 '10 at 12:35
  • 1
    Don't `NSLog` like this. It should be `NSLog(@"%@",versText);`. If `versText` remains undefined, which is possible from the code, this will result in a crash. – Mundi Jan 01 '12 at 14:33
4
  NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];

returns me always the same value(initial version) even if i change the version from the project settings but.

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

does the trick.

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
1

For the lazy here is the swift version :

NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")!
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
1

NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];

Gabriel
  • 964
  • 9
  • 8