How to create your own Cocoapod (Simple, easy steps)

Matan Cohen Abravanel
Swift2Go
Published in
5 min readAug 8, 2018

This topic is a mystery to me. I never found a simple few steps explanation for a complete process.
Every time I would publish to Cocoapods, I use to try and remember the steps.
So I made my note, with a few easy steps, and today I will share it with you!
If you never published a project to Cocoapods and your an iOS developer, this is your chance to give back to the community, and shine!

Here we go!

Before starting, I recommend adding your root project to git, so you can erase progress at any time, and go back to start.

Add your folder to git, if you didn’t already:

$ git init 
$ git add -a
$ git commit -m "my first commit"

Ok let’s do this!

So you have written this super cool component. SDK, whatever. And now, you want it published for the rest of the community to use, here we go:

1. Choose a name, check availability at https://cocoapods.org

This step should probably happen before creating your repo, but you can always rename the project/classes after experiencing your name has already been taken (:

2. Specify classes with “open” or “public” for developers to access them

Yes, you should to explicitly mark props and classes for other developers could access them, as part of “Access control”:
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html
Only add to classes that are part of your API.

3. Add podspec file to the root of your project

Podspec is the file you define project details and describes what files are part of your pod.
To create a podspec from the terminal, just run from project root:

$ touch YourProjectName.podspec

Here is an example podspec content I use for my new pods, copy past it, and edit with your information. (Open file with a text editor)

Pod::Spec.new do |s|s.name              = 'YourProject'
s.version = '0.0.1'
s.summary = 'YourProject'
s.homepage = 'https://github.com/mcmatan/YourProject'
s.ios.deployment_target = '8.0'
s.platform = :ios, '8.0'
s.license = {
:type => 'MIT',
:file => 'LICENSE'
}
s.author = {
'YOURNAME' => 'Bob'
}
s.source = {
:git => 'https://github.com/mcmatan/YourProject.git',
:tag => "#{s.version}" }
s.framework = "UIKit"
s.source_files = 'YourProject*' , 'Classes/*', 'Resource/*'
s.requires_arc = true
end

Let’s sum up all the fields you should change their values:
s.name, s.summary, s.homepage, YOURNAME, s.source :git, s.source_files

Note in s.source_files you define all folders that contain files which are part of your pod.

4. LICENSE

Yes, every Cocoapod must have a LICENSE since some time ago… I use MIT license; you are welcome to copy past it. It says that anyone can do whatever with your code.

First run from root:

$ touch LICENSE

Open LICENSE file from code editor, and past this:

MIT License
Copyright © 2018 Matan Cohen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4. Project structure + Example project

Sure, you can probably just upload the damn project the way it is. But if you are a professional that wants some stars ⭐️ then master the details, and deliver an easy to maintain and understand project structure.
No one whats to get your damn .xcodeproj and plist files with a pod they have installed.

Final structure should look like so:

Resource: Images & other assets
Classes: your source files
ExampleProject: A project that uses as a dependency your new pod , for tests and examples

Create both folders: Classes and Resources, throw the files you would like the pod to contain in the correct folders (From within your project to the newly created folders)

Now delete your original project: “.xcodeproj” and “project folder” (after you removed all necessary files to the new folders we created)

Now create at the root, a new project named ExampleProject. This project will have a dependency of your new pod, and use it locally. It will help you get a perspective and test how your pod will work after it It’s published live. (And if it worked at all)

Check one of my projects to see the final app structure in action:
https://github.com/mcmatan/TopicEventBus

5. Test pod locally

From within the newly created project folder ./ExampleProject, run

$ pod init

Now that we have a podfile, set your new pod as a dependency

target 'ExampleProject' do
pod 'YourProject', :path => '../'
end

Open ExampleProject.xcworkspace, and boom! You can import your new pod, and use it like a pro!
If you are not able to reference classes or protocols, you probably should change there “Access controller” explained on step 2
Run, test your pod works as expected.

5. Push to Github

If you didn’t already, this is the time to push your repo to origin:

(Add project to git, if you didn’t already: $ git init)

$ git remote add origin git@github.com:yourUser/YourProject.git
$ git push -u origin master

7. Run lint

Before publishing, you must run lint on your code I always add “ — allow-warnings”
Run:

$ pod lib lint - allow-warnings

If there are errors, fix them (:

8. The only thing that is left is to push to Cocoapod!

Here are two examples of push commands:

First-time push to Cocoapods

$ git add -A && git commit -m "Release 0.0.1." 
$ git tag '0.0.1'
$ git push - -tags (Some times will need to type this by hand)
pod trunk push YourProject.podspec - allow-warnings

After the first time (On every update)

Update podspec file, to version 0.0.2 (Version should bump every time)

git add -A && git commit -m "Release 0.0.2." 
git tag '0.0.2'
git push
git push - -tags
pod trunk push YourProject.podspec - allow-warnings

You have done it! 🎉

Now advertise your repo, and wait for the stars ⭐️!

Check you repo out at https://cocoapods.org/
The community says thank you ❤️

--

--

Matan Cohen Abravanel
Swift2Go

The man who does not read has no advantage over the man who cannot read