1.26. Creating Bundles

Problem

You want to group your resources into hierarchical structures and be able to access those resources at runtime with ease.

Solution

Follow these steps to successfully create a bundle:

  1. Create a root folder on your disk that will later become your bundle. For instance, let’s give this folder the name Resources.

  2. Under the Resources folder, create three more folders named Images, Videos, and Sounds.

  3. Under the three aforementioned folders, place related resources. For instance, place one or more images in the Images folder and one or more video files under the Videos folder and so on.

  4. Once you are done, rename your Resources folder to Resources.bundle. Once you add this extension to your folder name, OS X will ask for your confirmation and a dialog similar to that shown in Figure 1-33 will appear on the screen. Press Add on the dialog to add the .bundle extension to the Resources folder.

Adding a .bundle extension to a folder name in order to turn it into a bundle

Figure 1-33. Adding a .bundle extension to a folder name in order to turn it into a bundle

Discussion

Bundles are simple folders with a .bundle extension. They have two main distinctions from regular folders:

  1. Cocoa Touch provides an interface through which you can access bundles and their resources really easily.

  2. If a bundle is added to the Navigator on the lefthand side of Xcode, any files added to or removed from the bundle outside Xcode will, respectively, appear in or disappear immediately from Xcode’s navigator. In contrast, if you had added a normal folder to Xcode’s navigator and then went and deleted a file from that folder on disk, without using Xcode’s help, you would see that file marked with red color in Xcode rather than getting deleted immediately. Bundles can be very useful, especially if you want to add files to your folders manually using Finder, instead of using Xcode.

Main bundles are flat bundles, in that all files inside the main bundle will be stored in one directory (its root directory). Bundles created by programmers can have subdirectories. Any bundle, including the main bundle, can contain other bundles.

Every iOS application comes with at least one bundle, called the main bundle. The main bundle contains your app’s binary code and any other resource you are using inside your application, such as retina images, sounds, HTML files, and whatnot. The main bundle, in other words, contains the resources that get compiled into your final binary that you will submit to the App Store or distribute in your organization. These resources can then be dynamically loaded using the NSBundle class’s mainBundle class method.

Note

Although you can add two or more bundles with the same name to one iOS project, it is best not to complicate things like that. The reason this situation could get complicated is that when we start loading resources from our bundles, we will first need to find our bundles by their path. If you have two bundles with the same name, it will become quite difficult to detect which is which. So as a good practice, make sure that your bundles have different names when you add them to your Xcode projects.

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.