Create a CocoaPod

In the previous post we created a dynamic framework.

Now we’re going to share this code with the world, by turning it into a CocoaPod and uploading it to cocoapods.org:

Screenshot 2016-02-16 16.13.44.png

First, drag your whole project folder to Terminal, and create a podspec. In Terminal, type:

pod lib create ExampleProjectName

(Replace ‘ExampleProjectName’ with the name of your project, but be sure to give it a different name to your framework project name)

This pod lib create command is like a pod wizard, and will go through a series of questions to customize your pod. Your answers should be Swift, and Yes to include a demo application. The wizard will generate a podspec file and an Xcode project and workspace that you can use to demonstrate how to use your library, and open it up automatically for you. If you want to look for the new Xcode workspace in the Finder, you’ll find it located in ExampleProjectName/Example/ExampleProjectName.xcworkspace.

In the Project Navigator in Xcode, find Podspec Metadata. In here are three files you’ll want to edit:

  1. PExampleProjectName.podspec – This specifies all the details of your CocoaPod, including name, version number, dependencies, minimum requirements etc. The wizard prepulates this file with prefilled details and comments (anything after #) to help you understsand the expectations. Go through this file editing it for your project.
  2. README.md – The README is prepopulated with some explanations of your pod, instructions how to install etc. Feel free to add additional details.
  3. LICENSE – A license for distribution of your code, This defaults to the MIT License, but feel free to edit this.

Now to edit your two projects.

  1. Remove the references of your Swift classes in your dynamic framework project(leave behind ProjectName.h and Info.plist)
  2. Move these classes from your project folder to your new example project, in the Pod/Classes folder.
  3. Drag references to the new locations of your classes back into where they were in the original project in Xcode. (deselect Copy items if needed)
  4. Build your dynamic framework to be sure all is still working properly.
  5. Drag references to the classes into your example project, in the Classes folder.
  6. Remove the ReplaceMe.swift file – you can Move to Trash.
  7. Now to install the pod – drag the Examples folder to the Terminal, and install your pod to your example project:
    pod install
  8. Build your project.
  9. Hooray. You should be able to use your code in the example project! Write some code to demonstrate its use.

Validate your podspec

Now it’s time to check your CocoaPod passes validation. Enter the following into Terminal:

pod spec lint ProjectName.podspec

It should provide you with helpful guidance as to what changes you need to make, if any, to your podspec and files.

If you see the message:

ProjectName passed validation.

you’re good to go! Just one more thing to do.

Update the git tag

Before you upload to CocoaPods, you need to demonstrate that you are describing the same version of the github project in the podspec. To do this you need to give your github project a tag and push up to github.

git tag -a 0.01 -m "First CocoaPod version"
git push --tags

Be sure that your version number matches the s.version number in your podspec.

Upload to CocoaPods

Now finally, we’re ready to upload to CocoaPods!

pod trunk push ProjectName.podspec

Soon you should see your project appear on CocoaPods! You can see my Sprite Kit Easing framework on CocoaPods here.

Install your pod

You should test your pod from a user’s perspective. Create a new Xcode project, and then create a Podfile. Mine looks like:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
platform :ios, '8.0'

pod 'SpriteKitEasingSwift'

Install your CocoaPod:

pod install

You should see your pod install, and a workspace created. Open the workspace and build your project.

You should now be able to access your code. Don’t forget to import your framework:

import SpriteKitEasingSwift

Make changes

Have to make changes to your code? Easy. After making your changes you just need to:

  1. Update version number in the podspec.
  2. Update the git tag and push i (as per above).
  3. Check everything is still valid (as per above).
  4. All good? Upload it to CocoaPods (you guessed it, as per above)

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with: ,
Posted in Swift
4 comments on “Create a CocoaPod
  1. Amitay Molko says:

    Hi,

    I am having some trouble with the setup of my two projects.
    I have a framework project which is hosted on a private repository.
    I have a pod project with example that is hosted on a public repo.
    I don’t want the public repo to contain the code from the framework project, just the binary.
    How do i get the pod to just contain the binary and not the code?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: