May 14, 2020

Create your own CocoaPods library.

By Mohit Agrawal
cocoa_feature

Let’s see how to create own custom CocoaPods library and publish it.

Introduction

If you are an iOS developer then you may have used CocoaPods in your projects. Today I will show how you can create your own CocoaPods library and publish it to https://cocoapods.org/. This is a great opportunity to showcase some of your good work by making reusable code and publishing it on CocoaPods.

In this tutorial, I will create a basic function to design a boundary to any image view. Then I will show you how to publish this function as a library in CocoaPods.

Prerequisites

  • Cocoa pods should be installed on your system. If not then run this pip install cocoapods.
  • Some basic concepts of git.
  • Basic knowledge of writing Unit Test cases.

Create the library and basic setup

You need to choose a unique and valid name for your library before you initialize the project. So let’s give a name to our library WImageHandler. If we search this library name on the cocoa pods website then we will not find any library as shown in the below image, so we can use this name.

cocoa home page

Now choose a suitable location on your system and run this command to create basic boiler plate for the library.

pod lib create WImageHandler

When you run the above command then you need to initialize some basic project setting in the terminal itself as shown in the below GIF. You can do as I have done it.

cocoa_install

If everything is good till here then it will automatically open the newly created library in the Xcode. Now open the WImageHandler.podspec file.

cocoa_boiler_plate

You need to make a few important changes to WImageHandler.podspec before pushing it to the GitHub. Give proper summary at line 12 and a brief description at line 21, also uncomment line 40 because we will be using ‘UIkit’. Edit the file as shown in the below GIF.

podspec

Till here we are done with the basic setup of the library. Now you need to push your code to the GitHub. So let’s see how to connect the library with the Github repo.

Setting up the GitHub

Go to GitHub and create a new repo with this name WImageHandler. Now again open WImageHandler.podspec and update s.homepage and s.source with your updated GitHub repo link which you have created just now. Mine is the same as the above GIF. This is an important change to be a little careful. Now it’s time to push the code. Open the terminal in the root folder which is WImageHandler and run these commands.

git init
git add *
git commit -m "Your commit message"
git remote add origin https://Your-git-url.git
git push -u origin master

Run all the above commands one by one and you will be able to push your code.

Let’s do some coding

Till here we were doing just setup only. Now it is time to code something in the library. So I will be showing you how to write an image extension which will give a nice border to any image view. Let’s start.

In Xcode expand Pods -> Development Pods -> WImageHandler. Here you will find a swift file Replaceme.swift. You just rename this file to WImageHandler.swift.
Note: You can also delete Replaceme.swift file and create a new one.
Use this code block for image extension.

import UIKit

extension UIImageView {
    public func applyBorder(color: UIColor, width: CGFloat) {
        layer.borderColor = color.cgColor
        layer.borderWidth = width
        clipsToBounds = true
    }
}
Image view extension

Great!!!. You have created the reusable library. Let’s use this library in the example project and check whether it is working or not. Open Main.storyboard and ViewController.swift in “Example from WImageHandler” folder. Make an image view and set any image to that. In ViewController import WImageHandler and try to access the image view extension which we have created. Run the code and you will get this as output.

cocoa final output
Gray border to image view

Push your code till here. We are ready to publish out WImageHandler library. Before that need to verify the pod spec file.

Final changes before pushing the library

Go to the root folder of your project and run this command to verify WImageHandler.podspec.

pod lib lint WImageHandler.podspec

Basically this command will verify the podspec file locally and will make sure that it is error free. If you run this command then we will get swift version warning. To fix this and one more line s.swift_version = ‘4.0’ at line 13. Refer the below image.

swift version cocoa

Now again run the above lint command, this time it will pass the validation.
Push the final code on the gitHub.

The second last step is creating a release on the GitHub repo. Open the repo which you have created there you will find a release tab. Click on it and create a release of version “0.1.0”. This version should be equal to the version given in your podspec file.

github release
GitHub release process

Last step, open the terminal in your root folder of the project and run this command to verify your email. You will get confirmation mail and you need to verify your email before publishing it.

pod trunk register abc@gmail.com "Mohit Agrawal"

After verifying your mail run the final command.

pod trunk push WImageHandler.podspec
Successful cocoapod
Successful cocoa pod terminal

Congratulations 🥳 , You have successfully pushed your library, now go and share with your friends.

Do you know the difference between value type and reference type?

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!