1.28. Loading Data From Other Bundles
Problem
You have included a few images or other resources in a separate bundle inside your main bundle and you would like to access those resources at runtime.
Solution
Find the path to your bundle at runtime using the pathForResource:ofType: method of your main
bundle. Once you have the path to your bundle, simply access it using
the bundleWithPath: class
method of NSBundle.
Note
Before continuing with this recipe, please follow the instructions in Recipe 1.26 to create a bundle called Resources.bundle and place it inside your main bundle.
Discussion
If you have followed the instructions in Recipe 1.26, you now have a bundle called Resources.bundle inside this bundle you have a folder called Images. Let’s now put an image inside this folder. After I placed an image called AlanSugar.png into the bundle Figure 1-35 shows what the bundle contains.

Figure 1-35. Placing an image inside the bundle which we created before
Since the Resources.bundle
is added to our app’s main bundle, we will need to use the main bundle
in order to find the path to our Resources.bundle. Once that is done, we can
directly access the files (only AlanSugar.png right now) inside this
bundle. Since bundles other than the main bundle can have folders
embedded inside them, to access files inside folders of a bundle other
than the main bundle it is best to use the pathForResource:ofType:inDirectory: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access