Create your own CocoaPods library

Mohammad Jeeshan
6 min readAug 4, 2022

--

Let’s learn how to create your own Cocoapods library from scratch and make it live for public use, just like other libraries.

Introduction

You’re probably familiar with some well-known, open-source CocoaPods libraries like Alamofire or Kingfisher. But sometimes, you don’t find a pod with the exact functionality you need, or you may want to separate a large project into smaller, reusable components. With the help of Cocoapods, you can create your own libraries. It can be private or public, depending on your preference. You can also make it live for the public so that other users also get the benefits of your library.

Today in this blog, I will show you how to create a Cocoapods library & make it live for public use, just like other open-source CocoaPods libraries did.

Before moving further, Let’s understand what CocoaPods is. If you are already familiar with CocoaPods, which is excellent, but if you are new to iOS, then it will be new for you, So Let’s understand:

CocoaPods is a dependency manager for Swift and Objective-C projects. It has over 95 thousand libraries and is used in over 3 million apps. You can also check out this link for more information.

Important Note: In this blog, I will create an open-source CocoaPods library for all standard social logins. With the help of this single library, you can integrate Apple, Facebook, and Google logins into your project, so here I’m not going to explain the code, but you take the reference of it.

Overview

There are many different ways to create a pod. The most common one is to follow CocoaPods' official document. Here I will guide you step by step. So without wasting any more time, Let's start.

Step-1 (Create a pod project)

First, open your mac terminal & use the following command to bootstrap an Xcode workspace to create a new pod project:

pod lib create [Pod Name].        Ex- pod lib create MjSocialLogins

After running this command, it will ask you for some basic configuration details like email, platform, language, etc.

You can change the configuration details as per your library requirements.

After giving the basic configuration details for your CocoaPods library, it will create a sample pod project and automatically open the .xcworkspace file. If it doesn’t open the .xcworkspace file automatically, in that case, you can do it manually. Finally, your new pod project will look like this.

Step-2 (Configure podspec file)

This new project contains a podspec file, which includes all configuration details of your CocoaPods library. You need to edit auto-generated podspec file data as per your library requirement.

You can take the reference of the above image for your configuration. Here I want to add some important points, that you should remember during podspec file configuration.

  • Your long description should be larger than your short description.
  • Your library version should be correct. If it's your first version, it should be in this format (major.minor.patch). Ex- 0.1.0
  • The homepage URL should be the URL of your Git repo where you want to host it. If you do not create any repo for your library on GitHub, then first create it, but make sure the repo name should be the same as your library name. (It’s a good approach)
Here you can see my GitHub repo URL.
  • The source URL should be the URL of your repo source code. If you want to manage branches for your library, in that case, you need to pass the URL of a specific branch.
  • You can set the development target and library source files directory path as per your library requirements.

What happens if you want to use some other open-source library in your new CocoaPods library?

In my case, my CocoaPods library (MjSocialLogins) needs to integrate FacebookLogin and GoogleSignIn SDK. So in your case also it can be possible to integrate some other libraries. CocoaPods also fulfils this requirement. You can add your library dependencies. Example-

s.dependency 'GoogleSignIn'
s.dependency 'FBSDKLoginKit/Swift'
You can take the reference of the above image.

Step-3 (Create/add swift files)

Now add all your swift files to your source directory path, which you define within your Podspec file. Make sure the path should be correct; otherwise, your library will not work.

Step-4 (Add an example)

Now time to test our newly created CocoaPods library. We can test our new library locally with the help of an example. It can be a better option for other developments if you add an example in the library project, so make sure to add a sample within your library.

https://github.com/MjCodingCamp/MjSocialLogins

Step-5 (Write unit test cases)

Unit testing is crucial for making sure your library is robust. We should write some unit test cases for our library, and here, I will explain how to write unit test cases for your project. You can check out this link.

Step-6 (Test library configurations)

With the help of lint, we can test our newly created library configuration. Basically, Linting is the process by which your pod is validated to ensure it can be consumed by others before it gets published. This includes running tests and building for RELEASE configuration.

pod lib lint [Library_Name].podspec

After running this command, it will validate your newly created library, and it's super important before publishing your library.

Note: If you get any validation error, you must resolve it before publishing.

Step-7 (Create release tag)

Now time to make your library for public use. For this first push project files on the git remote repo, you next need to create a Tag release for this repo, So go ahead and click on releases. I am mentioning some images just for your reference.

Step-8 (Deploy)

Now all is good to go to deploy the library on the CocoaPods server. First, you have to register your email id via the trunk. Hit the following command:

pod trunk register ‘Your email ID’ ‘name’

After hitting the command on the terminal, you will get a verification email from CocoaPods to verify your email address. Open your mail and verify it in the current session.

After verifying the email address, push your library to CocoaPods via the trunk. Hit the following command:

pod trunk push [Library_Name].podspec

The process takes a few seconds or minutes. Finally, you will see a successful message from CocoaPods.

🏆 Congratulations :)

Your open-source library has been lived for public use.

Now time to celebrate :)

Thank you so much for reading this article. If you have any queries regarding this blog, please comment below.

Upcoming blogs

1- How to create a private or public Swift Package Manager?
2- How to provide the support of SPM within the existing CocoaPods library?

--

--

Mohammad Jeeshan
Mohammad Jeeshan

Written by Mohammad Jeeshan

iOS/Flutter developer, Freelancer, Vlogger & Technology Lover…

No responses yet