Xcode project templates 2: How to auto-configure a project
Technology
3
 min read
October 4, 2021

Xcode project templates 2: How to auto-configure a project

Andrej Jasso
Andrej Jasso
iOS Developer

Every time I set up a new project, it took a full working day to get it ready for development. Until I discovered templates. Xcode templates have been around since the days of Xcode 4, but I only recently learned about them and they have completely changed the way I look at project creation. Try them with me and new projects will suddenly be that much more consistent and maintainable.

In the previous article about Xcode projet templates, I walked you through running this tool step by step and pointed out its benefits. This time I will introduce you to the additional capabilities of Xcode project templates, where when generating the project we will have a podfile ready with a pre-filled configuration. What that means in practice is that, after we generate new project, we enter the project via the console, we call
pod install

and the project will have pods configured. This will significantly reduce the configuration of the project and the style of the podfiles will be uniform when it comes to more complex podfiles.

The first thing to do is to create a new template, similar to last time, in the project templates folder with the .xctemplate extension

Here we will first create a Podfile file and insert the pods we want to import into each new project.

Note the variables used __VARIABLE_VERSION__ which sets the minimum project version below and __PACKAGENAME__ which sets the project name. When creating a project, __VARIABLE_VERSION__ will be the input parameter and __PACKAGENAME__ will be taken from the project name.

Then it is necessary to configure the TemplateInfo.plist file which represents the configuration of the template

Here it is essential to note the identifier variable

  • Kind: The template type is project
  • Concrete: Determines whether the template is visible when selecting a template or whether it is only part of another template and therefore not visible when selected. We will use this template as part of the template from the previous article and thus set false
  • Identifier: Specifies the template ID between templates. This is so that we can refer to this template from another template
  • Definitions: Used to define the variable, which in our case is the Podfile file and the place where it will be created in the project.
  • Definitions \ Path: Services as the path to the file in the templates folder
  • Definitions \ Group: Services as a file path after creating a new project. If there is more nesting, it must be defined as an array of strings where each string represents one nesting of the path
  • Options: Here we have defined all inputs of the template user when creating. There is a checkbox if we want to include podfile and iOS version as text input.
  • Options \ Identifier: Option ID. This can be accessed from files via  ___ VARIABLE_VERSION ___ where you enter the name of your input instead of VERSION.
  • Options \ Name: Field name
  • Options \ Desciption: Tooltip array
  • Options \ SortOrder: Specifies the usage of the field in the template form
  • Options \ Type: Specifies the type of user input
  • Options \ Default: Specifies the default value for the option
  • Options \ Units: Specifies which file the template will select within the logical branch, in this case within the checkbox it is either true or false and if the option is false it does not select any files and therefore in our case it is not necessary to define the option.
  • Options \ Required: Specifies whether it is possible to create a project even with an empty option
  • Options \ NotPersisted: Specifies whether the template will remember the last option filled

However, we now want to insert this template into our project template from the previous instructions.

The result will be as follows.

The only way these files differ is the ancestors key where we add the Identifier from the template we just created.

The only way these files differ is the ancestors key where we add the Identifier from the template we just created.

Like what you see?
Join our newsletter.

Great! Welcome to newsletter.
Oops! Something went wrong while submitting your email.
High quality content once a month. No spam, we promise.
Your personal data is processed in accordance with our Memorandum on Personal Data Protection.

Páči sa vám náš content?
Odoberajte newsletter.

Great! Welcome to newsletter.
Oops! Something went wrong while submitting your email.
Vaše osobné údaje sú spracované v súlade s našim Memorandom na ochranu osobných údajov.