CFBundle

Overview

CFBundle allows you to use a folder hierarchy called a bundle to organize and locate many types of application resources including images, sounds, localized strings, and executable code. In macOS, bundles can also be used by CFM applications to load and execute functions from Mach-O frameworks. You can use bundles to support multiple languages or execute your application on multiple operating environments.

You create a bundle object using one of the CFBundleCreate... functions. CFBundle provides several functions for finding resources within a bundle. The CFBundleCopyResourceURL function returns the location of a resource of the specified name and type, and in the specified subdirectory. Use CFBundleCopyResourceURLForLocalization to restrict the search to a specific localization name. Use CFBundleCopyResourceURLsOfType to get the locations of all resources of a specified type.

CFBundle provides functions for getting bundle information, such as its identifier and information dictionary. Use the CFBundleGetIdentifier function to get the identifier of a bundle, and the CFBundleGetInfoDictionary function to get its information dictionary. The principal intended purpose for locating bundles by identifier is so that code (in frameworks, plugins, etc.) can find its own bundle.

You can also obtain locations of subdirectories in a bundle represented as CFURL objects. The CFBundleCopyExecutableURL function returns the location of the application’s executable. The functions CFBundleCopyResourceURL, CFBundleCopySharedFrameworksURL, CFBundleCopyPrivateFrameworksURL, CFBundleCopySharedSupportURL, and CFBundleCopyBuiltInPlugInsURL return the location of a bundle’s subdirectory containing resources, shared frameworks, private frameworks, shared support files, and plug-ins respectively.

Other functions are used to manage localizations. The CFBundleCopyLocalizedString and CFBundleCopyLocalizationsForURL functions return a localized string from a bundle’s strings file. The CFBundleCopyLocalizationsForPreferences function returns the localizations that CFBundle would prefer, given the specified bundle and user preference localizations.

Unlike some other Core Foundation opaque types with similar Cocoa Foundation names (such as CFString and NSString), NSBundle objects cannot be cast (“toll-free bridged”) to CFBundle objects.

Unlike NSBundle, which does not support unloading (because the Objective C runtime does not support the unloading of Objective C code), you can unload CFBundle objects.

CFBundleGetFunctionPointerForName and related calls automatically load a bundle if it is not already loaded. When the last reference to the CFBundle object is released and it is finally deallocated, then the code will be unloaded if it is still loaded and if the executable is of a type that supports unloading. If you keep this in mind, and if you make sure that everything that uses the bundle keeps a retain on the CFBundle object, then you can just use the bundle naturally and never have to worry about when it is loaded and unloaded.

On the other hand, if you want to manually manage when the bundle is loaded and unloaded, then you can use CFBundleLoadExecutable and CFBundleUnloadExecutable—although this technique is not recommended. These functions force immediate loading and unloading of the executable (if it has not already been loaded/unloaded, and in the case of unloading if the executable is of a type that supports unloading). If you do this, then the code calling CFBundleUnloadExecutable is responsible for making sure that there are no remaining references to anything in the bundle's code before it is unloaded. In the previous approach, by contrast, this responsibility can be distributed to the individual code sections that use the bundle, by making sure that each one keeps its own retain on the CFBundle object.

One further point about CFBundle reference counting: if you are taking the first approach, but do not actually wish the bundle’s code to be unloaded (as is often the case), or if you are taking the second approach of manually managing the unloading yourself, then in many cases you do not actually have to worry about releasing a CFBundle object. CFBundle instances are uniqued, so there is only one CFBundle object for a given bundle, and rarely are there so many bundles being considered at once that the memory usage for CFBundle objects would be significant. There are cases in which a process could create CFBundle objects for potentially an unlimited number of bundles, and such processes would wish to balance retains and releases carefully, but such cases are likely to be rare.

Note that it is best to compile any unloadable bundles with the flag -fno-constant-cfstrings—see Bundle Programming Guide for more details.

Topics

Creating and Accessing Bundles

CFBundleCreate

Creates a CFBundle object.

CFBundleCreateBundlesFromDirectory

Searches a directory and constructs an array of CFBundle objects from all valid bundles in the specified directory.

CFBundleGetAllBundles

Returns an array containing all of the bundles currently open in the application.

CFBundleGetBundleWithIdentifier

Locate a bundle given its program-defined identifier.

CFBundleGetMainBundle

Returns an application’s main bundle.

Loading and Unloading a Bundle

CFBundleIsExecutableLoaded

Obtains information about the load status for a bundle’s main executable.

CFBundlePreflightExecutable

Returns a Boolean value that indicates whether a given bundle is loaded or appears to be loadable.

CFBundleLoadExecutable

Loads a bundle’s main executable code into memory and dynamically links it into the running application.

CFBundleLoadExecutableAndReturnError

Returns a Boolean value that indicates whether a given bundle is loaded, attempting to load it if necessary.

CFBundleUnloadExecutable

Unloads the main executable for the specified bundle.

Finding Locations in a Bundle

CFBundleCopyAuxiliaryExecutableURL

Returns the location of a bundle’s auxiliary executable code.

CFBundleCopyBuiltInPlugInsURL

Returns the location of a bundle’s built in plug-in.

CFBundleCopyExecutableURL

