How to build your own CocoaPod Library for iOS — Part 1

Phan Quang Hoàng
6 min readJun 10, 2019

--

If you are a developer iOS, I firmly believe that you are no stranger to CocoaPods and you can even use it every single day. But at some point, you were wondering how to release a great CocoaPod library for others who can use it, or you just want to reuse the feature for your future projects without you having to copy and paste the old code.

In case you want to learn more about CocoaPod you can find out more at the homepage

So in this tutorial, I only guide you how to release a CocoaPod library for iOS. Let’s start now.

First of all, we need to have an account in github to create own repository. After you logged success into github, let create your repository. For example:

Then you can clone the repository to your machine and create the library in this repo

Now we will create Cocoa Touch Framework and fill some information for the project as the following:

Click next and select github folder that you have cloned before. Now we can double check the github folder again to make sure the project was stored in right place.

Now we will create Calculator class with the simple function to add two integer number together.

Noticed that you will see open keyword in this class and function. That mean you public class and function to everywhere can see and use it. Otherwise, when you import to iOS project, it will be hidden.

We will further to create CalculatorTests class to test our function to make it work well.

In Test class, every function you want to run test case, it must has test prefix for each function. let run Test class, you can see the check green that mean your function work well.

When you create a pod library, you also want to check it work well in iOS project. So we will create CalculatorExample project to import our library.

From Xcode select File -> New -> Target. Select iOS Target to create Single View App for iOS.

After you finish this step, you will see in the project appear two schemas, one for library and one for iOS project.

We will try to import our library to iOS project by cocoaPod to run the function in the library.

If you still not installed cocoaPod, you should take time to read their document at CocoaPod website to install it. I assume that you installed success CocoaPod by the scope of this tutorial, so I will move to next step!

Now, we will open terminal and move to our folder to create a Pod file as the following:

Now you will see the Podfile appear in your folder:

Let open it to add our library:

target 'CalcutatorExample' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for CalcutatorExample
pod 'HPCalculator', :path => '../HPCalculator'
end

Then you try to install pod with command:

pod install

You will see the error because you not define the podspec for library. This file declare what attributes for cocoapod set up the library. We will create it right now!.

pod spec create HPCalculator

After that you will HPCalculator.podspec file is created in your folder. Open this file you will see a template with a lot of lines. To learn more about podspec syntax you should visit here to explore somethings. Right now, you just describe for your library as the following:

Pod::Spec.new do |spec|spec.name         = "HPCalculator"
spec.version = "0.0.1"
spec.summary = "The example make a iOS library"
spec.description = "The demo how to release a iOS library to CocoaPods"
spec.homepage = "The url link to your repository" # Example: https://github.com/username/your_name_of_repository
spec.license = { :type => "MIT", :file => "LICENSE" }
spec.author = { "Your name" => "Your email" }
spec.platform = :ios, "9.0"
spec.source = { :git => "Your repository", :tag => "#{spec.version}" } # Example: https://github.com/username/your_name_of_repository.git
spec.source_files = "HPCalculator/**/*.{swift}"
spec.swift_version = "5"
end

When you filled some information in this spec, we will install our pod library:

pod install

Now, let’s open *.xcworkspace file, you can see our library has been installed.

We will move to Example project to import the library and try to call our function:

Okay, the library can work in iOS project right now. Perfect!

We will go ahead to public the library to CocoaPod for everyone can easy install the library. Sound good!

Before you want to release to CocoaPod, you must push code to github then CocoaPod can fetch all code from your repository.

git add .
git commit -m "Complete the library"
git push origin master

Then you must add tag for repository to CocoaPod can fetch exactly the version of code. Remember tag in repository must same spec version in podspec file. Example:

spec.version      = "0.0.1"

Now, we will add tag 0.0.1 to repository:

git tag 0.0.1
git push origin --tags

To make sure the podspec file is valid, we need to verify it, otherwise we can not release it.

pod spec lint

After this command, if everything is good, you will see the notification is the podspec file passed validation.

We can push the library to CocoaPod right now.

pod trunk push

CocoaPod will take a few time to push the resource. Don’t hurry!. After this processing is success, you can see in terminal:

Copy the url and paster to browser to see the our result:

Okay, from now, any iOS developers can install your library from CocoaPod or you can reuse your code for future projects. Look great!

In next tutorial, I will guide you how to setup CI for our library to make sure your code don’t break when you update the library.

To learn about CI/CD for iOS project, you can take a look at previous tutorial to have a bit knowledge before we move to config for our library.

Preferences:

--

--

Phan Quang Hoàng
Phan Quang Hoàng

No responses yet