---
title: "#60: Creating a CocoaPod 🍩"
url: https://littlebitesofcocoa.com/60-creating-a-cocoapod
author: Jake Marsh
date: 2015-08-14
published: 2015-08-14
topics: ["Tools"]
description: "CocoaPods are an extremely popular way to publish and use third-party libraries on iOS and OS X. Let's create and publish one of our own."
---

# #60: Creating a CocoaPod 🍩

By Jake Marsh, August 14, 2015. HTML: https://littlebitesofcocoa.com/60-creating-a-cocoapod

[CocoaPods](https://cocoapods.org/) are an extremely popular way to publish and use third-party libraries on **iOS** and **OS X**. Let's create and publish one of our own. Our **Pod** will provide shortcuts to commonly used directories, like the user's documents directory as `NSURLs`. Let's get started.

We start by generating our new **Pod** like this:

``` bash
➜ pod lib create common-paths
```

We'll be asked a few questions such as what language we want to use (Objective-C/Swift), whether we'd like to create a demo application, etc. then a complete Xcode workspace and directory structure will be created.

<img src="https://media.littlebitesofcocoa.com/bite-assets/060-creating-a-cocoapod-podspec.png" style="max-width: 183px;" />

We'll open up the newly created workspace and get started by filling out all the metadata (summary, author, etc.) about our new **Pod** inside the `.podspec` file.

**CocoaPods** generated a blank .swift showing us where to put our code. We do as we're told, and replace it with a new file called `NSURL+CommonPaths.swift`.

<img src="https://media.littlebitesofcocoa.com/bite-assets/060-creating-a-cocoapod-replaceme.png" style="max-width: 229px;" />

``` swift
// NSURL+CommonPaths.swift

import Foundation

extension NSURL {
  static var documentsDirectoryURL: NSURL? {
    guard let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first else { return nil }
    return NSURL.fileURLWithPath(path)
  }
}
```

Neat, now let's publish it! We create a new Github repo matching the source we entered in our `.podspec`. Then we commit our changes, tag them, and push them up:

``` bash
➜ git add .
➜ git commit -am 'Initial commit.'
➜ git tag 0.1.0
➜ git push origin master --tags
```

Finally, we'll register via our email and follow the instructions, then release to the world! &#127881;

``` bash
➜ pod trunk register jake@deallocatedobjects.com 'Jake Marsh'
➜ pod trunk push
```

Here's our new `common-paths` **Pod** up on the **CocoaPods** website: [common-paths](https://cocoapods.org/pods/common-paths).
