Once you have acquired a bundle reference, you can retrieve information about the bundle's location on disk as well as gain access to property values in the bundle's info plist. CFBundle's API includes several functions that allow you to retrieve standard bundle info.
The CFBundle Services functions
CFBundleCopyBundleURL
,
CFBundleCopySupportFilesDirectoryURL
, and
CFBundleCopyResourcesDirectoryURL
allow you to retrieve the exact location of the bundle and its internal directories. Usually, you should not care about any of these locations. URLs to specific resources within the bundle should be constructed with the resource-finding API described in Locating Resources Inside a Bundle. This API exists to insulate you from the exact structure of the bundle. If you find yourself wanting to call these functions for anything other than help in debugging, think twice.
The functions
CFBundleGetVersion
,
CFBundleGetIdentifier
, and
CFBundleGetDevelopmentRegion
give you direct access to the standard bundle properties stored in the
Info.plist
file. See the section More About the Info.plist File for a listing of all standard keys defined by CFBundle.
The following example demonstrates how you would retrieve the bundle's version number from the info plist. Though the value in the info plist may be written as a string, for example "2.1.0b7", the value is returned in unsigned long integer format as defined by the
'vers'
resource on Mac OS 8. See the "Core Foundation Property List Services" chapter for details on how to create entries in your
CustomInfo.plist
file.
Listing 1-7 Obtaining the bundle's version
// This is the `vers' resource style value for 1.0.0 #define kMyBundleVersion1 0x01008000 UInt32 bundleVersion; // Look for the bundle's version number. bundleVersion = CFBundleGetVersionNumber( mainBundle ); // Check the bundle version for compatibility with our app. if (bundleVersion < kMyBundleVersion1) return (kErrorFatalBundleTooOld);
If you want to retrieve custom properties from the Info plist, you can access them using the function
CFBundleGetInfoDictionary
. Because the resulting info plist is an instance of the standard Core Foundation type CFDictionaryRef, you can use the dictionary lookup routines from
CFDictionary.h
to find and retrieve your properties.
Listing 1-8 Retrieving information from a bundle's info plist
CFDictionaryRef bundleInfoDict; CFString myPropertyString; // Get an instance of the info plist. bundleInfoDict = CFBundleGetInfoDictionary( myBundle ); // If we succeeded, look for our property. if ( bundleInfoDict != NULL ) { myPropertyString = CFDictionaryGetValue( bundleInfoDict, CFSTR("MyPropertyKey"); }
It is also possible to obtain an instance of a bundle's info dictionary without first opening the bundle using the function CFBundleCopyInfoDictionaryInDirectory. This might be useful if you want to simply search the info plists for a set of bundles without having to create an instance for each bundle.