3

I want to get the version number from info.plist using:

NSString *version = (NSString *)CFBundleGetVersionNumber(CFBundleGetMainBundle());

That doesn't give me the string value from Bundle Version that I expect.

This does give me the string I expect:

NSString *version = (NSString *)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);

I'd like to know how to get the first form to work since it looks like it's the preferred way to get the version from info.plist.

Here is how I'm using the result:

htmlAbout = [htmlAbout stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"[version]"] withString:version];

I'm working in Xcode 4.1

Thanks

3 Answers3

2
CFStringRef ver = CFBundleGetValueForInfoDictionaryKey(
                      CFBundleGetMainBundle(), 
                      kCFBundleVersionKey);
NSString *appVersion = (NSString *)ver;

If you are using ARC ,

CFStringRef ver = CFBundleGetValueForInfoDictionaryKey(
                      CFBundleGetMainBundle(), 
                      kCFBundleVersionKey);
NSString *appVersion = (__bridge NSString *)ver;
Carina
  • 2,260
  • 2
  • 20
  • 45
2

a serious error in conversion exists in your program: CFBundleGetVersionNumber returns UInt32, which is not convertible via typecast to NSString (or any objc type).

justin
  • 104,054
  • 14
  • 179
  • 226
0

See the Apple docs for CFBundleGetVersionNumber:

https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFBundleRef/Reference/reference.html

It says your second form is actually the preferred way for X.Y versions:

Where other version number styles—namely X, or X.Y, or X.Y.Z—are used, you can use CFBundleGetValueForInfoDictionaryKey with the key kCFBundleVersionKey to extract the version number as a string from the bundle’s information dictionary.

Alan
  • 3,715
  • 3
  • 39
  • 57