Create your own CocoaPod – Swift / Objective-C

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over ten thousand libraries and can help you scale your projects elegantly.

CocoaPods Logo
CocoaPods Logo

Lets create a CocoaPod

Open your terminal and follow the steps:

  1. Update your system
  2. Update your system with latest ruby gems by installing ruby gems

    sudo gem update --system
    

  3. Install CocoaPods in your system
  4. Install CocoaPods by using following command :

    sudo gem install cocoapods
    

    If you are facing issue in EL Capitan then use following command :

    sudo gem install -n /usr/local/bin cocoapods
    

  5. Create a project for CocoaPod
  6. pod lib create command will help you to create a directory structure for the pod with all the files which need to create the pod.

    For Example :

    pod lib create AKGitViewControllers
    

    It will ask following questions :

    What language do you want to use?? [ ObjC / Swift ]
    > ObjC

    Would you like to include a demo application with your library? [ Yes / No ]
    > Yes

    Which testing frameworks will you use? [ Specta / Kiwi / None ]
    > None

    Would you like to do view based testing? [ Yes / No ]
    > No

    What is your class prefix?
    > AK

    Answer it with based on your preference.

    After all process done open the workspace of project. The structure should be as follows :

    Folder Structure
    Folder Structure

  7. Updating your pod spec
  8. pod lib lint command is used to validate the pod spec file.

    $ pod lib lint AKGitViewControllers.podspec 
    
     -> AKGitViewControllers (0.1.0)
        - WARN  | summary: The summary is not meaningful.
        - ERROR | description: The description is empty.
        - WARN  | url: There was a problem validating the URL https://github.com/<GITHUB_USERNAME>/AKGitViewControllers.
    
    [!] AKGitViewControllers did not pass validation, due to 1 error and 2 warnings.
    You can use the <code>--no-clean</code> option to inspect any issue.
    

    They will display the warnings and errors. Based on it we have to update our pod spec file with the some informations.

    Main points are summary, description, source and url.

    For Example:

      s.summary          = "Testing a git with cocoapods - AKGitViewControllers.”
    
      s.description      = "This is just a testing a git with cocoapods - AKGitViewControllers."
    
      s.homepage         = "https://github.com/ashishkakkad8/AKGitViewControllers"
    
      s.source           = { :git => "https://github.com/ashishkakkad8/AKGitViewControllers.git", :tag => s.version.to_s }
    

    Note : For the GitHub link upload the project on GitHub by using command line tools or by using the GitHub desktop.

    Create a repository / Commit and Push the project to GitHub.

    Validate again with the pod lib lint :

    $ pod lib lint AKGitViewControllers.podspec 
    
     -> AKGitViewControllers (0.1.0)
    
    AKGitViewControllers passed validation.
    

  9. Add code to project
  10. Lets add some functionality to the pods and make it batter.

    In your pods folder there is a file available with the project (ReplaceMe.m/ReplaceMe.swift) It will be a empty file.

    At following location you will find that file :

    Pod Replace Me File Location
    Pod Replace Me File Location

    Delete that ReplaceMe file and add your own code there with different files {.h|.m} or {.swift}

    Objective-C Sample Code :

    //  AKGitViewControllers.h
    #import <UIKit/UIKit.h>
    
    @interface AKGitViewControllers : UIViewController
    
    - (void)placeALabelOnVC;
    
    @end
    
    //  AKGitViewControllers.m
    #import "AKGitViewControllers.h"
    
    @implementation AKGitViewControllers
    
    - (void)viewDidLoad {
        [self placeALabelOnVC];
    }
    
    - (void)placeALabelOnVC{
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, 20)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = @"This is a testing of pods";
        [self.view addSubview:lbl];
    }
    
    @end
    

    It will just an wrapper of the UIViewController which will add one label in the UIViewController “This is a testing of pods”.

    Use it with the example in that project:

    @import UIKit;
    
    #import <AKGitViewControllers/AKGitViewControllers.h>
    
    @interface AKViewController : AKGitViewControllers
    
    @end
    

    If any issues there (like file not found from the pod) then use the pod install command to update the code with that example project. because we have added some files in pod.

    Test your example is working or not.

  11. Make your Pod Available in Public
  12. Push your code to git again.

    – Tagging:

    Tag your most recent commit and push it to the remote.

    $ git tag 0.1.0
    $ git push origin 0.1.0
    Username for 'https://github.com': ashishkakkad8
    Password for 'https://ashishkakkad8@github.com': 
    Total 0 (delta 0), reused 0 (delta 0)
    To https://github.com/ashishkakkad8/AKGitViewControllers.git
     * [new tag]         0.1.0 -> 0.1.0
    

    This step indicates that you are marking this commit as a specific release of your pod. The name of the tag should match s.version in your .podspec file. The next step will validate this.

    Validate again with pod spec lint.

    – Pushing to specs repository:

    pod trunk push will be used for push the Specs.

    $ pod trunk push AKGitViewControllers.podspec 
    [!] You need to register a session first.
    

    To Register for cocoa pods:

    $ pod trunk register ashishkakkad8@gmail.com 'ashishkakkad8' --description='Mac mini'
    [!] Please verify the session by clicking the link in the verification email that has been sent to ashishkakkad8@gmail.com
    

    Again try after registration:

    $ pod trunk push AKGitViewControllers.podspec --verbose
    
    Updating spec repo <code>master</code>
      - Log messages:
        - January 11th, 13:29: Push for <code>AKGitViewControllers 0.1.0' initiated.
        - January 11th, 13:29: Push for </code>AKGitViewControllers 0.1.0' has been pushed (1.128956505 s).
    

