Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSBundle to load my resources

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

like image 450
Sandeep Avatar asked Dec 10 '12 21:12

Sandeep


3 Answers

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

Let me know how you get on.

like image 174
hd1 Avatar answered Oct 03 '22 07:10

hd1


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

like image 36
Ramy Al Zuhouri Avatar answered Oct 03 '22 06:10

Ramy Al Zuhouri


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.

like image 36
Anton Avatar answered Oct 03 '22 06:10

Anton