Using NSBundle to load my resources

10,960

Solution 1

[NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];

Let me know how you get on.

Solution 2

Try something like this:

NSBundle* bundle=[NSBundle bundleWithIdentifier: @"bundle name"];

And make sure that you have selected these options when you have dragged the bundle to the project:

enter image description here

Solution 3

For projects with one bundle, I use:

// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];

What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the @"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.

Share:
10,960
Sandeep
Author by

Sandeep

Updated on June 14, 2022

Comments

  • Sandeep
    Sandeep about 2 years

    I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.

    I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you