Currently the only way to create re-usable code amongst different projects for the iPhone platform is to create static libraries. 3rd party frameworks (as in Mac OS X) are not supported in the iPhone SDK. Also setting up unit testing, which is a crucial part of building re-usable libraries of course, is not that trivial.
To help building static libraries for the iPhone, I decided to create a project template and file template. The project template includes 2 targets. One is the actual static library itself and the other is a unit test target. The file template simplifies creating unit test classes, by including the necessary header files and boiler plate code. I have included the Google Toolbox for Mac’s iPhone unit testing facilities and the OCMock framework in these templates.
The source code for the templates are checked into my github repository. You can take a look at the instructions to install and use them over there. Once you create your static library, including that in your application project is quite straightforward. Just follow the below steps to add it to your project file:
- Drag and drop the “your_static_library.xcodeproj” file to your application project’s “Groups & Files” sidebar. Make sure that “Copy Items” is unchecked and “Reference Type” is “Relative to Project” in the dialog that pops up when you drop the static library project.

- Link your static library to your application project. In this case, select the project you just added, and check the checkbox to the far right for the library target. Leave the unit test target as is.

- Add the static library headers to your application project. Double click on your application project to open “Project Info” dialog. Switch to the “Build” tab and first search for the “Other Linker Flags” setting and set it to -ObjC. Then search for the “Header Search Paths” setting and set it to the relative path to the header files in your static library.

- Add your static library as a dependency to your application. This ensures that the your static library project is also compiled when you compile your application. To do this, double click on your application target switch to the “General Tab” of the “Target Info” dialog. Add the static library target by clicking the + sign in the direct dependencies section.

Now you are ready share your library code with any number of applications you build. You can of course apply the same steps to other 3rd party libraries that you find on the web. Happy application building!
at 2:56 am