Custom Xcode Templates for iPhone Development

Jonah Williams ·

Previously we discussed building an iPhone Unit Testing Toolkit. I wanted to reuse that work without having to repeat those configuration steps every time I started a new Xcode project. Xcode provides a number of templates or starting iPhone applications so I made my own versions which include all of the unit testing tools we rely on.

To reuse my templates checkout a copy from http://svn.carbonfive.com/public/jonah/Xcode-templates/Xcode/ to “/Library/Application Support/Developer/Shared/Xcode/” and restart Xcode.

Installing the template files:

I installed Apple’s developer tools using the default path so my copy of Apple’s Xcode templates is located in “/Developer/Library/Xcode/File Templates” and “/Developer/Library/Xcode/Project Templates”. Installing the iPhone SDK added additional templates in “/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/”. Those were useful examples to base my own templates on but I needed to save my new templates separate from Apple’s default configuration files.

The Xcode release notes state that custom support files including project and file templates belong under “/Library/Application Support/Developer/” or “~/Library/Application Support/Developer/”. I didn’t need to restrict the visibility of these templates to a specific version of Xcode and I wanted them to be available to any user account so I put them in “/Library/Application Support/Developer/Shared/Xcode/Project Templates” and “/Library/Application Support/Developer/Shared/Xcode/File Templates”.

Building a template project:

I started by adding a “test” build target to Apple’s default “View-Based Application” template. First I copied “/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/View-Based Application” to “/Library/Application Support/Developer/Shared/Xcode/Project Templates/Carbon Five/Testable View-Based Application”. Then I opened “Testable View-Based Application/___PROJECTNAME___.xcodeproj” in Xcode and changed the customized the project structure by adding a “Unit Tests” target, the hamcrest and OCMock frameworks, GTM classes, an example test class, and a couple of groups in the overview list.

Custom project settings

While setting up the project and testing its configuration Xcode created a bunch of files I did not want as part of my template so when I finished with the project I deleted both the build products directory and the user specific settings files inside the “___PROJECTNAME___.xcodeproj” package. I also editted the template description in “TemplateInfo.plist”.
I restarted Xcode and could start a new project using the template by selecting it from the User Templates section of the New Project dialog.

Creating a file template:

Creating a project from my template was handy but I wanted a faster way to build individual test classes as well so I wrote a file template.

First I copied “/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Classes/NSObject subclass.pbfiletemplate” to “/Library/Application Support/Developer/Shared/Xcode/File Templates/Carbon Five/GTM test template.pbfiletemplate”. Then in the class.m file I added hamcrest and OCMock imports and an example test method. This class did not require a separate header file so I deleted class.h.

In TemplateInfo.plist I updated the template’s description and since I removed the header file I removed the CounterpartTemplateFile line.

After restart Xcode again I could create test classes based on the template using the “New File…” menu.

A couple dozen unit tests later I’m happy not to be repeating even this minimal amount of configuration each time I start a new test.

If you’re developing in Xcode I’d like to hear what sort of project setup you use. Are there libraries or frameworks you always include? Class files you have, or wish you had, created templates for? Do you use Apple’s default resource groups or have you established your own conventions for organizing your projects?