XCode Templates — Part 1

Rakesh Chander
5 min readOct 24, 2020

XCode 11 & above

We, as a developer, should follow defined practices while developing features in a project or create a new project. Every dev team has their own defined coding guidelines and utility classes / methods. There is always some common steps / code, which keeps on getting repeated across different features / projects. Additionally, its must to remember all those to comply with development guidelines.

XCode Templates comes as a rescue for us and help us in automating guidelines at the time of setup.

Refer Sample Templates in my Git Repo — xcode-templates

Templates are very useful in terms of

  • setting up basic structure with minimum input
  • reducing duplication of steps
  • standardising structure

In this story, I am going to discuss mainly below types of Templates —

  • Project Templates
  • File Templates

Adding new Templates —

Templates can be created by following defined structure as per Apple XCode IDE. Each template must have below files at root of xctemplate folder-

  1. TemplateInfo.plist
  2. TemplateIcon.png
  3. TemplateIcon@2x.png

Now this new xctemplate folder needs to be placed at ~/Library/Developer/Xcode/Templates and XCode IDE will automatcally pick up this.

For Showing Templates as groups, we can create folders and place xctemplate folders in required structure under ~/Library/Developer/Xcode/Templates

Best way to start with is to copy any existing XCode Templates and modify them to use as custom templates. You can get existing default XCode Templates at below locations —

You can refer existing templates at below location -

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

For default iOS sepcific templates, you can browse to below location in your machine -

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

Template Info.plist

This is the main file in defining any template. At top level there are below entries —

  • Platforms —(optional) which takes string array and defines out the type of OS it will work with. If this key is not defined, then that template will be visible for all OS. For iOS apps specifically, mention — com.apple.platform.iphoneos
  • Kind — which takes string entry and defines out which type of template it is.

Values for Kind Key

File Templates — Xcode.IDEKit.TextSubstitutionFileTemplateKind

Project Templates — Xcode.Xcode3.ProjectTemplateUnitKind

  • Identifier — (optional)A unique identifier for your template. It can be used as ancestor name hen extending it to create new template. refer below
  • Ancestors — (optional) which takes string array. We can define any of existing default / custom templates as ancestor to new template so that all capabilities of parent template are available and you can add new capabilities. Its very useful as we can extend any default iOS Project / File templates to start with.

For Custom UI Test Template — com.apple.dt.unit.iosUITestBundleBas

For Custom Unit Test Template — com.apple.dt.unit.iosUnitTestBundleBase

For Custom iOS App Template — com.apple.dt.unit.applicationBase, com.apple.dt.unit.iosBase

For Custom File Template — No ancestor required

  • Options — which takes array of Dictionaries defining user input / options for that custom template. For example, lets say we want to capture Feature Name for which MVVM file set need to be prepared, then we can ask featureName as a option.

It’ll look like as below —

Enough for theory for now, Lets start with FILE TEMPLATE first.

File Template —

Lets say, we want to create a MVVM file template where we want to generate set of below Files with some pre-populated code for linking with required feature name.

  • Model — Data Model & UI Model
  • View — View Controller & XIB
  • ViewModel
  • DataSource

When user will be using our template, we need to ask feature name against which this template will generate Files and name all Files & Classes using that name only. Create TemplateInfo.plist as below entries -

Now we need to place template swift & xib files in same template folder. Currently files can be empty. Refer below image—

__FILEBASENAME__ will pick up module name and get replaced while we use this custom template.

Give it a shot. You will see all files are created but they are empty.

Lets start with putting up template code in UI Model -

////  ___FILENAME___//  ___PROJECTNAME___////  Created by ___FULLUSERNAME___ on ___DATE___.//  ___COPYRIGHT___//import Foundation/// ___VARIABLE_moduleName___ErrorUIModel for API Success Response Mapping to View Elementspublic struct ___VARIABLE_moduleName___ErrorUIModel {/// statusCode of API Error Responsepublic let statusCode : String/// statusMessage of API Error Responsepublic let statusMessage: String/// constructor for ___VARIABLE_moduleName___ErrorUIModelpublic init(statusCode : String, statusMessage: String = String.empty) {self.statusCode = statusCodeself.statusMessage = statusMessage}}/// ___VARIABLE_moduleName___DataUIModel for API Success Response Mapping to View Elementspublic struct ___VARIABLE_moduleName___DataUIModel {//TODO - Set UI consumable variables//TODO - Set public init for variables}

As you see — moduleName — id given to option dictionary item in templateinfo.plist and being accessed here using ___VARIABLE_moduleName___ syntax. Similarly you can add more options if you need.

This way you can pre-populate your template code files and when you will create new file set using this template, you will see most of the heavy lifting is already done for you.

So, creating File Template is having this much only!! Lets cover up Project Templates in next story — XCode Templates — Part 2.

--

--

Rakesh Chander

I believe in modular development practices for better reusability, coding practices, robustness & scaling — inline with automation.