Returns the location of a bundle’s main executable code.

CFBundleCopyPrivateFrameworksURL

Returns the location of a bundle’s private Frameworks directory.

CFBundleCopyResourcesDirectoryURL

Returns the location of a bundle’s Resources directory.

CFBundleCopySharedFrameworksURL

Returns the location of a bundle’s shared frameworks directory.

CFBundleCopySharedSupportURL

Returns the location of a bundle’s shared support files directory.

CFBundleCopySupportFilesDirectoryURL

Returns the location of the bundle’s support files directory.

Locating Bundle Resources

CFBundleCloseBundleResourceMap

Closes an open resource map for a bundle.

CFBundleCopyResourceURL

Returns the location of a resource contained in the specified bundle.

CFBundleCopyResourceURLInDirectory

Returns the location of a resource contained in the specified bundle directory without requiring the creation of a CFBundle object.

CFBundleCopyResourceURLsOfType

Assembles an array of URLs specifying all of the resources of the specified type found in a bundle.

CFBundleCopyResourceURLsOfTypeInDirectory

Returns an array of CFURL objects describing the locations of all resources in a bundle of the specified type without needing to create a CFBundle object.

CFBundleCopyResourceURLForLocalization

Returns the location of a localized resource in a bundle.

CFBundleCopyResourceURLsOfTypeForLocalization

Returns an array containing copies of the URL locations for a specified bundle, resource, and localization name.

CFBundleOpenBundleResourceFiles

Opens the non-localized and localized resource files (if any) for a bundle in separate resource maps.

CFBundleOpenBundleResourceMap

Opens the non-localized and localized resource files (if any) for a bundle in a single resource map.

Managing Localizations

CFBundleCopyBundleLocalizations

Returns an array containing a bundle’s localizations.

CFBundleCopyLocalizedString

Returns a localized string from a bundle’s strings file.

CFBundleCopyLocalizationsForPreferences

Given an array of possible localizations and preferred locations, returns the one or more of them that CFBundle would use, without reference to the current application context.

CFBundleCopyLocalizationsForURL

Returns an array containing the localizations for a bundle or executable at a particular location.

CFBundleCopyPreferredLocalizationsFromArray

Given an array of possible localizations, returns the one or more of them that CFBundle would use in the current application context.

CFCopyLocalizedString

Searches the default strings file Localizable.strings for the string associated with the specified key.

CFCopyLocalizedStringFromTable

Searches the specified strings file for the string associated with the specified key.

CFCopyLocalizedStringFromTableInBundle

Returns a localized version of the specified string.

CFCopyLocalizedStringWithDefaultValue

Returns a localized version of a localization string.

Managing Executable Code

CFBundleGetDataPointerForName

Returns a data pointer to a symbol of the given name.

CFBundleGetDataPointersForNames

Returns a C array of data pointer to symbols of the given names.

CFBundleGetFunctionPointerForName

Returns a pointer to a function in a bundle’s executable code using the function name as the search key.

CFBundleGetFunctionPointersForNames

Constructs a function table containing pointers to all of the functions found in a bundle’s main executable code.

CFBundleGetPlugIn

Returns a bundle’s plug-in.

Getting Bundle Properties

CFBundleCopyBundleURL

Returns the location of a bundle.

CFBundleGetDevelopmentRegion

Returns the bundle’s development region from the bundle’s information property list.

CFBundleGetIdentifier

Returns the bundle identifier from a bundle’s information property list.

CFBundleGetInfoDictionary

Returns a bundle’s information dictionary.

CFBundleGetLocalInfoDictionary

Returns a bundle’s localized information dictionary.

CFBundleGetValueForInfoDictionaryKey

Returns a value (localized if possible) from a bundle’s information dictionary.

CFBundleCopyInfoDictionaryInDirectory

Returns a bundle’s information dictionary.

CFBundleCopyInfoDictionaryForURL

Returns the information dictionary for a given URL location.

CFBundleGetPackageInfo

Returns a bundle’s package type and creator.

CFBundleGetPackageInfoInDirectory

Returns a bundle’s package type and creator without having to create a CFBundle object.

CFBundleCopyExecutableArchitectures

Returns an array of CFNumbers representing the architectures a given bundle provides.

CFBundleCopyExecutableArchitecturesForURL

Returns an array of CFNumbers representing the architectures a given URL provides.

CFBundleGetVersionNumber

Returns a bundle’s version number.

Getting the CFBundle Type ID

CFBundleGetTypeID

Returns the type identifier for the CFBundle opaque type.

Data Types

CFBundleRefNum

Type that identifies a distinct reference number for a resource map.

CFBundleRef

A reference to a CFBundle object.

Constants

Information Property List Keys

Standard keys found in a bundle’s information property list file.

Architecture Types

Constants that identify executable architecture types.

See Also

Opaque Types

CFFileDescriptor

The CFFileDescriptor provides an opaque type to monitor file descriptors for read and write activity via CFRunLoop.

CFStringTokenizer

CFStringTokenizer allows you to tokenize strings into words, sentences or paragraphs in a language-neutral way. It supports languages such as Japanese and Chinese that do not delimit words by spaces, as well as de-compounding German compounds. You can obtain Latin transcription for tokens. It also provides language identification API.