Custom Xcode Template for Static Library in iOS

Where to store your templates?

Your custom templates are stored under the following path ~/Library/Developer/Xcode/Templates. When you first navigate to this path you may find it empty, this is expected as you may not have any custom templates. In your Templates folder you first create a folder called Project Templates. This folder will then hold all your project templates. If you want to create file templates then you create a folder called File Templates. It is important to get the names correct otherwise Xcode wont recognise your custom templates.

Lets create some more folders…

In your Project Templates folder you then create a folder to group your templates and you can call it what ever you like but for now lets call it Personal Static Libraries. This folder is where we are going to add our custom templates.

  1. Kind: A string type used to identify the template type for example you use Xcode.Xcode3.ProjectTemplateUnitKind for project templates and use Xcode.IDEFoundation.TextSubstitutionFileTemplateKind file templates.
  2. Identifier: A unique identifier for a template. Used to uniquely identify a template or to inherit properties from ancestry templates more on that later.
  3. Concrete: A Bool type. Must be set to YES for Xcode to show your template when choosing a template for your project. If missing or set to NO Xcode treats the template as an abstract base template that other templates can inherit from.
  4. Ancestors: An Array type to hold a collection on Identifier, used to inherit from other templates.

Copying the default static template

The following path is where default templates are stored: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates. If your Xcode app name is called other than Xcode then don’t forget to replace Xcode.app with the appropriate name in the path.

  1. ___PACKAGENAMEASIDENTIFIER___: The Objc and Swift files are default files you get when you create a project based on the selected language. For a Swift static lib you obtain the ___PACKAGENAMEASIDENTIFIER___.swift and for Objc you obtain the .h and .m files. Macros are used as file names so they are replaced on project creation. For more info on what macro to use visit here.
  2. TemplateIcon.png: These are optional png files used to spice up your custom templates, so that its more recognisable.

Setting an identifier

As mentioned previously it is important for the identifier of a template to be unique. The default templates provided by Apple uses a com.apple.dt.unit prefix, so every template provided by them has it. For our custom template we have to provide our own identifier prefix. I decided to go for com.personalLibrary.dt.unit but feel free to use your own.

Setting up swiftlint

Paste the following dictionary as an array item to BuildPhases under the first item in the Targets array in TemplateInfo.plist to setup swiftlint.

<dict>
<key>Class</key>
<string>ShellScript</string>
<key>Name</key>
<string>Run SwiftLint</string>
<key>ShellPath</key>
<string>/bin/sh</string>
<key>ShellScript</key>
<string>$(git rev-parse --show-toplevel)/Tools/SwiftLint/swiftlint --path ${SRCROOT}</string
</dict>

Setting up Build Configuration

Paste the following to the root dictionary in TemplateInfo.plist to setup build configurations.

<key>Project</key>
<dict>
<key>Configurations</key>
<dict>
<key>Debug</key>
<dict/>
<key>Stage</key>
<dict/>
<key>AppStore</key>
<dict/>
</dict>
</dict>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melvin John

Melvin John

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC