Static Libraries & Workspaces

November 29th, 2011 by Ricardo Silva Leave a reply »

They say that you should not duplicate your code, but when it comes to iOS development what I see is a lot of duplicated files from third party libraries… For example, JSON and ASIHTTPRequest. So we end up with loads of duplicated files or, if not duplicated, very similar, differing in version.

Static Libraries would help the programmer with this problem.

To create your static library project, you choose the Cocoa Touch Static Library template under Framework & Library. What you need to note about this project is:

  • Set the headers (you want to be public) to public. This can be set during the Copy Headers under Build Phases of your static library target.
  • Set the Installation Directory to $(BUILT_PRODUCTS_DIR).
  • Skip Install should be set to Yes.
  • Public Headers Folder Path should be set to $(TARGET_NAME).

This project can then be used by other projects. I recommend you to set up a workspace for this. You can either save a current project as a workspace or create a workspace and drag and drop your project (the .xcodeproj file) to it. Be careful when you drag the static library project not to drag it inside another project. It should be at the same level of the main project.

Then, follow these steps for your project:

  • Add your library to Link Binary With Libraries.
  • Set User Header Search Paths to $(BUILT_PRODUCTS_DIR) recursively.
  • You may need to add -ObjC to Other Linker Flags if you use categories. You may also need to add -all_load and -force_load in some situations.
Advertisement

Leave a Reply