How to create project templates in Xcode 4

20,603

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

@bennythemink Your answer is really very much help full but I want to add some more things in your answer. When we copy xcode predefine template than this key contains

<key>Ancestors</key>
<array>
<string>com.apple.dt.unit.storyboardApplication</string>
</array>

this value. When we use this key value in Custom template of xcode it will create AppDelegate class by default. Which we can not move to folder only we can shift it into group. So i suggest to add this:

<key>Ancestors</key>
<array>
<string>com.apple.dt.unit.objectiveCApplication</string>
<string>com.apple.dt.unit.iPhoneBase</string>
<string>com.apple.dt.unit.prefixable</string>
</array>

value in Custom template so that we will have fully empty template in which we can do any custom stuff.

Share:
20,603
bennythemink
Author by

bennythemink

Born to a large rock in Russia i was raised by an Amish family until i reached my teen years. Then when i heard the music of Rick Asley and Wham I decided that the country life was not for me, so i packed up things and headed to the big city with my pet hamster i called Hans christian anderson. unfortunately city life proved to be a big change for a country boy like myself and i was eventually became homeless in a matter of weeks. our struggle for food and clothes turned me to pimp my beloved Hans out to middle aged gay couples. things were finally on the up as me and Hans saved our earnings and managed to buy a nice condo on the Dominican coast. we spent the next 2 years in relative bliss only needing to go on the job every other week. life was good, that is until and unfortunate mining accident occured with Hans in one of his regular customers. Hans was left brain dead and the hospital eventually turned off the machine. on the brighter side Hans left me everything in his will! end:) Oh and I also do iPhone and web development!

Updated on April 19, 2020

Comments

  • bennythemink
    bennythemink about 4 years

    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:

  • Josef Salyer
    Josef Salyer about 11 years
    I agree with Foram. This explanation of how to create your own custom project is great.
  • Phong Le
    Phong Le about 11 years
    Is there a way to add a static library?
  • bennythemink
    bennythemink about 11 years
    @PhongLe I do not think so but I have not really looked into it so hopefully I am proved wrong :)
  • PJR
    PJR over 10 years
    hey really nice answer but i have a doubt if I creates a template and it contains several modules like 1) db related 2) network related etc…now in my app if i want only one module as per example DB related code. is it possible by using the template that user can select modules which is needed..not the each one (different files) in particular templates.
  • bennythemink
    bennythemink over 10 years
    @PJR thanks for your comment. I think you are correct, I cannot think of a way to have the functionality you mention :(
  • PJR
    PJR over 10 years
    so @bennythemink it suggests that Template can automatically integrate all the files by which we created it.we can not restrict it.:)
  • PJR
    PJR over 10 years
    Hi I created template and write file names in defination and nodes , but in new project those two files are coming empty!!
  • bennythemink
    bennythemink over 10 years
    @PJR this sometimes occurred for me too. I do not know the exact problem but I re-added the files and it worked for me.
  • PJR
    PJR over 10 years
    Okay , Thanks @bennythemink same works for me..I think we have to place files in Definitions part correctly.so if Declaration is right inside "nodes" array and Definitions are wrong then it creates empty files :)
  • seufagner
    seufagner over 10 years
    In Xcode5 the correctly path is: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists you should create it too.
  • Morkrom
    Morkrom over 10 years
    The only one that I still have trouble with is creating a group.
  • bennythemink
    bennythemink about 10 years
    Thanks for the comment Legoless! Anything Mattt Thompson does is great quality so I will be checking his templates out!
  • Admin
    Admin almost 9 years
    Many thanks for this and PLEASSSE Apple read this post above and see how much of a pain it is to make a new template. CHANGE THIS! (P.S. of course I sended a bug report)