I've created a static library in Xcode, which I am able to successfully use in other projects. However, with resources like plists, I find I must include any plists referenced in my library in the main project where the project is used.
In my static library project, I have my plist included in the "Copy Bundle Resources" phase of the target. In my code, here is what I am doing:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"MyClassParams" ofType:@"plist"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
If I use mainBundle and the MyClassParams.plist is included in the main project, all is good. If MyClassParams.plist is included in the library project, it doesn't work.
On the assumption that [NSBundle mainBundle] was referencing the wrong static method to use, I replaced it with:
NSBundle *mainBundle = [NSBundle bundleForClass:[MyClass class]];
This did not work either.
So, is it possible to include a plist or any other resources with a static library -- or do I have to include whatever I need in the project where the lib is used?
Static Libraries are not in bundles, when they get linked into an application they are part of that applications bundle. On the iPhone, effectively all code you write will be in the mainBundle since you can't include embedded frameworks.
So yes, you need to copy over all the resources into the project you are linking the static framework into.
I know you can't include resource files into a static library (that's what frameworks do). I use another solution in my projects:
Inside the static library "YYY" project:
Inside the main project:
libYYY.a
YYY.bundle
to the copied resource filesThis way, resource files used in the static library are not managed in the main project. Say I have a
foo.png
picture in the static library, I useYou could then get your plist like this:
Below, two screenshots:
I followed everything @Jilouc suggested, however, I can get the bundle but failed to get files inside it. I tried both the two ways (
@"yyy.bundle/image"
orstaticlib pathforresource...
)Then I used the method below and it worked!