Making Your Own CocoaPods — Part 2

Bharghav Kumar
6 min readFeb 24, 2019

Cocoapods and it’s installation as well as the creation of private spec repo were demonstrated in the first part. Don’t miss the first part to create private cocoapods.

Actually, this series of the story starts with an intention of private cocoapods but in the journey of writing, it rejuvenates me to make it completely along with public cocoapods at one shot.

In this story, you are going to experience a step by step demonstration of cocoapod creation and it’s distribution (private as well as public).

Making Own CocoaPod

Step 1

This step is for direct part 2 viewers because of part 1 viewers already experienced while creating private spec repository.

Log in to any source control management tool (i.e Github, Gitlab or Bitbucket) which supports .git projects.

Step 2

Creating a New Repository

Create a new repository with any option (private or public) and name your cocoapod repository. Leave remaining fields as it is except description because those fields will be going to add by the next step. For the demo, I created a repository named as my_own_cocoapod

The creation process is the same for both private and public but the distribution processes will differ. So, request you to select your option wisely.

Step 3

Open up the terminal for creating your own cocoapod with the following commands.

$ cd [DIRECTORY_PATH]
$ pod lib create [COCOAPOD_NAME]
  1. In the terminal, change to your respective directory to create your own cocoapod. For demo, cd Desktop/MY_OWN_COCOAPODS/
  2. Cocoapods has nice utility to create your own cocoapod with an example project along with testing framework as optional. This command will be prompted with an interactive script as following
platform? -> [iOS / macOS]
language? -> [Swift / ObjC]
include demo application? -> [Yes / No]
suggest testing framwork? -> [Quick / None]
view based testing? -> [Yes / No]

It will run pod install on the sample project w.r.t your options are given by the interactive script and open .xcworkspace of your sample project with Xcode.

For demo,

$ pod lib create my_own_cocoapodsiOS ⮑ Swift ⮑ Yes ⮑ None ⮑ No

Step 4

The workspace contains a sample project with your own cocoapod integration. It’s time to add some functionality by creating a new swift file where the default ReplaceMe.swift is located i.e Classes directory and delete the default file.

For demo,

my_own_cocoapod/Classes/PhoneNumberValidation.swift

After describing the functionality in your development pod, try to implement it in the sample project and check if there are any issues in order to go to the next step.

Step 5

Now move on to .podspec and it’s validation which includes metadata, source details, versioning, build settings and so on. Thanks to Cocoapod, It’s template already generated along with step 3 i.e Cocoapod creation. So no need to worry.

For more details about podspec click here.

Please refer following data which were used for the demo,

Pod::Spec.new do |s|
s.name = 'my_own_cocoapod'
s.version = '0.0.1'
s.summary = 'My Own Cocoapod'
s.description = 'Creating my own cocoapod for demo purpose.'
s.homepage = '[REPO_URL]
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Bharghav Kumar' => 'cbk2604@gmail.com' }
s.source = { :git => [REPO_URL], :tag => "#{s.version}" }
s.swift_version = '4.2'
s.source_files = 'my_own_cocoapod/**/*.{swift}'
s.ios.deployment_target = '9.0'
end

Now, open the terminal again and change the directory where the podspec file is located and then run the following command to validate its data.

$ pod lib lint [COCOAPOD_NAME].podspec

If everything is perfect, above command validate as given below in order to go to the next step

$ pod lib lint my_own_cocoapod.podspec-> my_own_cocoapod (0.0.1)my_own_cocoapod passed validation.

Please find my contact details at the end of this story. Feel free to contact me if any problems encountered for validating podspec because most of the people get stuck in this step.

Step 6

Assuming everything passes and moving to the next step that is setup remote for your cocoapod. It helps to make your cocoapod available for others through a source control management tool. The following commands should help to move forward

$ git init
$ git add --all
$ git commit -m 'Initial commit'
$ git remote add origin [REPO_URL]
$ git push -u origin master

REPO_URL is the repository URL generated or created in step 2.

Step 7

Tag the code to the repository by running the following commands using the terminal. Make sure that tag number is the same as the version which was mentioned in the podspec file.

$ git tag '0.0.1'
$ git push --tags

Cocoapod Distribution

Cocoapods can distribute privately as well as publicly.

Private Cocoapod Distribution

After the creation of cocoapod, it needs to push to your private spec repo which was created in the last part by running following commands from your podspec directory.

To avoid failure, run pod spec lint [COCOAPOD_NAME].podspec to validate that your spec is correct.

$ pod spec lint [COCOAPOD_NAME].podspec
$ cd [PODSPEC_FILE_PATH]
$ pod repo push [SPEC_REPO_NAME] [COCOAPOD_NAME].podspec

For the demo, the above commands look like below

$ pod spec lint my_own_cocoapod.podspec-> my_own_cocoapod (0.0.1)my_own_cocoapod passed validation.$ cd Desktop/MYOWNCOCOAPODS/my_own_cocoapod
$ pod repo push my_privateSpec my_own_cocoapod.podspec
Validating spec
-> my_own_cocoapod (0.0.1)
Updating the 'my_privateSpec' repo
Already up to date.
Adding the spec to the 'my_privateSpec' repo
- [Add] my_own_cocoapod (0.0.1)
Pushing the 'my_privateSpec' repo

Run the following command to check your cocoapod referenced in your spec repo

$ cd ~/.cocoapods/repos/[SPEC_REPO_NAME]/[COCOAPOD_NAME]
$ ls
0.0.1

Please jump into the next part of the series to know how to share it privately and how to use it by other team members.

Public Cocoapod Distribution

For getting started, need to register for trunk account with your email address to begin a session on your current device. For more details, read their blog post on why they made the trunk.

$ pod trunk register [EMAIL_ID] '[USER_NAME]'

Quickly, you get an email from cocoapods to verify your ‘session’ to make your pod publically available. Run the following command to push your pod to public

$ pod trunk push [COCOAPOD_NAME].podspec

For the demo,

$ pod trunk push my_own_cocoapod.podspecUpdating spec repo 'master'Validating podspec
-> my_own_cocoapod (0.0.1)
Updating spec repo 'master'--------------------------------------------------------------------
🎉 Congrats
🚀 my_own_cocoapod (0.0.1) successfully published
📅 February 22nd, 15:37
🌎 https://cocoapods.org/pods/my_own_cocoapod
👍 Tell your friends!
--------------------------------------------------------------------
Congrats! You did it

Hope this series of stories will help you to create your own cocoapod library if you have any troubles and suggestions please mention in the comments and reach me out on my mail (ID: cbk2604@gmail.com) as well. Good luck my friends will see you in the next part. Keep following me.

--

--