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
    [code language=”bash”]
    sudo gem update –system
    [/code]

  3. Install CocoaPods in your system
  4. Install CocoaPods by using following command :
    [code language=”bash”]
    sudo gem install cocoapods
    [/code]
    If you are facing issue in EL Capitan then use following command :
    [code language=”bash”]
    sudo gem install -n /usr/local/bin cocoapods
    [/code]

  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 :
    [code language=”bash”]
    pod lib create AKGitViewControllers
    [/code]
    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.
    [code language=”bash”]
    $ 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 --no-clean option to inspect any issue.
    [/code]
    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:
    [code language=”text”]
    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 }
    [/code]
    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 :
    [code language=”bash”]
    $ pod lib lint AKGitViewControllers.podspec
    -> AKGitViewControllers (0.1.0)
    AKGitViewControllers passed validation.
    [/code]

  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 :
    [code language=”obj-c”]
    // 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
    [/code]
    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:
    [code language=”obj-c”]
    @import UIKit;
    #import <AKGitViewControllers/AKGitViewControllers.h>
    @interface AKViewController : AKGitViewControllers
    @end
    [/code]
    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.
    [code language=”bash”]
    $ 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
    [/code]
    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.
    [code language=”bash”]
    $ pod trunk push AKGitViewControllers.podspec
    [!] You need to register a session first.
    [/code]
    To Register for cocoa pods:
    [code language=”bash”]
    $ 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
    [/code]
    Again try after registration:
    [code language=”bash”]
    $ pod trunk push AKGitViewControllers.podspec –verbose
    Updating spec repo master
    – Log messages:
    – January 11th, 13:29: Push for AKGitViewControllers 0.1.0' initiated.
    - January 11th, 13:29: Push for
    AKGitViewControllers 0.1.0′ has been pushed (1.128956505 s).
    [/code]

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. [code language=”bash”]
    Pod::Spec.new do |s|
    s.dependency ‘AFNetworking’, ‘~> 3.0’
    s.dependency ‘MBProgressHUD’, ‘~> 0.9.2’
    end
    [/code]

  5. Update the pod trunk with the newly added sub dependency
  6. If you push directly then you will get the message : duplicate entry
    [code language=”bash”]
    $ pod trunk push AKSamplePod.podspec
    Updating spec repo master
    CocoaPods 1.0.0.beta.2 is available.
    To update use: gem install cocoapods --pre
    [!] 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
    -> AKSamplePod (0.1.0)
    [!] Unable to accept duplicate entry for: AKSamplePod (0.1.0)
    [/code]
    So you have to update the version in pod spec
    [code language=”bash”]
    Pod::Spec.new do |s|
    s.version = "0.1.1"
    end
    [/code]
    And again create a new tag with new version
    [code language=”bash”]
    $ 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
    [/code]
    Then again try with pod trunk push to release the version in public
    [code language=”bash”]
    $ pod trunk push AKSamplePod.podspec
    Updating spec repo master
    CocoaPods 1.0.0.beta.2 is available.
    To update use: gem install cocoapods --pre
    [!] 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
    -> AKSamplePod (0.1.1)
    Updating spec repo master
    CocoaPods 1.0.0.beta.2 is available.
    To update use: gem install cocoapods --pre
    [!] 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 AKSamplePod 0.1.1' initiated.
    - January 25th, 18:29: Push for
    AKSamplePod 0.1.1′ has been pushed
    (2.025618487 s).
    [/code]
    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.
    [code language=”bash”]
    $ pod trunk push AKSamplePod.podspec –verbose
    [/code]

COCOAPOD
COCOAPOD

Happy Coding 🙂