How to create your own Pod?
Developing an iOS application is a lengthy and hard process. While developing we actually write plenty of lines code and different-different module. Many of the modules actually are repetitive and required in every project like opening the camera, selecting photos, etc. So it’s good to have that class/module with us every time. But keeping this module somewhere where it can be available easily.
The pod is one of the best ways of storing our module in safe hands and easily available. So here we will see how can we create the pod files.
Prerequisites:
*You should know about creating a single view application.
*Git account
*Knowledge of git commands like push, pull, etc.
Module Description
I am creating a module name ImagePicker which helps to open the camera and capture photos, selecting photos from the gallery.
I have created the Single View application PodDemo.
Add your module to a single view application. In my case, I am adding ImagePicker in PodDemo.
Now we all done with the basic setup to create our Cocoapod.
Uploading your Cocoapods to GitLab
- First login to your account at GitLab
- Create a new repository by clicking + top right corner and selecting New repository. Name it and select other options based on your requirements.
- Now add this repository to your local project with git commands
1. git init
2. git add .
3. git commit -m “first commit”
4. git branch -M main
5. git remote add origin <your project URL>
6. git push -u origin main - Now all your code is at GitLab
Creating .podspec file
What is podspec file?
podspec file is file which include all required information about the pod file we are going to create like name of pod, version of pod, address of git repository, also we can add summary of the pod file and dependescies which required for the module
Additionally here is the detail and how can we write podspec file.
Following is the way of creating podspec file.
- Open terminal
- Navigate to your project directory.
- Run the following command :
touch MBImagePicker.podspec - Now you can see MBImagePicker.podspec file in the project directory
- Open file and copy-paste following snippet and change accordingly.
<script src=”https://gist.github.com/iYuvraj-kale/58de077ee11101174a73261d458641f3.js"></script>
6. Now you can push all your changes to GitLab
currently, we are done with the local setup of creating podspec file and all, now we need to release the pod file for that we need to release the version.
For that follow the next step
- Click on create new release under the Release button
2. Set the version given in podspec file for us it’s 0.1.0 then set title and description and Publish release.
Now Linting the project
CocoaPods needs to verify that nothing is wrong with a project. This spans from limitations and requirements to errors and even suspicious code. CocoaPods requires you to lint your project.
Following are the command for that
- pod lib lint
if you see the warnings after the command then you can change this with following
pod lib lint — allow-warning - Now register the user in the trunk. For that following is the command
pod trunk register <emailid> ‘<Name>’ — description=’<decription>’ - Now push our pod file for release
pod trunk push yourpodspecfile.podspec — allow-warnings
OR
pod trunk push yourpodspecfile.podspec
Yehhhhh!!!!!!!! we are done here for creating our pod file named MBImagePicker. Now wait for 15–20 min to check your pod is available or not
Conclusion
This is how we can actually create our own pod and can be integrated using including it in the pod file and running command pod install. We can actually make changes in podspec file according to our requirements. For that, you can go through cocoapod podspec file documentation