Locating Resources Inside a Bundle

Once you have found a bundle and obtained a CFBundle instance, it is likely that you'll want to load one or more of the bundle's resources. CFBundle doesn't take responsibility for actually loading resources other than executable code and localizable strings. For all other resource types, CFBundle Services provides two functions for finding and returning the URL of the resource. Once you have the URL for your resource, you can load it using the function appropriate to the type of resource.

The most commonly used of the resource finding functions is CFBundleCopyResourceURL . It allows you to find a single resource using a name, a type, or both. The function CFBundleCopyResourceURLsOfType gives you a way to find all resources of a certain type in one search. The current method of typing resources relies on file extensions. In order for CFBundle to search by type you must make sure to properly name your files.

CFBundle Services also includes functions for accessing the resources inside a bundle without actually creating a CFBundleRef . These functions might be slightly faster than creating a bundle and searching it, but only if you intend to search once. If you need to query for more than one resource, searching is always much faster if you create the bundle because it can then cache information about how to find resources.

Suppose you have placed an image called Seagull.jpg in your application's main bundle. Listing 8 shows you how to search for this image by name and type.

Listing 8 Locating resources inside a bundle by name and type
#define kCFBundleNoSubDir NULL CFURLRef seagullURL; // Look for a resource in the main bundle by name and type. seagullURL = CFBundleCopyResourceURL( mainBundle, CFSTR("Seagull"), CFSTR("jpg"), kCFBundleNoSubDir );

The first parameter is of type CFBundleRef and specifies the bundle to search. The second parameter is a CFStringRef containing the name of the resource you wish to find. The third parameter allows you to specify a type for the search. The fourth parameter provides a means to search a specific subdirectory of the bundle for the resource. In this case pass NULL which causes the search to begin at the top level of the bundle's Resources folder.

Perhaps your bundle contains a folder called BirdImages . You could load all of the JPEGs in the folder using the function CFBundleCopyResourceURLsOfType .

Listing 9 Locating multiple resources by type
CFArrayRef birdURLs; // Find all of the JPEG images in a given folder. birdURLs = CFBundleCopyResourceURLsOfType( mainBundle, CFSTR("jpg"), CFSTR("BirdImages") );

© 1999 Apple Computer, Inc. – (Last Updated 07 September 99)