XCode templates for iPhone static libraries with unit testing

April 9th, 2009 / in IPhone / by keremk /

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.Add Project Dialog
  • 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.Link Library Selection
  • 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.Project Info Dialog
  • 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.Project Dependencies

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!

Tags: , , , , , ,

Reader Comments

  • [...] and file templates for static libraries: Well this last one is from me. There is a more detailed blog post on this [...]

  • [...] Xcode templates for iPhone Static Libraries with Unit Testing This could be a real aid in helping to automate the setup of your Xcode projects with regards to unit testing. [...]

  • XCode templates for iPhone static libraries with unit testing…

    You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

  • [...] XCode templates for iPhone static libraries with unit testing | Coding Ventures Excellent article on making it easier to set up static libraries for unit testing. [...]

  • [...] approach), it still runs UI-less, which most of the time is fine. Approaches such as using OCMock look great, but at the time I wanted to see if I could get unit testing running on the simulator with [...]

  • Do these work for the 3.0 SDK?

  • Nice! I had to add the files under /Library/Application Support/Apple/Developer Tools/File Templates to make them visible under XCode.

  • I had a problem trying to create the folders “Project Templates” and “User Templates”. Trying to add the files under /Library/Application Support/Apple/Developer Tools/File Templates didn’t work for me.

    Actually the “Project Templates” folder already existed. But I could only add “Smart Folders” under ../Shared/Xcode using Finder. I didn’t want to create a Smart Folder, so popped up a terminal to look and see what was up. Inside the /Library/Application Support/Developer/Shared/Xcode/Project Templates folder, there was a folder named MacFuse. I had installed MacFuse a while back. Well the MacFuse folder was symbolic link pointing to where the MacFuse templates resided. I put your project template in a separate folder on my machine and then created a symbolic link to it under the Project Templates folder. I then created a File Templates folder under the above mentioned Xcode folder. I put your file template in a separate folder on my machine and then created a symbolic link to it under the File Templates folder that I just created.

    When I brought up Xcode, the project template (in MyProjectTemplates) was now there under User Templates.

    Note:

    While I was in /Library/Application Support/Developer/Shared/Xcode/Project Templates

    I created the link with:

    ln -s ‘/My iPhone Stuff/My Project Templates’ ./MyProjectTemplates

    While I was in /Library/Application Support/Developer/Shared/Xcode/File Templates

    I created the link with:

    ln -s ‘/My iPhone Stuff/My File Templates’ ./MyFileTemplates

  • Your own templates should be stored in the “~/Library…” folder not the “/Library…” folder. Notice the trailing “Home” sign (”~”). Basically anything that is user specific would go into the “~” path. The systemwide “/Library..” path is managed by the XCode installation or things that you need to install systemwide.

  • I understand the “Home” sign(”~”) well enough. :) I did move the templates into ~/Library/…/Xcode to keep things in line with the other applications that I have and I reset the links. Thanks!

Add Your Comment