A minimal project template for Xcode 4

by borealkiss

Introduction

Many people have their own project- and/or file-templates for practical or aesthetic reasons. For example I don’t like the naming convention provided by Apple such as XXXX-Info.plist, XXXX-Prefix.pch, and XXXXAppDelegate for the project XXXX. On Xcode 3.x. it was relatively easy to modify them. Since Xcode 4, however, Apple introduced a new template format that is no longer compatible with those of Xcode. 3.x. This has often become topics among Apple’s developer forums, since there is no official information provided. You can see how it changes from the Xcode 4 built-in templates. They are under

/Developer/Library/Xcode/Templates/

for Mac OS X stuffs and

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/

for iOS. If you add your own templates, the place you should use is under the following path:

~/Library/Developer/Xcode/Templates/

The problem is that it is quite hard to create a custom template. For example, to copy and paste the built-in template to your place will not show up the template on Xcode 4 until you manually change its identifier in TemplateInfo.plist.

TemplateInfo.plist

TemplateInfo.plist is basically a setup file for a Xcode 4 project. When creating a project, Xcode 4 first reads this file to prepare all environment for you to get started. Each TemplateInfo.plist has own identifier for Xcode 4 to distinguish from others (and that’s why copy-and-pasting built-in template described above does not work as a user template). If you need your own templates, you should give them your own identifiers. For example, the Xcode 4 built-in template for window based iPhone application has the following identifier:

com.apple.dt.unit.windowBasedApplication

TemplateInfo.plist has something inheritance nature like classes of programming languages (and the identifier corresponds to the name of class). Xcode 4 reads a given TemplateInfo.plist and aggregates data from its ancestral files. For example, the following figure shows pedigree charts for the built-in window based iPhone application template.

com.apple.Templates

You can see that the TemplateInfo.plist for the window based iPhone application template is the sixth generation from the top-most base file (com.apple.dt.unit.base). This demonstrates that when you want to customize a given TemplateInfo.plist and the place you want to change within comes from the ancestral files, you have to manipulate multiple files, i.e., it could be error prone. Since TemplateInfo.plist is just an XML file, there is no compile errors. Moreover the inheritance nature of XML files has no readability at all. In fact it seems Apple creates them not by coding but by automatic generation tools.

A minimal project template

This was originally just for me but since it founds to be useful I will share this with someone who wants to have own templates on Xcode 4. This contains several templates within but only shows up one window based application template on Xcode 4 (the identifier is com.borealkiss.windowBasedApplication), because the others are not a concrete template (there are abstract templates not to be actually implemented). The following figure shows the contents (gray and orange ones are included):

om.borealkiss.Templates

This template enables you to manipulate in the old way as of Xcode 3.x, i.e., editing actual files and adding onto templates. Basically you only have to manipulate the TemplateInfo.plist of com.borealkiss.windowBasedApplication. You can add whatever files you want as long as you do the following edits. For example, if you want add a given file called SomeClass.m, you add the following two parts into the TemplateInfo.plist of com.borealkiss.windowBasedApplication:

These are the definition part of the file:

<key>Definitions</key>
<dict>
	<key>SomeClass.m</key>
	<dict>
		<key>Path</key>
		<string>SomeClass.m</string>
	</dict>
</dict>

and its actual implementation:

<key>Nodes</key>
<array>
	<string>SomeClass.m</string>
</array>

If you want to add the file but not for the target of Copy Bundle Resources (like Info.plist, Prefix.pch, and header files), change the definition as follows:

<key>Definitions</key>
<dict>
	<key>SomeClass.m</key>
	<dict>
		<key>Path</key>
		<string>SomeClass.m</string>
		<key>TargetIndices</key>
		<array/>
	</dict>
</dict>

You can add it inside a given folder. For example if you want to add the file inside a directory called MyClasses, change the definition as follows (note that the key name should be consistent with the actual file path, otherwise the file location in the project will be different from that seen in the Finder’s view):

<key>Definitions</key>
<dict>
	<key>MyClasses/SomeClass.m</key>
	<dict>
		<key>Group</key>
		<string>MyClasses</string>
		<key>Path</key>
		<string>MyClasses/SomeClass.m</string>
	</dict>
</dict>
<key>Nodes</key>
<array>
	<string>MyClasses/SomeClass.m</string>
</array>

[Update on 06.04.2011]

Some reader gave me a tip how to add files in a subgroup (i.e., the group inside a group). As an example consider the following situation; you are adding the SomeClass.m file that is in the location below and wanting to add it in the same directory.

Classes/SomeClass/SomeClass.m

In this case you can do this by changing a definition part:

<key>Definitions</key>
<dict>
	<key>Classes/SomeClass/SomeClass.m</key>
	<dict>
		<key>Group</key>
		<array>
			<string>Classes</string>
			<string>SomeClass</string>
		</array>
		<key>Path</key>
		<string>Classes/SomeClass/SomeClass.m</string>
	</dict>
</dict>

and its nodes part:

<key>Nodes</key>
<array>
	<string>Classes/SomeClass/SomeClass.m</string>
</array>

I also added these examples on my templates (link listed at the bottom).

Caution before editing

Keep in mind that TemplateInfo.plist is a setup file for Xcode 4 and any invalid manipulation would cause unexpected actions. For example, the following figure shows a normal behaviour of Xcode 4 during a new project creation, showing a product name and company identifier input (other options such as unit testing are disabled). And pushing the next button asks you which directory to create the project (as expected).

Normal behaviour of Xcode 4

The following wizard pane is abnormal one after some ill manipulation. No product name input is found. Fortunately you cannot go further by pushing the next button.

Abnormal behaviour of Xcode 4

However the worst case happens when you could go further. Even if you meet the situation, do NOT push the next button; Xcode 4 creates a project inside the directory you intend to place the project. For example the directory you are placing your project folder is Desktop, Desktop will become the project folder and other irrelevant files will be gone. I had two times desktop-sweeping during the try-and-error manipulation. Be careful.

Downloads

License

This template is provided under the terms of the MIT license for the reason above, i.e., feel free to use it but at your own risk.