Search code examples
macosbundlecore-foundationresourcebundle

CFBundleGetVersionNumber returns 0?


An XCode 3.X application running on 10.6.8 has this entry in info.plist:

<key>CFBundleVersion</key>
<string>2035</string>

In awakeFromNib() a quick check is made:

CFBundleRef br = CFBundleGetMainBundle();
if (br)
{
  uint32_t v32 = CFBundleGetVersionNumber(br);
}

The reference is non-zero but the version number is always 0.

Get Info in Finder displays the expected number.

Any ideas why? Many thanks.


Solution

  • See the documentation for CFBundleGetVersionNumber:

    Return Value

    A Mac OS 'vers' resource style version number. If the bundle’s version number is a number, it is interpreted as the unsigned long integer format defined by the 'vers' resource on Mac OS 9. If it is a string, it is automatically converted to the numeric representation, where the major version number is restricted to 2 BCD digits (in other words, it must be in the range 0-99) and the minor and bug fix version numbers are each restricted to a single BCD digit (0-9). See Technical Note TN1132 for more details.

    Discussion

    This function is only supported for the Mac OS 'vers' resource style version numbers. 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.

    You're not using the old OS 9-style 'vers' version resources in a .rsrc file, you're using the modern method of specifying it as a string in an Info.plist file.

    You could use something like:

    NSInteger bundleVersion = CFStringGetIntValue(
           CFBundleGetValueForInfoDictionaryKey(br, kCFBundleVersionKey));