Deploying a Custom Library via CocoaPods
Deploying a custom library is essential for developers sharing their code. Methods like Swift Package Manager (SPM), Carthage, and CocoaPods exist for this purpose. CocoaPods is favored due to its vast library repository, ease of use, and strong community support. While SPM integrates seamlessly with Xcode, it lacks CocoaPods' extensive library ecosystem. Carthage prioritizes simplicity but requires more manual maintenance. CocoaPods' comprehensive library support, automated integration, and handling of complex dependency graphs make it the preferred choice, ensuring projects stay consistent and up-to-date. This guide will cover the steps to deploy a custom library using CocoaPods.
Creating a Library Locally
To create a library locally, follow the steps listed below:
Navigate to the directory in your device where you want to create the project locally
Open terminal in the directory and type in the below command followed by your chosen repository name (without the angle brackets), then hit the return key.
pod lib create <LIBRARY-NAME>
For Example:
(Tip: Refer CocoaPods Website and name your project uniquely)
(Now type in your preferred options from this project initializer template as per your needs)
Open the project in XCode
( If you have included a demo application with your project, then follow the instructions given in the terminal and open the ProjectName.xcworkspace file inside the Example directory in the Project Directory -> e.g " PROJECT-NAME/Example/ProjectName.xcworkspace ".
-- OR --
If you have not included an example project, then just navigate to the folder using terminal => " PROJECT-NAME > PROJECT-NAME > Classes ". Then, delete the "ReplaceMe.swift" file and create a new file inside it with your desired name. Now, open it in XCode using the command
$ xed <FILE-NAME>
or by dragging and dropping the file in the Xcode Window )Write your code in the opened file.
This is the code that you want to be added to any project which uses your library. ( Say, in SwiftUI you have created a Rounded Rectangular frame with some gradient and you feel that this can also be useful for other SwiftUI developers. Instead of providing the code snippet of the Rounded Rectangle, you create a Custom View which takes in the width of border and gradient colors, embed it in your own Custom Library and then publish it on CocoaPods. Now, the other developers who want to use it just have to add your library to their project and use the Custom View directly without writing any separate code for it. )
For Example:
(Save the edited file and now that the file is saved and the library created, it's time to push the code to GitHub)
Uploading the Library to GitHub
Initialize Git on the Library
Go back to the terminal and back to the parent directory i.e DemoLibrary which we created using pod lib create. Check if the folder is already initialized with git.
(To check for it, enter the command in terminal
$ ls -a
. This command will reveal all the hidden files inside the directory, and there check for a directory named .git ; If the directory is there, the well and good else we need to initialize our parent directory with git)(If the .git is not there, then use the command
$ git init
to initialize the directory with git)Now commit the files to git
Use the command
$ git add .
to add all the files to the staging areaNow, use the command
$ git commit -m "Your commit message"
to commit the changes.
(All the files have been added to git, now it's time to put it on GitHub)
Push the code to GitHub
(Now, there are two ways to go about it: one is using the terminal for all tasks, where we first create a repository on GitHub, then set the origin of our local repository to point to the newly created GitHub repository)
OR
(We use the GitHub Desktop App with a nice GUI for simplicity and convenience. Here, I will use the GitHub Desktop Client for the purpose.)
So, after installing the GitHub Desktop Client, open it and click on
Add
option on the top-left corner of the screen. Now, chooseLocal Repository
After this, a dialog box will appear where you need to locate the local repo. In that, select the parent directory which was created after pod lib create and click on open.
Now, the repository has been added to the GitHub Desktop. Then, click on the publish repository button on the top of GitHub Desktop. Fill in the trivial details in the dialog box which appears after that and click on the publish repository button.
Registering the Pod with CocoaPods
Validating the Podspec File
Open the LibraryName.podspec file in the parent directory and adjust the details as per the library.
Now, run the command
$ pod lib lint LibraryName.podspec
to validate it and if there are any errors, debug them, else move aheadVersioning the Library
Now, it's time to put a version tag to the library. For that open terminal and locate to the parent directory of the library, add and then push the tag using the code lines
$ git tag version
(eggit tag 0.1
) and then push the tag to GitHub using the lines$ git push —-tags
. Then, a version tag will also be available in GitHub Desktop App history. (The GitHub Desktop also has the functionality to add a tag on a commit. Prefer the method based on convenience.)(Note: The version tag in GitHub should be the same as in your .podspec file)
Deploying to CocoaPods
Now, run the command
$ pod trunk register YourEmailID
. Then verify the mail you receive on this e-mail ID.Now, push your library to the public access channel (trunk) of CocoaPods with this command
$ pod trunk push LibraryName.podspec
(Tip: Check CocoaPods to verify that no other repository with the same name as your exist else you won't be able to push to the trunk since all the libraries are supposed to be having unique names)
After one or two minutes, the validation will be complete and your library will be uploaded to CocoaPods and can be used by all the developers worldwide. Cheers to that !!
Upgrading the Pods
Make improvements and additions to the library and push
Make the changes to the library
Upgrade the version in the podspec file
Push the code to GitHub with the new tags with same version identifier as in the podspec file
Push the code upgrade to CocoaPods
Check if you are logged in with the same email with which you uploaded the pods to trunk (use the command
$ pod trunk me
). If you are not logged in with the same email, then you need to log in.Now, use the commands
$ pod lib lint LibraryName.podspec
to validate the library and$ pod trunk push LibraryName.podspec
to push the upgraded libary to CocoaPods respectively.
I hope this blog has provided a clear and straightforward path to publishing your library on CocoaPods. By following these steps, you should be well-equipped to share your work with the iOS development community. Remember, the process of creating and distributing a library is an excellent way to contribute to open-source projects and enhance your professional portfolio.
If you encounter any challenges or have any questions, don’t hesitate to reach out to me at sakshamshrey@gmail.com. Your feedback is always welcome, and I’d be happy to assist further. Happy coding, and best of luck with your library !
Subscribe to my newsletter
Read articles from Saksham Shrey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Saksham Shrey
Saksham Shrey
Student, learner and a tech enthusiast || likes to discuss all things tech and also abstracts || believes in First-Principles Thinking || ENTJ-A 🍁