Xcode Project and File Templates

Learn how to create custom project and file templates in Xcode to start new projects and files more efficiently. By Keegan Rush.

Leave a rating/review
Download materials
Save for later
Share

If you’re tired of filling out boilerplate code whenever you begin a new Xcode project, you can save yourself a ton of time by creating customized project templates.

Using Xcode’s project and file templates, you have the power to customize how new projects and files start out their life from the moment you use File ▸ New. These templates allow you to pre-fill the contents of new files and project so that you can get to work on the core of your next excellent app idea more quickly.

In this tutorial, you’ll learn:

  • What Xcode templates are and where to find the default templates that ship with Xcode.
  • The anatomy of Xcode’s templates.
  • How to create project templates.
  • How to create file templates.
  • Getting input from users when they use a template.

Get ready because you’re about to replace some unnecessary boilerplate with custom Xcode templates!

Getting Started

First, click the Download Materials button at the top or bottom of this tutorial to download the project materials.

In this tutorial, you’ll build an app called Stellar Space which uses NASA’s Astronomy Picture of the Day API to show today’s latest space-related and wallpaper-worthy photo. Along the way you’ll how to create your own templates.

Picture of the day in the Stellar Space app

Note: The starter materials don’t contain a starter Xcode project. That’s because you’ll be creating the project by yourself, using custom Xcode project templates!

Imagine for a moment that you are a big fan of the Model-View-ViewModel (MVVM) design pattern. You want to use it in all your apps, and this app is no different. Usually, a new SwiftUI app comes with a ContentView.swift for you to use for the first screen of your app. But instead of using the default template, you’ll create your own template that pairs ContentView with a view model to separate the UI and business logic. Using that template will make your life easier when creating your projects.

Since Xcode’s templating system is robust but poorly documented, you’ll need to explore the default templates Xcode offers so you can know what you can customize.

Understanding Default Templates

Every time you’ve created a new Xcode project in the past, you did so using Xcode’s default templates. These default templates make things easier than starting with a blank project.

Templates decide what your new project will contain. The default templates include many different options including, but not limited to:

  • A blank view with a “Hello, World!” text.
  • Setup code for your Core Data implementation
  • A fully functioning ARKit scene

Xcode’s rich and complex templating system also allows user input, conditional logic and many combinations of files. Each template also offers inheritance, meaning the templates you create can adopt logic from one another or the default templates.

Inheritance also means that all the fantastic behavior of the default templates is up for grabs. Consequently, looking at the default templates is a great way to start creating templates of your own.

Exploring Base Templates

Xcode stores the default templates within the Xcode app bundle itself. To take a look at all the base templates, open a Finder window. You can do this by opening Finder and pressing Shift-Command-G or going to Go ▸ Go to Folder…. Then, copy and paste the location of the default project templates into the resulting window:

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

Finder's folder navigation pop-up containing project templates' folder path

Next, click Go.

Folder content of the Project Templates

The Base folder contains templates that you won’t see directly in Xcode’s New Project dialog. These base templates are abstract, and they serve as good starting points that other templates can inherit from.

Folder structure of the Project Template's Base folder

The Base folder contains most of Xcode’s default templates. iOS templates are in a different folder. You’ll look at those next.

Exploring iOS Templates

In Finder, go to Go ▸ Go to Folder… again. Then, enter the location of the default iOS templates as follows:

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

Click Go.

Folder content of the iOS Project Templates

You’ll see the iOS folder. Here, you’ll find templates for the iOS category in Xcode, as seen in this screenshot:

Xcode pop-up with the iOS categories available for choosing a template for creating a new project

Open the Application subfolder in Finder, and then open App.xctemplate.

Folder content of App.xctemplate

Take a look. When you create a new iOS app, Xcode uses this template. Next, you’ll copy this template to a different location so you can customize it and make it your own.

Creating Your First Project Template

Now it’s time to create your custom project template. There are three basic steps:

  1. Copy an existing template. In this case, App.xctemplate.
  2. Create a folder to store all your custom templates.
  3. Modify the copied template to match your needs.

First, while in the iOS default templates folder, right-click App.xctemplate and click Copy.

Next, open a new Finder window. Press Command-Shift-G or go to Go ▸ Go to Folder… and type in the path to the Xcode folder in your user library:

~/Library/Developer/Xcode

Then, click Go.

Contents of the Xcode folder

The Xcode folder stores device logs, application archives and other user data. Most importantly, for now, it also stores your custom templates. It is in your home directory and therefore is specific to your user account on the machine.

To create new custom template subfolder, right-click in this folder and then click New Folder. Name the subfolder Templates. Then, open the new Templates subfolder.

Create a new subfolder named Templates and open it in finder

This folder will store your custom templates. Now, right-click in the folder and click Paste Item to paste the copied App.xctemplate.

Next, rename the item you just pasted from App.xctemplate to View Model App.xctemplate.

Your new template looks like this:

Content of custom template, View Model App.xctemplate

To make your template work, you’ll need to give it an unique identifier. Inside View Model App.xctemplate, open TemplateInfo.plist with Xcode.

Contents of TemplateInfo.plist

Change the value for Identifier to com.raywenderlich.mvvm.

Voilà! :] You just created a new custom template!

However, you’ll need to add code to make it more meaningful. Xcode knows to read templates in the folder you just created. However if Xcode is already open, you need to quit and restart to enable your changes. Then, go to File ▸ New ▸ Project… to open the New Project dialog.

Scroll to the bottom of the iOS category. Here, you’ll find a new Templates section that holds your custom templates.

Xcode pop-up to choose custom templates

Select View Model App and click Next.

Template options for first custom template

Your template comes with the same rich list of options as the default app template. How does a simple template exhibit all that behavior?