Now you can use it in pod file anywhere.

UPDATE

How to add sub dependencies to the cocoa pod?

Suppose your pod requires to use of another pods then we can add the dependency to the current pod spec with the list of dependency.

  1. Open your pod spec file
  2. Find the commented s.dependency
  3. Add dependency as you want
  4. Pod::Spec.new do |s|
      s.dependency 'AFNetworking', '~> 3.0'
      s.dependency 'MBProgressHUD', '~> 0.9.2'
    end
    

  5. Update the pod trunk with the newly added sub dependency
  6. If you push directly then you will get the message : duplicate entry

    $ pod trunk push AKSamplePod.podspec 
    Updating spec repo <code>master</code>
    
    CocoaPods 1.0.0.beta.2 is available.
    To update use: <code>gem install cocoapods --pre</code>
    [!] This is a test version we'd love you to try.
    
    For more information see http://blog.cocoapods.org
    and the CHANGELOG for this version http://git.io/BaH8pQ.
    
    Validating podspec
     -&gt; AKSamplePod (0.1.0)
    
    [!] Unable to accept duplicate entry for: AKSamplePod (0.1.0)
    

    So you have to update the version in pod spec

    Pod::Spec.new do |s|
      s.version          = "0.1.1"
    end
    

    And again create a new tag with new version

    $ git tag 0.1.1
    $ git push origin 0.1.1
    Total 0 (delta 0), reused 0 (delta 0)
    To https://github.com/ashishkakkad8/AKSamplePod.git
     * [new tag]         0.1.1 -> 0.1.1
    

    Then again try with pod trunk push to release the version in public

    $ pod trunk push AKSamplePod.podspec 
    Updating spec repo <code>master</code>
    
    CocoaPods 1.0.0.beta.2 is available.
    To update use: <code>gem install cocoapods --pre</code>
    [!] This is a test version we'd love you to try.
    
    For more information see http://blog.cocoapods.org
    and the CHANGELOG for this version http://git.io/BaH8pQ.
    
    Validating podspec
     -&gt; AKSamplePod (0.1.1)
    
    Updating spec repo <code>master</code>
    
    CocoaPods 1.0.0.beta.2 is available.
    To update use: <code>gem install cocoapods --pre</code>
    [!] This is a test version we'd love you to try.
    
    For more information see http://blog.cocoapods.org
    and the CHANGELOG for this version http://git.io/BaH8pQ.
    
      - Log messages:
        - January 25th, 18:29: Push for <code>AKSamplePod 0.1.1' initiated.
        - January 25th, 18:29: Push for </code>AKSamplePod 0.1.1' has been pushed
        (2.025618487 s).
    

    Note : If you are getting error in the pod trunk push then you can check in more effective logs by using its command (–verbose) it will display more debugging information.

    $ pod trunk push AKSamplePod.podspec --verbose
    
COCOAPOD
COCOAPOD

Happy Coding 🙂

  • Novak Miha

    Do we have to create a private repository to make that
    “pod lib lint AKGitViewController.podspec” work ?

    It gives such an error
    No podspec exists at path AKGitViewController.podspec

    • You have to create yours. This is a sample. You can’t change another user’s podspec.

      Thanks for visiting website.

      • Novak Miha

        where to write those lines ?
        $ git tag 0.1.0
        $ git push origin 0.1.0

  • Novak Miha

    Source code for your Pod was not accessible to CocoaPods Trunk. Is it a private repo or behind a username/password on http?

    when i run pod trunk push then it gives that error. What is the reason ?

    • If you are getting my point then you have to create your own pod. You can not update my pod.

      Yesterday I already told you. But you are not getting.

      • Novak Miha

        i created my own repository. But the error was about privacy setting of the repository. So i made it public and then i could push it. Thank you for your help.

        • Yes. Cocoapods will available to the all users not just one user. So, repository must be public.

          Welcome.

        • 刘宁

          how did u made that public?i use SSH for github ,is this means my codes private?

  • Novak Miha

    is it possible to import a framework that i create into this project ?

    • Yes. You can install that pod in any of the project.

  • Abhishek Thapli Thapliyal

    Can you tell me to setup cocoapods for existing xcode project ?

  • Tal Zion

    I am getting this error, any idea why? I did not change the name

    (0.1.0)
    - ERROR | name: The name of the spec should match the name of the file.

    [!] TZPod did not pass validation, due to 1 error.
    You can use the
    –no-clean option to inspect any issue.

    Great tutorial, by the way.

  • Tareyev Gregory

    The most helpful article about creating own Pod in the internet!
    Thank You a lot!

    • Tareyev Gregory

      Keep it up 🙂