5

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

Sandeep
  • 20,908
  • 7
  • 66
  • 106

3 Answers3

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

Let me know how you get on.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
hd1
  • 33,938
  • 5
  • 80
  • 91
  • 1
    It does not load the bundle. It seems like, the bundle itself is inside the main application bundle. So I had to get the path to the bundle using, [[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"] and then use the path to load the bundle using bundleWithPath: method – Sandeep Dec 10 '12 at 22:14
  • However, I added an image into the bundle and tried loading the image using pathForResource:ofType: method but the method is nil and then I browsed into the simulator application folder and saw that the image is not being copied. – Sandeep Dec 10 '12 at 22:21
  • 1
    You may need to add a "Copy Files" phase to your build. – hd1 Dec 10 '12 at 22:47
3

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

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
1

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.

Anton
  • 3,998
  • 25
  • 40