Create Custom Xcode Templates

Mayur Patel
Nov 2 · 8 min read

Hey Guys, We have all been through where we have to time and again set up a new project, and every time we have to use some common files/templates in every new project and isn’t it tiresome to set up network manager, API calls, Alert manager, etc for multiple projects? We came across this pain and after giving it a thought and all the agony a programmer has to go through we thought how can we make a template that’s reusable and save us time? Adding these classes by writing manual code is time-consuming. With Xcode templates, we can speed up the process of adding them to any of the projects that require these files. After all, time is money 💰 💰 💰 💰.

So let’s begin and see how we can make those classes reusable by using the Xcode 13 and the way we are going to achieve that is via creating Xcode templates.

So now let’s see how to configure it and set up it.

What are Xcode Templates?

Xcode Templates is a tool for creating code snippets to give you a better starting point to achieve your goal. In this tutorial, I will walk you through preparing a custom template for MVVM project architecture with RxSwift and reusable classes of RxServices.

Often we need to create structure and files from the scratch for a new module, and this process is pretty much the same each time. For example — in the MVVM pattern, to create a SignIn module we need to create at least 3 classes:

  1. SignInViewModel

Adding each class using the required code is time-consuming. With Xcode templates, we can speed up the process of adding them to our project. I will show you how to configure a template for use with a new MVVM module.

Let’s get started

All the Xcode default template files are located at /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates and grouped into sections by folder name.

We can use Terminal or Finder to go directly to this folder. With Finder we can use the Go Menu, then Go to folder option.

Inside we find many subfolders and inside of them there are others that have the extension .xctemplate as we can see in the picture below:

if we are using an existing project with Xcode or creating a new one, when you open the New->File menu (cmd+N) you can see the below options available.

See the above 2 screenshots you can find the above open folder in Xcode as a template like a Source, User Interface, Core Data, Apple Watch, etc…

If you open the folder Source with finder, you can see a certain number of files with extension .xctemplate inside. We can open the subfolder named Swift File.xctemplate and see a group of files:

Here we can see a file with extension .swift and a .plist file.

If we open the __FILEBASENAME__.swift file we can see this content:

Here __FILEHEADER__ indicates the info about the FileName, ProjectName, AuthorName, etc..

The file templateInfo.plist contains information about the template:

In the Above XML file, you can see the Key Kind which should have the fixed value so it has its own predefined value for the file. __FILEBASENAME__.swift is the name of the swift file that will be used as a template.

The following list of fields is the most common keys you could find.

— Kind: The value of this key is always: Xcode.IDEKit.TextSubstitutionFileTemplateKind

— Description: It contains a description of the template file

— DefaultCompletionName: the default name of the file.

— SortOrder: used to rearrange the order in the container category.

— Summary: Description of the template file.

You can also find these optional fields:

— Platforms: the template file will be available for all platforms by default. If we want our template only for one platform we should define something like this com.apple.platform.iphoneos.

— MainTemplateFile: In case a template has more than one file, MainTemplateFile specifies which file should be opened first after the template’s files have been created.

— Options: It determines the options that you can configure in the first step of creating a new File.

Create a new custom template

The procedure to create a template for Xcode is quite easy. I would like to create my own template and make sure that it is presented on the screen together with default templates.

Hence, I can create a container folder where Xcode stores all the others containers:

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

and then create inside of it a subfolder with the .xctemplate extension that will contain all the required files and subfolders.

But, this isn’t the right place to do this job. If we update Xcode, the File Templates folder would be deleted and replaced with the previous one that we would lose all our templates.

Instead, we can create our specific folder here:

~/Library/Developer/Xcode/

Again, you can use Finder to reach the folder above and create a new folder named Templates. Once it is created we can add our specific container that I will name: RxSwift MVVM. After that I will create a new subfolder called MVVM.xctemplate inside my container as we can see in the picture below:

I added a new set of icons inside of MVVM.xctemplate that you will see in the Xcode template window after that add TemplateInfo.plist file with the below content:

Our template will be available for all platforms. We can set the value for only iPhone as com.apple.platform.iphoneos.

In the above file, we add a key called Options which allows us to configure a selection that will appear in the file creation dialog window of Xcode once we select the template. Also, we can set the type of other options like TextBox where the user can write something or Popup where the user must select a value from a list of predefined items. When the user selects our template, Xcode asks him the name of the module, for example, SignIn. Then template will automatically generate a list of files with classes declared inside named: SignInViewController, SignInViewModel, SignInModel, and other files with SignIn prefix in their filename.

The Options key contains the below possible fields.

— Identifier: used to uniquely identify options.

— Default: default selection or text.

— Description: Description of the option. You can see the text when the mouse hovers over it.

— Name: This text will be shown on the left side of the control.

— Required: determinate is an option required.

— SortOrder: determine the position of the option.

— Type: Its defined options type like checkbox, text, static, combo box, popup.

— NotPersisted: determinate if the value will be saved for the next time.

— RequiredOptions: It is used to enable/disable the option.

— Values: It's used for defining the option of the combo box.

Now we are all set. Open the Xcode and New->File menu (cmd+N) you should see the below window.

In the above picture, you can see the MVVM template which we have created. but if you try to create a new file selecting it, nothing will create because we have not added subfolders in the MVVM.xctemplate folder.

Now we are going to add the required folders for RxSwift MVVM and RxServices as you can see in the picture below.

Here we have added Model, View, and ViewModel folders for MVVM and Services folder which contains reusable classes for RxService.

Now we are going to observe the content of ViewController which we have added to the template folder.

Here the ViewController contains basic information about UI and initialization of ViewModel and it's very easy to understand.

Also, we can see ___FILEHEADER___ that was used to add info about the file and project name and the author name.

In the above code, you can see the class name as ___VARIABLE_productName:identifier___ViewController which will automatically add a prefix of our class name.

Now open the Xcode, create a new file and Select our MVVM template it should like below.

The module name is enabled
The module name is disabled

If you compare the above pictures, If I select RxServices module name is disabled and for MVVM it is enabled because we have set the RequiredOptions true only for MVVM.

Now select the Class type MVVM and define the name as per the requirement and it will create the project like shown in the image:

The template has automatically generated the SignIn prefix for all classes as we have added a file named __FILEBASENAME__ in the template folder.

If you observe the SignInViewController then it should like below:

So this is the best time server option to use reusable classes.

Conclusion

Hence we can reduce our development time by creating custom templates. We need to manage our time well and use the tools that Xcode provides us.

If you have any questions, please feel free to ask.

You can download the source code from the below link.

https://github.com/Mindinventory/XcodeTemplate

Happy Coding…!!!😊😊

Mindful Engineering

Mindinventory Tech Blog