Getting started to create your CocoaPods library step-by-step
Setup Pod Project
- Open Terminal. Type the following command to initialize your pods project.
pod lib create <Your_Project_Name>
Please make sure you have installed CocoaPods & Xcode before you go, otherwise you will get error.
To install CocoaPods, you can type the following command.
gem install cocoapods
And you can install Xcode in App Store on Mac.
For example, if I am going to create my project which named KTLoadingView, I will enter like this in Terminal.
pod lib create KTLoadingView
It will check duplication of your project's name in those created CocoaPods libraries. If your project's name is duplicated, you will see error like this.
Follow the steps of setup if your project's name is validate.
Your project will open automatically while the setup is completed.
2. Go back to Terminal, type the following command to check your project's .podspec file is passed validation. It may takes a minute.
pod lib lint <Your_Project_Name>.podspec
Unfortunately, you will see some warnings or even some errors. Try to update the information in the .podspec file to fix those warnings.
Some of you will see the following error.
Just go to:
Xcode > Preferences > Locations
And make sure one of Xcode version is selected in Command Line Tools section.
Here is my .podspec file for your reference.
If your project requires other pods libraries to work. Just add s.dependency attribute to the .podspec file
// Syntax
s.dependency '<Pod_Name>'
// Single dependency
s.dependency 'Alamofire'
// Multiple dependencies
s.dependency 'Alamofire'
s.dependency 'SDWebImage'
and more...
After completed the .podspec file, run lint command again to do validation.
pod lib lint <Your_Project_Name>.podspec
Somehow, you will see an error like this.
- WARN | url: The URL (https://github.com/kokitang/KTLoadingView)
is not reachable.
It is because you haven't create your project on GitHub yet. So, you need to create a repository on GitHub.
And then, from the command line, execute the following commands.
1 git add .
2 git commit -m "Initial Commit"
3 git remote add origin https://github.com/<GITHUB_USERNAME>/<Your_Project_Name>.git // replace <GITHUB_USERNAME> with your github.com username and <Your_Project_Name> with your project name
4 git push -u origin mastergit push -u origin master
At this point, if you’ve done everything correctly and lint the .podspec file again, it should pass validation.
Code the project
Before you go through this step, you should setup your pods project completely. If not, go back to previous step and follow instructions.
- Find the file ReplaceMe.swift under Pods/Development Pods/<Your_Project_Name>/Pod/Classes/ and replace by your library's .swift file.
- Go back to Terminal, execute following commands.
1. cd Example
2. pod install
The above commands is to install your pod library to the Example project of your pods library. The Example project is for other developer to understand how to use your pods library by providing some sample codes to them.
Remember to run pod install again on each modification of your library's source file (<Your_Project_Name>.swift) to make sure your Example project using newest codes.
3. Open <Your_Project_Name>/Example for <Your_Project_Name>/ViewController.swift in the Xcode project. (Opened automatically at previous steps).
4. Modify the code with your sample code and press CMD + R to run the codes. Make sure the codes work.
5. If everything works fine, it's time to update your project's README.md under <Your_Project_Name>/Podspec Metadata/. Here is my one of the CocoaPods library's README.md, you may reference it to create your own one.
Although it's optional to modify the README. But it's one of how others define it's a high quality project or not. Remember that a README is often the first thing that someone will see when looking at your pod. First impression is important!
Update to GitHub
Before you release your pod. You have to push your most updated source code onto GitHub. Tag a version of your pod which is same as s.version in .podspec.
// Commit code with comment message
1. git add -A && git commit -m "Release 0.1.0"
// Tag the version
2. git tag '0.1.0'
// Push tag to GitHub
3. git push --tags
// Push to GitHub
4. git push origin master
If you have any changes in future. Simply execute command 1 & 4 with commit message to update the Git repository. And if you want to release a new version of your pod, execute command 2 & 3 with updated version number then.
Release to World
Final section of this article!
Push the spec to the Specs repository by executing the following command.
pod trunk push <Your_Project_Name>.podspec
You may see an error message like.
[!] You need to register a session first.
Since we are using trunk to push our pod to CocoaPods, it requires a registration to verify your identity. So execute following command to register your session on CocoaPods first.
pod trunk register <Your_Email> '<Your_Name>' --description='macbook pro' --verbose
After registration you will receive an activation email. Click the activation link in the email. The registration is complete.
You can use me command to check your information.
pod trunk me
Now, execute the following command again.
pod trunk push <Your_Project_Name>.podspec
pod trunk push command will do 3 works:
- Validate your local .podspec file (Can use pod lib lint command to validate yourself).
- Upload your .podspec file to trunk.
- Transform your .podspec file to JSON format file that requires by trunk.
If the push is success, you will see the success message.
Congratulations!
CONGRATULATION! You are now published your first CocoaPods library to the world! Enjoy your work!
Last but not least
- You can use pod search <Your_Project_Name> command to check your project's information. It can use to check whether it's publish successfully on CocoaPods.
$pod search KTLoadingView
-> KTLoadingView (0.1.0)
KTLoadingView is a subclass of UIView that provides animated loading page.
pod 'KTLoadingView', '~> 0.1.0'
- Homepage: https://github.com/kokitang/KTLoadingView
- Source: https://github.com/kokitang/KTLoadingView.git
- Versions: 0.1.0 [master repo]
- You can use pod trunk add-owner <Your_Project_Name> <Other_Email> command to empower others to build your project's codes.
- Steps to update pod in future
** GitHub **
// Commit code with comment message
1. git add -A && git commit -m "<Update_Message>"
// Tag new version
2. git tag '<New_Version>'
// Push tag
3. git push --tags
// Push source
4. git push origin master
** Cocoapods **
// Update version
1. set the new version to <New_Version>
// Update Tag
2. set the new tag to <New_Tag>
// Push to release new version
3. pod trunk push <Your_Project_Name>.podspec