For the More Curious: The Application Bundle

When you build an iOS application project in Xcode, you create an application bundle. The application bundle contains the application executable and any resources you have bundled with your application. Resources are things like XIB files, images, audio files – any files that will be used at runtime. When you add a resource file to a project, Xcode is smart enough to realize that it should be bundled with your application.

How can you tell which files are being bundled with your application? Select the Homepwner project from the project navigator. Check out the Build Phases pane in the Homepwner target. Everything under Copy Bundle Resources will be added to the application bundle when it is built.

Each item in the Homepwner target group is one of the phases that occurs when you build a project. The Copy Bundle Resources phase is where all of the resources in your project get copied into the application bundle.

You can check out what an application bundle looks like on the filesystem after you install an application on the simulator. Navigate to ~/Library/Application Support/iPhone Simulator/(version number)/Applications. The directories within this directory are the application sandboxes for applications installed on your computer’s iOS simulator. Opening one of these directories will show you what you expect in an application sandbox: an application bundle and the Documents, tmp, and Library directories. Right- or Command-click the application bundle and choose Show Package Contents from the contextual menu (Figure 18.11).

Figure 18.11  Viewing an application bundle

Viewing an application bundle

A Finder window will appear showing you the contents of the application bundle (Figure 18.12). When a user downloads your application from the App Store, these files are copied to their device.

Figure 18.12  The application bundle

The application bundle

You can load files from the application’s bundle at runtime. To get the full path for files in the application bundle, you need to get a pointer to the application bundle and then ask it for the path of a resource.

/​/​ ​G​e​t​ ​a​ ​p​o​i​n​t​e​r​ ​t​o​ ​t​h​e​ ​a​p​p​l​i​c​a​t​i​o​n​ ​b​u​n​d​l​e​
N​S​B​u​n​d​l​e​ ​*​a​p​p​l​i​c​a​t​i​o​n​B​u​n​d​l​e​ ​=​ ​[​N​S​B​u​n​d​l​e​ ​m​a​i​n​B​u​n​d​l​e​]​;​

/​/​ ​A​s​k​ ​f​o​r​ ​t​h​e​ ​p​a​t​h​ ​t​o​ ​a​ ​r​e​s​o​u​r​c​e​ ​n​a​m​e​d​ ​m​y​I​m​a​g​e​.​p​n​g​ ​i​n​ ​t​h​e​ ​b​u​n​d​l​e​
N​S​S​t​r​i​n​g​ ​*​p​a​t​h​ ​=​ ​[​a​p​p​l​i​c​a​t​i​o​n​B​u​n​d​l​e​ ​p​a​t​h​F​o​r​R​e​s​o​u​r​c​e​:​@​"​m​y​I​m​a​g​e​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​f​T​y​p​e​:​@​"​p​n​g​"​]​;​

If you ask for the path to a file that is not in the application’s bundle, this method will return nil. If the file does exist, then the full path is returned, and you can use this path to load the file with the appropriate class.

Also, files within the application bundle are read-only. You cannot modify them nor can you dynamically add files to the application bundle at runtime. Files in the application bundle are typically things like button images, interface sound effects, or the initial state of a database you ship with your application. You will use this method in later chapters to load these types of resources at runtime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset