Arcanelab Blog
text

Creating and using a static library with a resource bundle in Xcode 4 The other day I…

Creating and using a static library with a resource bundle in Xcode 4

The other day I wanted to create a static library that uses its own resources for one of my iOS projects.

It took me quite a while to figure this stuff out, because the whole process is non-trivial, to say the least. I read Apple’s related documentations, blog posts and many other sources and aggregated all the info that is needed to properly achieve the goal above.

The sources I collected the useful tips from are:

The funny thing is that non of the above sources alone contain all the steps needed to successfully achieve our goal (although the first one got me pretty close!) The whole process to figure every single step was quite frustrating, but after many rigorous attempts I succeeded! Here’s a very condensed, step-by-step guide for your pleasure:

  1. Create a new workspace in Xcode
  2. Create a new project for the static library (New->Project->iOS->Framework & Library->Cocoa Touch Static Library)
  3. Create your classes, add your resources to the project
  4. When you know which header(s) you’d like to export with your static library, go to and select the library target, under Build Phases look for a section called “Copy Headers”. If it doesn’t exist, create it (“Add Build Phase” button on the bottom right), then select the “Project” part (under Public and Private) and add the chosen header(s) to the list.
  5. Make sure that the Skip Install build setting is set to Yes. (Should be already set to that.)
  6. Add a new target to the project (OSX->Framework & Library->Bundle)
  7. Select this new bundle target, go to Build Settings and change the Base SDK to “Latest iOS” and select “Sandard (armv7, armv7s)” as Architecture. We had to do this, because only OSX specific Bundle can be created from the “New Target” menu of Xcode.
  8. Having the bundle target still selected, go to Build Phases and under the “Copy Bundle Resources” part add all of your resource files to the list (usually nib files, images, etc).
  9. Now that we have set the static library and its resources up, we can go ahead and create a new project in the same workspace. In this project we’ll build the app that will utilize the static library.
  10. The Apple documentation says that the “User Header Search Paths” build setting should be set to the recursive absolute path of a directory under which the static library’s header files are stored.
  11. The Apple documentation mentions the “Always Search User Paths” build setting to be set to Yes, but for me everything works without this setting, too. I guess it won’t hurt setting it anyway.
  12. Make sure that the “Skip Install” build setting is set to No. (Should be No by default.)
  13. At this point you should have two projects in your workspace, both visible in the Project Navigator. Go to the Targets folder of the static library project, bring up the context menu for the bundle file, and open its location in Finder. Then drag the resources bundle into your app’s project.
  14. In the main menu of Xcode go to “Product->Scheme->Edit Scheme”, select Build, then with the little + icon add the resources bundle and the static library to the targets.
  15. When you’d like to include the public header(s) from your static library, use the following method:
    #import “LibraryName/HeaderName.h”
  16. Whenever you need to refer to a specific resource file, you must first create an NSBundle object to refer to the bundle you created above. You can do it as such:
    NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@“NameOfTheBundle” withExtension:@“bundle”]];
    Then you can pass this bundle object to initialization methods that might require it.
  17. That should be it! Good luck!

Any questions, opinions? Feel free to comment.

Update: I’ve found this page linked on Stack Overflow, it might be interesting: Using open-source static libraries in Xcode 4.