If you ever stumble upon an idea that you might want to make into a library, and share it with the world via CocoaPods, this post will guide you to do so. We’ll go through series of steps to accomplish our goal. These steps will be quick, straight-forward, and easy to understand. So let’s get started!
Create a folder on your mac where you want to place your cocoaPod. Then open up terminal and locate to that folder directory. Let’s name that folder ‘ABCircularButton’.
In terminal, run command:
‘pod lib create ABCircularButton’
You can put any name instead of ABCircluarButton, as this will be the name of your pod. After you put in the command, you’ll be asked several question like so:
Answer the questions like done above, and once you are finished, a Xcode project will be generated for your library, where you can put your code for your pod. And as you’ve answered ‘Yes’ for the including of demo project question, you’ll also see an example project right above it. Right, so we are done with setting up the project.
In Xcode project you will see the following hierarchy :
You will put all your code for pod inside Development Pods / ABCircularButton / directory. Remove or replace the file ReplaceMe.swift with your own code file/s. Let’s say our Pod functionality is to round a UIButton, so our code will look some what like below:
One thing that you should remember is to make your class public, or else you wont be able to access that class.
Now that we are done with our code for the pod, lets try it in the example project, which has been generated automatically when we answered yes to the demo application question. Go to ViewController and import the pod like so:
If you get a warning like ‘No such module as ABCircularButton’, then clean the project and build again, the error will go away. Great! So now play around the code. For instance, create a UIButton and assign CircleButton as its class, and after setting its frame and constraints, run the project and you’ll see an orange coloured circular button. NAILED IT!!
Note: If you want to use your pod locally in other projects which are in your machine, then you can also do that easily. Just go into the podfile of the project where you want to use the pod, then inside the podfile, write pod name along with the path where your pod is located. It will look like this :
Once you’ve specified the path, just install the pod and you can easily use the pod locally on any of your project!
Great!! So now we are half way there. We have completed the coding for our pod and now all it is left to do is setup remote repo, setup our podspec file and push it.
Go to ABCircularButton.podspec file which is located just below CircularButton.swift in Pod folder. Open up the file and modify following specs.
s.summary (Summary of your pod) ,
s.swift_version (You have to add that manually below the s.summary, value should be swift version like ‘5.0’ ),
s.description (Description of your Pod ) ,
s.frameworks (Frameworks that your pod is using, like UIKit, MapKit ) ,
s.dependency (Dependency like Alamofire that your pod is using. In our case, it will remain commented)
Great! Now that you have modified the specs file, its time to validate those specs. To do so, we’ll run the following command:
pod lib lint
It will take around 30 to 40 seconds to validate the specs, and you are good to go once you see validation passed message in green colour, or else your validation will be failed along with the reason of failure and that will be in the form of warning. Resolve that warning and run the above command again.
Now that the validation is passed, its time to go to git hub and create a new project with the same name as your pod. Once that is done, commit and push your code to that repo.
Now, go to the release section in git hub and create a new release.
In tag version, give tag of ‘0.1.0’ as version, as this is our very first version and give a suitable title. Click on publish release. Now that is the first version of your pod.
After all your code is on remote repo, now its time to register your pod. To do so, run the following command in terminal :
pod trunk register email@example.com ‘Your Name’ — description=’Any Description you want!!’
After that, you need to go to the above email and click on the confirmation link. Once done, add one last command to the terminal :
pod trunk push ABCircularButton.podspec
You’ ll see a congrats message stating that your pod has been published. Voila!!!
Now it’ll take some time before it will be available for anybody to use it. In my case it took around 10 minutes but that can vary.