Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create project templates in Xcode 4

Its really hard to find a tutorial to do this as most information relates to Xcode 3. Answer below may help some people.

I sourced most of my information for the answer below from the following sites:

  • http://meandmark.com/blog/2011/12/creating-custom-xcode-4-project-templates/
  • http://www.bobmccune.com/2012/03/04/creating-custom-xcode-4-file-templates/
like image 357
bennythemink Avatar asked Mar 19 '13 05:03

bennythemink


People also ask

How do I create a project template in Xcode?

Creating A New Project Template To get started create a folder under “Project Templates” to store the template. The folder name shows up as the section name in the Xcode template browser. To make changes to the template we need to edit the TemplateInfo. plist file.

Where are Xcode templates stored?

They are located in Xcode. app/Contents/Developer/Library/Xcode/Templates or Xcode. app/Contents/Developer/Platforms/<platform_name>/Developer/Library/Xcode/Templates.


3 Answers

I spent hours searching the web to find information about doing this. Its not actually that hard to set up project templates for Xcode4.5 but its hard to find information on the web that puts it all together! Hopefully the steps below will help you to create your own.

Setting Up

  1. Lets start by getting a copy of an existing Xcode project template to use as a base. Open finder, go to Applications and right click on Xcode to show package contents. Navigate to Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Application/

[EDIT] In Xcode 5 the PATH is as follows: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists you should create it too. Credit to seufagner in the comments below for the update although I have not tried this myself. [/EDIT]

  1. Copy (do not cut/paste!) one of the listed templates.
  2. Open another finder window and navigate to the following directory (to unhide your Library folder type this cmd in terminal: chflags nohidden ~/Library/ or select press the alt/option key when clicking on Finder/Go) /Library/Developer/Xcode/Templates (you may need to create this folder if not already present) /Project Templates/
  3. Create a folder in here. Call it whatever you wish, a suggestion is your company name. This name appears in the left hand menu of the new project dialogue in Xcode. e.g. in the attached image I've called mine Appscore, there's another one there for cocoas2d. enter image description here
  4. Paste the project template in here and change its folder name to whatever you wish e.g. MySuperProjectTemplate.xctemplate. We are not done yet though as we need to change the template's identifier. Otherwise it will not appear in the Xcode new project dialogue window.
  5. Open the TemplateInfo.plist file in TextEdit. Search for the Identifier key. You should see a string value something similar to "com.apple.dt.unit.XXXXXX". Replace this with whatever you wish as long as its unique. I again suggest adding your company name and a name that describes the template.
  6. If you now open Xcode you should see the project template appearing under your company name in the new project dialogue.

Customising

At the moment you have a copy of an existing project template which is not very useful. I'm guessing you have a number of classes that get reused in nearly all your projects? How about we include them into this template?

  1. Copy the files you wish, and paste them into your new project template i.e. navigate to /Library/Developer/Xcode/Templates/Project Templates//MySuperProjectTemplate.xctemplate/
  2. Open the TemplateInfo.plist file in TextEdit again. First thing we have to do is tell the project template to include the new files so search for a key called "Nodes" that has an array of values. Add the two following lines:

    <string>_VARIABLE_classPrefix:identifier_.h</string> <string>_VARIABLE_classPrefix:identifier_.m</string>

For example if your controller was called BaseViewController the lines would look like:

<string>___VARIABLE_classPrefix:identifier___BaseViewController.h</string> <string>___VARIABLE_classPrefix:identifier___BaseViewController.m</string>

Step 2

  1. Next find the Definitions key and you should see a dictionary as its value. In here we have to add a reference to the included files. Create a new key and call it ___VARIABLE_classPrefix:identifier___BaseViewController.h (again taking the BaseViewController as an example).

Step 3

  1. The value of this key is again a dictionary. It contains a key called Path and a string value which is the name of the file e.g. BaseViewController.h
  2. I've attached the following images to show what I mean as I think my description is falling short. You may notice that there is a key in there called "Group", this as you can guess allows you to create groups and input files directly :D

Hope this is actually useful to someone :) Any questions comment below and I'l do my best to answer. I did this a few weeks ago so my memory is a little hazy.

like image 183
bennythemink Avatar answered Sep 30 '22 14:09

bennythemink


You can also learn a lot from inspecting the existing project templates, which you can find in:

/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/

for Mac and

/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/

for iOS.

There are few sample templates also on GitHub by Reid Main and another one by Acani. There are also AFNetworking templates, created by Mattt Thompson. See all the examples, including ones built by Apple and then you can start creating your own.

like image 26
Legoless Avatar answered Sep 30 '22 12:09

Legoless


A good overview of all of the variables used in the plist file can be found here: https://gist.github.com/shazron/943736

like image 38
bvmobileapps Avatar answered Sep 30 '22 12:09

bvmobileapps