How to Create a CocoadPods Library?

Building your own CocoaPods library in Swift involves several steps. Here’s a guide to help you get started:

  1. Set Up Your Development Environment.
  2. Create Your Swift Project.
  3. Write Your Library Code.
  4. Create a Podspec File.
  5. Push Your Code to GitHub.
  6. Validate Your Podspec.
  7. Publish Your Library.
  8. Using Your Library.

Make sure you have the necessary tools installed:

  • Xcode
  • CocoaPods

First, navigate to the desired directory where you want to create your custom Pod. Once you have selected the folder, open the terminal within that directory.
To create a new project for Pod creation, run the following command:

pod lib create [POD_NAME]

After that, proceed with configuring your Pod as necessary.
A.
 What platform do you want to use?? [ iOS / macOS ]
iOS
B. What language do you want to use?? [ Swift / ObjC ]
Swift
C. Would you like to include a demo application with your library? [ Yes / No ]
Yes
D. Which testing frameworks will you use? [ Quick / None ]
None
E. Would you like to do view based testing? [ Yes / No ]
No

Once the setup is complete, it redirect to the project within XCode. If the directory is not already open, open it manually.

Set the default pod template setting to the minimum version:

  • iOS deployment target: 12.0
  • Swift version: Swift5
  • Project Format: Xcode 12-compatible

In addition, reflect this updated configuration in both Xcode and the relevant files.

The .podspec file defines your library and its dependencies. Here’s a basic example:

Pod::Spec.new do |s|
  s.name         = 'YourLibraryName'
  s.version      = '0.1.0'
  s.summary      = 'A short description of YourLibraryName.'
  s.description  = <<-DESC
                   A longer description of YourLibraryName.
                   DESC
  s.homepage     = 'http://yourlibraryhomepage.com'
  s.license      = { :type => 'MIT', :file => 'LICENSE' }
  s.author       = { 'Your Name' => 'your.email@example.com' }
  s.source       = { :git => 'https://github.com/username/YourLibraryName.git', :tag => s.version.to_s }
  s.ios.deployment_target = '10.0'
  s.swift_version = '5.0'
  
  s.source_files = 'Sources/**/*.{swift,h,m}'
  s.public_header_files = 'Sources/**/*.h'
  
  s.dependency 'SomeOtherPod', '~> 1.0'
end

Creating a Codebase Directory

Delete the ReplaceMe.swift file and Create Classes folder in the directory:
Pods project > Development Pods group > [POD_NAME] group

Create a file with the same name as the Pod within the Classes folder and use it to write custom code for the Pod.
example: <POD_NAME>.swift
You can also create multiple folders and files in order to write personalize code.

Refer to the screenshot for clarification:

Push Your Code to GitHub

Now, We are at the stage where you can easily share your iOS development work with community by publishing personalize Pod to CocoaPods.

  1. Initialize a Git repository in your project folder if you haven’t already:
git init
git remote add origin https://github.com/username/YourLibraryName.git
git add .
git commit -m "Initial commit"
git push -u origin master

2. Create a tag for your version:

git tag '0.1.0'
git push --tags

Publish Your Library:

  1. Register your pod with the CocoaPods trunk (if you haven’t already):
    • pod trunk register joynalcu7@gmail.com 'Joynal Abedin' --description='Macbook Air'
    • pod trunk me
  2. Validate Pod using lint
    • pod lib lint <POD_NAME.podspec>
    • pod lib lint <POD_NAME.podspec> --allow-warnings
  3. Deploying a Library
    • pod trunk push [POD_NAME.podspec]

126 thoughts on “How to Create a CocoadPods Library?

Leave a Reply

Your email address will not be published. Required fields are marked *