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. 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:
➜ 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.
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
.
// 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:
➜ 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! 🎉
➜ 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.