Codementor Events

Create your own cocoa-pod

Published Feb 12, 2020
Create your own cocoa-pod

Why?
Mainly because you want to do it, but then it has been proved to be the highly efficient approach when you know that this component you gonna uses again and again in most of your project. For example, a UITextview that provides placeholder and character limit ยน, or a view that can be panned to the directions and area you want it to. Later we'll create while going through a course of this post.


What do you need?

  • Xcode, we'll be developing this pod in Swift 5
  • Cocoapod configured in your system, here you can find how to do
    that
  • An Idea, what you think will qualify as a pod, and not already
    there for you to send a pull request and make updates that will add features you need, that way it will be a help for another user's too.
  • And a little bit of the will, don't give up, because by the end of this post you have your pod published on to cocoapods.org.

How we'll proceed?
You'll see. Let's Do it!


Step 1: Create a pod project and write your code.

  • First, thing create a folder in your mac and move to that folder in terminal

Tip: You can do that by typing cd folder/path of dragging your folder on the terminal after cd. Name it the same as your pod for future references. I will name it RPTInteractiveView. make sure to choose a unique name (it's a requirement) for your pod as well.

  • It will a good thing to remember pod lib this associated with lint and create is here to make your life easier.
  • Create a pod project using pod lib create. You can check the console log for that here.

pod lib create RPTInteractiveView

  • Now follow these automated steps, I have added my preferences you can go with yours:

What platform do you want to use?? [ iOS / macOS ]
>iOS
What language do you want to use?? [ Swift / ObjC ]
>Swift
Would you like to include a demo application with your library? [ Yes / No ]
>Yes
Which testing frameworks will you use? [ Quick / None ]
>None
Would you like to do view based testing? [ Yes / No ]
>No

  • Once the steps are done and processing completed, the project is created, if everything went right your project will be automatically get launched on Xcode, check for similar structure as:

  • Pod Project Structure

  • Now you can write the code for the idea that will help the fellow developers. I prefer writing that in my example cod, Why? Because if you write code on pod project then each time when you try to see the reflection of the code written you will have to clean and re-run the code.

  • Keep writing debugging and testing until you are content with the outcome. Once you feel you are ready you can move your code to pod file.

  • You can see in the above image, inside Pods->Development Pods->RPTInteractiveView.swift You can also notice an R next to it, that is because I renamed it (here name doesn't matter much) earlier it used to be ReplaceMe.swift and I did exactly what it requested for.

  • Now move your code here, (remove it from the file if that was inside example folder) clean project. Import your pod in the class where you were using it to test it, Run your project again and you should be able to see all that you created just the way you created it.

  • Feel so happy? Use it in another controller too. Go Crazy in testing. ๐Ÿ˜ƒ

  • Alright, you can use this as a development pod in any of your projects, Just add it to your pod file like

pod 'RPTInteractiveView', : path => '/path/to/my/pod/RPTInteractiveView'

  • it will be a local pod though and only the projects in the machine where this pod is created will have access to it.

Step 2: Create a remote repository

  • Now if you wanna share this pod and maintain versioning properly you need to push it to git.
  • Create a repo on git platform, name it the same as your pod (you know mine is RPTInteractiveView). It doesn't necessarily have to be on GitHub but I am using that. Since we'll be making our pod available globally, keep the repo public.

Step 3: The Metadata

  • Now that you have invested your time and efforts and love your pod, you are rather motivated for the next step, which we programmer often don't love much, updating the metadata. You gotta take care of all the files in Podspec Metadata folder the image above you can see that as the first group from the top, right below the project name.
  1. .podspec: This will be the only file that will be hosted on Podspecsยฒ, Hence this is a kinda important file to update. Also, it is a ruby file so, indentation better de right! It comes with a predefined structure so that is a big help. Just change all the values with relevant information.

    • Your pod project version Version and s.version must be the same
    • s.summary must be greater than the value given in s.description
    • add s.swift_version = '5.0', just below summary, to avoid some warning
    • s.homepage should be the path of your repository (https://github.com/rptwsthi/RPTInteractiveView)
    • s.license = { :type => 'MIT', :file => 'LICENSE' }
    • s.author= { 'Your Name' => 'your@email.com' }
    • s.source should look something like s.source = { :git => 'https://github.com/rptwsthi/RPTInteractiveView.git', :tag => s.version.to_s }. As you see :tag => s.version.to_s this is why we need to keep our tag version the same as the project version, so it remains symmetric in all 3 places(project version, podspec version and tag of the commit that we want to publish with this version).
    • You can leave the rest of the parameters as is.
    • Remember pod lib? Use it again on your terminal with lint to verify if your podspec is ready.

    $ pod lib lint

    • if you have followed instruction right you will see this sweet line RPTInteractiveView passed validation., in color greenยณ.
  2. README: This file is the difference between people using and not using your pod. This can be edited on Github you can do it locally. I prefer GitHub to do that interactively. Some stuff you should know while editing Readme:

    • To add screenshot (or gif) commit your screenshot with code and use git path of those images while adding the link
    • I use https://giphy.com to create nice .gif file to show the use interactive, you too can do that if you want
    • Must explain how to use it because although for you it is pretty straight forward for others, it might not be so.
  3. License: License file is here to spare you from liability, and inform users about liberty they have. You must add your license information in this file and save it. I used MIT you can use whatever you want.


Step 4: Push to remote repository

  • Add a tag to the latest commit of your branch, it must be the same as your pod projects version.
  • You can keep making an update in your pod and pushing to remote repo no problem, but every time you want to publish it to podspecs, update the version of your pod project create a tag and push that tag to the repo. Later we'll use this same value in our podspec file's s.version.
  • Push your code to this repo let's keep it on the branch master.

Step 5: Let's make it available for fellow programmers.

  • You have done all you needed to. Respect! Now it's time for the world to see what an amazing pod you have created. You will need to use the pod trunk.
  • If not already registered you have to register with pod trunk and as stated here

pod trunk register your@emai.com 'Your Name' --description='machine name (e.g. MacBook Pro)'
You must click a link in an email Trunk sends you to verify the connection between your Trunk account and the current computer. You can list your sessions by running pod trunk me.
Trunk accounts do not have passwords, only per-computer session tokens.

  • Now as you have registered, in the terminal inside your project folder run:

pod trunk push RPTInteractiveView.podspec

  • As you have followed the steps perfectly you must have got a response that says. You can ee full console log hereโด

๐ŸŽ‰ Congrats
๐Ÿš€ RPTInteractiveView (0.1.0) successfully published
๐Ÿ“… February 12th, 19:16
๐ŸŒŽ https://cocoapods.org/pods/RPTInteractiveView
๐Ÿ‘ Tell your friends!



Now what?

Now you wait, it can take up to 48 hours (happened to me) before your pod is published the first time. But go to https://github.com/CocoaPods/Specs/commits/master, and you will see something like this.

enter image description here

This is something that makes you happy and gives you the power to wait. You have done it, be proud of yourself. And get something awesome to eat.


Credits:

Discover and read more posts from Arpit Awasthi
get started
post commentsBe the first to share your opinion
Show more replies