A bundle is a directory in the file system that groups executable code and related resources such as images and sounds together in one place. In iOS and OS X, applications, frameworks, plug-ins, and other types of software are bundles. A bundle is a directory with a standardized hierarchical structure that holds executable code and resources used by that code. Foundation and Core Foundation include facilities for locating and loading code and resources in bundles.

Bundles bring several advantages to users and developers. They make it easy to install or relocate an application or other piece of software by simply moving it from one location to another. Bundles are also an important factor in internationalization. You store localized resources in specially named subdirectories of a bundle; programmatic facilities look for localized resources in the location that corresponds with a user’s language preferences.

Most types of Xcode projects create a bundle for you when you build the executable. Therefore you rarely need to construct a bundle by hand. Even so, it is important to understand their structure and how to access the code and resources inside them.

Structure and Content of Bundles

A bundle can contain executable code, images, sounds, nib files, private frameworks and libraries, plug-ins, loadable bundles, or any other type of code or resource. It also contains a runtime-configuration file called the information property list (Info.plist). Each of these items has its proper place in the bundle structure. Resources such as images, sounds, and nib files are deposited in the Resources subdirectory. They can be either localized or nonlocalized. Localized files (including strings files, which are collections of localized strings) are put in subdirectories of Resources that have the extension of lproj and a name corresponding to a language and possibly a locale.


Accessing Bundle Resources

Each application has a main bundle, which is the bundle that contains the application code. When a user launches an application, it finds the code and resources in the main bundle that it immediately needs and loads them into memory. Thereafter, the application can dynamically (and lazily) load code and resources from the main bundle or subordinate bundles as required.

The NSBundle class and, for procedural code, the CFBundleRef opaque type of Core Foundation give your application the means to locate resources in a bundle. In Objective-C, you first must obtain an instance of NSBundle that corresponds to the physical bundle. To get an application’s main bundle, call the class method mainBundle. Other NSBundle methods return paths to bundle resources when given a filename, extension, and (optionally) a bundle subdirectory. After you have a path to a resource, you can load it into memory using the appropriate class.

Loadable Bundles

As with application bundles, loadable bundles package executable code and related resources, but you explicitly load these bundles at runtime. You can use loadable bundles to design applications that are highly modular, customizable, and extensible. Every loadable bundle has a principal class that is the entry point for the bundle; when you load the bundle, you must ask NSBundle for the principal class and use the returned Class object to create an instance of the class.

Prerequisite Articles

Related Articles

Definitive Discussion