6

I have an Xcode workspace with a couple of projects, say Foo and Baz, that both depend on some common code, which happens to be the RCSwitch switch button code (can be found here if anyone is interested). RCSwitch comes with a few image files that need to be loaded when Foo or Baz are running. They get loaded using the UIImage imageNamed: message, like so

UIImage *knobTmpImage = [[[UIImage imageNamed:@"btn_slider_thumb.png"] retain] autorelease];

My understanding is that for that to work the images need to be part of the application bundle. My question is how to get them there from within my library? I have tried adding the "Copy Bundle Resources" build phase to my library project and then adding all the images to that without any effect. The images do not get found at runtime.

Then I tried adding the images to the "Copy Bundle Resources" build phase of Foo and Baz themselves; one has to select "Add Others" as the upcoming dialog does not list any resources outside the current project. That actually works, but it does seem like a weird way of doing it. Neither Foo nor Baz ought to have knowledge of the internals of the library. What if I wanted to ship the library to external customers?

Is there another way of doing this? Is it even possible to have images or other resources in a libXXX.a file?

McKrassy
  • 961
  • 2
  • 10
  • 19
  • You can't include resource files with a static library. Your problem seems like the following issue: http://stackoverflow.com/questions/707429/can-you-reference-xib-files-from-static-libraries-on-the-iphone
    You can apply workaround to solve what you are after, but I would suggest you to create a framework rather a static library. Following articles might help you.
    http://simplyitinc.blogspot.com/2011/04/creating-static-framework-in-xcode-4.html
    http://www.cocoanetics.com/2010/05/making-your-own-iphone-frameworks-in-xcode/
    – Learner Sep 02 '11 at 20:21

1 Answers1

3

What I do for this is have my static library create a bundle which I include in the main app project. If using a sub project / sibling project in a workspace then this bundle can be added in such a way that if you edit the library project to add another image then it gets rebuilt meaning the main app project picks up the change.

I wrote a tutorial about it here - http://www.galloway.me.uk/tutorials/ios-library-with-resources/

The bit you'll be interested in is the category on UIImage which I use to pick up the images in the library bundle. So from the library's code I use the method added in the category to grab the image from the right bundle.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • 1
    Great writeup (thanks, fixed my problems on a hairy project!), but it would be MUCH better if you'd copy/paste the key points here so the info is all on SO.com. Also ... it's a pity you have no comment box on your blog, because I wanted to share a 2-line addendum which lets you write code INSIDE the library that can be run both inside and outside (try the mainbundle; if the path returned is nil, try the hardcoded expoerted bundle -- this way, you can even "override" the resources by including a custom version in the main app) – Adam Apr 25 '13 at 15:39
  • @Adam - Yeh I probably should C&P the main points here - I'll try to get round to doing that. As for comments - there should be Facebook comment plugin at the bottom - you not seeing that? – mattjgalloway Apr 25 '13 at 19:24
  • I see a like button but no comment form. If it needs Facebook to comment, that might be the problem - I'm not going to register a facebook account to post on a blog, sorry :) – Adam Apr 26 '13 at 09:06
  • @Adam - Hehe OK fair enough :-). I use the Facebook comments because it's just easier. Could use Disqus but I just thought I'd use Facebook. Thanks for feedback though! – mattjgalloway Apr 26 '13 at 13:06