How to Create a Swift CocoaPod swiftly

dependencies

Credit: xkcd

CocoaPods are an incredibly helpful tool for incorporating dependencies into your Xcode projects. But what if you want to create your own pod to share as an open-source project or for internal use? This is exactly where I am today and I decided to document my findings as a reference to myself and others.

I’m going to walk us through the creation of a project I’m working on called ASTimer. ASTimer will be a library for adding some cool functionality to Apple’s NSTimer class.

Step 1:

If you don’t already have CocoaPods installed, it’s as easy as installing a RubyGem:

$ gem install cocoapods

This will tell RubyGems to go out and fetch the latest version of CocoaPods for you.

Step 2:

We’re going to use the pod command line tool in our pods directory:

$ pod lib create ASTimer

If everything goes correctly, CocoaPods should say you’re ready to go with something like this:

Ace! you're ready to go!
We will start you off by opening your project in Xcode

open 'ASTimer/Example/ASTimer.xcworkspace'

Step 3:

Now we should setup the .podspec file (so CocoaPods knows what your project is about) and software license for the pod. The authorities at CocoaPods require that all pods include a software license if they’re to be permitted in the main trunk. I am personally a fan of the WTFPL myself, but use your own discretion.

Add a .podspec file:

Pod::Spec.new do |spec|
spec.name = 'ASTimer'
spec.version = '0.0.1'
spec.license = { :type => 'WTFPL' }
spec.homepage = 'https://github.com/asowers1/ASTimer'
spec.authors = { 'Tony Million' => 'asow123@gmail.com' }
spec.summary = 'ARC and GCD Compatible timer Class for iOS and OS X.'
spec.source = { :git => 'https://github.com/asowers1/ASTimer.git', :tag => 'v0.0.1' }
spec.source_files = 'ASTimer.{h,m,swift}'
spec.framework = 'SystemConfiguration'
end

Next, check to see if your Podspec is acceptable

$ pod lib lint ASTimer.podspec

Step 4:

Finally, we are able start coding our development pods and build an example in the pod example target

Here’s a link to the code I created for this project: ASTimer

Step 5:

Once you’re happy with your Pod and its example code, you can tag a release and send it to the Cocoapods trunk.

If you haven’t already, you will need to register your machine so you can push to the Cocoapods trunk so other developers can access your pod.

$ pod trunk register asow123@gmail.com 'Andrew Sowers' --description='Andrew Sowers'

Once you’re registered, you should be ready to tag your release and push it.

$ git add -A && git commit -m "Release 0.0.1."
$ git tag '0.0.1'
$ git push --tags
$ pod trunk push ASTimer.podspec

Originally published at Haiku Robot.

Show your support

Clapping shows how much you appreciated Andrew Sowers’s story.