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 CFBundle​Create... functions. CFBundle provides several functions for finding resources within a bundle. The CFBundle​Copy​Resource​URL(_:​_:​_:​_:​) function returns the location of a resource of the specified name and type, and in the specified subdirectory. Use CFBundle​Copy​Resource​URLFor​Localization(_:​_:​_:​_:​_:​) to restrict the search to a specific localization name. Use CFBundle​Copy​Resource​URLs​Of​Type(_:​_:​_:​) 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 CFBundle​Get​Identifier(_:​) function to get the identifier of a bundle, and the CFBundle​Get​Info​Dictionary(_:​) 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 CFBundle​Copy​Executable​URL(_:​) function returns the location of the application’s executable. The functions CFBundle​Copy​Resource​URL(_:​_:​_:​_:​), CFBundle​Copy​Shared​Frameworks​URL(_:​), CFBundle​Copy​Private​Frameworks​URL(_:​), CFBundle​Copy​Shared​Support​URL(_:​), and CFBundle​Copy​Built​In​Plug​Ins​URL(_:​) 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 CFBundle​Copy​Localized​String(_:​_:​_:​_:​) and CFBundle​Copy​Localizations​For​URL(_:​) functions return a localized string from a bundle’s strings file. The CFBundle​Copy​Localizations​For​Preferences(_:​_:​) 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), Bundle 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.

CFBundle​Get​Function​Pointer​For​Name(_:​_:​) 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 CFBundle​Load​Executable(_:​) and CFBundle​Unload​Executable(_:​)—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 CFBundle​Unload​Executable 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.

Symbols

Creating and Accessing Bundles

func CFBundle​Create​Bundles​From​Directory(CFAllocator!, CFURL!, CFString!)

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

func CFBundle​Get​All​Bundles()

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

func CFBundle​Get​Bundle​With​Identifier(CFString!)

Locate a bundle given its program-defined identifier.

func CFBundle​Get​Main​Bundle()

Returns an application’s main bundle.

Loading and Unloading a Bundle

func CFBundle​Is​Executable​Loaded(CFBundle!)

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

func CFBundle​Preflight​Executable(CFBundle!, Unsafe​Mutable​Pointer<Unmanaged<CFError>?>!)

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

func CFBundle​Load​Executable(CFBundle!)

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

func CFBundle​Load​Executable​And​Return​Error(CFBundle!, Unsafe​Mutable​Pointer<Unmanaged<CFError>?>!)

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

func CFBundle​Unload​Executable(CFBundle!)

Unloads the main executable for the specified bundle.

Finding Locations in a Bundle

func CFBundle​Copy​Auxiliary​Executable​URL(CFBundle!, CFString!)

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

func CFBundle​Copy​Built​In​Plug​Ins​URL(CFBundle!)

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

func CFBundle​Copy​Executable​URL(CFBundle!)

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

func CFBundle​Copy​Private​Frameworks​URL(CFBundle!)

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

func CFBundle​Copy​Resources​Directory​URL(CFBundle!)

Returns the location of a bundle’s Resources directory.

func CFBundle​Copy​Shared​Frameworks​URL(CFBundle!)

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

func CFBundle​Copy​Shared​Support​URL(CFBundle!)

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

func CFBundle​Copy​Support​Files​Directory​URL(CFBundle!)

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

Locating Bundle Resources

func CFBundle​Copy​Resource​URL(CFBundle!, CFString!, CFString!, CFString!)

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

func CFBundle​Copy​Resource​URLIn​Directory(CFURL!, CFString!, CFString!, CFString!)

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

func CFBundle​Copy​Resource​URLs​Of​Type(CFBundle!, CFString!, CFString!)

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

func CFBundle​Copy​Resource​URLs​Of​Type​In​Directory(CFURL!, CFString!, CFString!)

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.

func CFBundle​Copy​Resource​URLs​Of​Type​For​Localization(CFBundle!, CFString!, CFString!, CFString!)

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

func CFBundle​Open​Bundle​Resource​Map(CFBundle!)

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

Managing Localizations

func CFBundle​Copy​Bundle​Localizations(CFBundle!)

Returns an array containing a bundle’s localizations.

func CFBundle​Copy​Localized​String(CFBundle!, CFString!, CFString!, CFString!)

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

func CFBundle​Copy​Localizations​For​Preferences(CFArray!, CFArray!)

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.

func CFBundle​Copy​Localizations​For​URL(CFURL!)

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

func CFBundle​Copy​Preferred​Localizations​From​Array(CFArray!)

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

Managing Executable Code

func CFBundle​Get​Data​Pointer​For​Name(CFBundle!, CFString!)

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

func CFBundle​Get​Function​Pointer​For​Name(CFBundle!, CFString!)

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

func CFBundle​Get​Function​Pointers​For​Names(CFBundle!, CFArray!, Unsafe​Mutable​Pointer<Unsafe​Mutable​Raw​Pointer?>!)

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

func CFBundle​Get​Plug​In(CFBundle!)

Returns a bundle’s plug-in.

Getting Bundle Properties

func CFBundle​Copy​Bundle​URL(CFBundle!)

Returns the location of a bundle.

func CFBundle​Get​Development​Region(CFBundle!)

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

func CFBundle​Get​Identifier(CFBundle!)

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

func CFBundle​Get​Info​Dictionary(CFBundle!)

Returns a bundle’s information dictionary.

func CFBundle​Get​Local​Info​Dictionary(CFBundle!)

Returns a bundle’s localized information dictionary.

func CFBundle​Get​Value​For​Info​Dictionary​Key(CFBundle!, CFString!)

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

func CFBundle​Copy​Info​Dictionary​In​Directory(CFURL!)

Returns a bundle’s information dictionary.

func CFBundle​Copy​Info​Dictionary​For​URL(CFURL!)

Returns the information dictionary for a given URL location.

func CFBundle​Copy​Executable​Architectures(CFBundle!)

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

func CFBundle​Copy​Executable​Architectures​For​URL(CFURL!)

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

func CFBundle​Get​Version​Number(CFBundle!)

Returns a bundle’s version number.

Getting the CFBundle Type ID

func CFBundle​Get​Type​ID()

Returns the type identifier for the CFBundle opaque type.

Data Types

CFBundle​Ref​Num

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

CFBundle

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

Related Documentation