Grimless’s Game Design Blog
Grimless’s Game Design Blog
Custom Embedded Static Library for iPhone
Wednesday, October 7, 2009
Code reuse is a big deal in any application, but especially for iPhone apps since multiple versions and spin-offs are quite common. In order to leverage reuse as much as possible we will make a pre-compiled library for our iPhone apps. Now, the iPhone does not support dynamically linked libraries (also called dylibs or frameworks). iPhone does, however, support static libraries, meaning they are actually compiled into your application, so no runtime linking is done.
So find a class/bunch of classes/code you want to put into potentially many other apps. Go ahead: go find something. Right. Start up Xcode and select the “Library” project type under the iPhone section and then the “Cocoa Touch Static Library” as below:

Select “Choose...”, name the project whatever you like and set it’s location. Create your files and whatnot and make sure it builds. Next, click on “Targets” in the Groups & Files browser. This will bring up the tableview that will be filled with all of your files with some extra stuff off to the side. Change all the headers you want to expose to the user (probably all of them) to “public”. The user can then enter #import “YourHeader.h” to import that header file and subsequent class. If you want to keep something hidden, change it’s visibility to “private” or “project”.

That’s pretty much it in that project. Build it and make sure you know where your product is. If you don’t know where it is: open the “Products” group, right-click on your framework, and then select “Reveal in Finder”.
Next, open the project that will be using the library. Single-click on your blue project root up at the top of the Groups & Files list and type Command-Option-A (add to project). Navigate to your library project and add it’s blue .xcodeproj file. Make sure the “Copy files into project” checkbox is NOT checked and change the reference type to “Relative to Project” and then click the Add button.

Expand the library project reference and drag the library reference down to your Target and drop it in “Link Binary with Library”. Then right-click on your application Target and select Add > New Build Phase > New Copy Files Build Phase. Drag the library down to this new build phase and drop it.

Double click on your application Target and click on the plus button below the “Direct Dependancies” box. Select your library and click on Add Target.

While still in the inspection pane for your Target, click on the “Build” tab. In the search field, type in “Header Search Path”. This should return a few results. Make sure the “Always search user header search paths” is checked.

Double-click on the space next to “User Header Search Paths” and press the plus button to add a new path. Check the “Recursive” box and then put in the path to your library project. If you don’t know what the path is, right-click on the library project reference in the Groups & Files section and select “Get Info”. This will show a dialog with the path. Enter that path into the Build pane.
**IMPORTANT** You MUST escape spaces and non-standard characters! To do this, place a \ before them. Accepted characters that do not need to be escaped are letters, numbers, underscores, and hyphens.
Clear the search field and put in “Other Linker Flags”. Double-click on the result, click the plus button and enter “-ObjC” to make sure the linker knows to use Objective-C linking.
You may need to “Rebuild Codesense Index” if your completions are not showing up.
That’s pretty much it! Go into one of the files in the project and put in #import “YourHeader.h” to import your header and use your code. You can also build global settings that let you do some of these things a little less often, but I like it project relative. To see how to do the global stuff, check out the top-notch tutorial here: Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References. Good luck and enjoy!
Reusing code is always a good idea and building a static library makes it super easy to move from project to project.