25 November 2013

Someone told that Cocoapods is easy to use at both aspects: integrating open third-party into own project and publishing opensource library to community. However, for a newbie or even an experience developer, at the first time, it is not easy as a pie. This post is for them. Here we go!

Preparing git repository

Pushing source code

You should push source code to remote repository first.

git add .
git commit -m "Initial commit"
git push origin -u master


A tag should be created for the source code because we will need tag version to config podspec file later.

git tag -a 1.0.0 -m "Make v1.0.0"
git push origin --tags

Create pod

Change directory to project which you want to create a pod. In my case, I will go to HUChartDemo:

cd HUChartDemo

Next, create a podspec file:

pod spec create HUChart    // May need sudo

Now, it’s time to config podspec file. See Pod specification to get further information. Just do like the example, it is OK. Note that s.version should be set by a tag version like:

s.version = "1.0.0"
open -e HUChart.podspec
vi HUChart.podspec

Thereafter, do a validation for podspec:

pod spec lint HUChart.podspec    // May need sudo

At the end of terminal screen, pod-name.podspec passes validation appears let you know that your pod is OK to publish.

alt text

Otherwise, check again all fields in podspec file to ensure that all is accurate.

Next, go to do the last task, send a pull request (PR).

A note for s.source_files attribute in podspec file

It is to locate source files you want to publish. Thus, be sure that you provide the exact path for s.source_files attribute. A legal path is a path from the root directory which contains .git folder to destination file. For example, the below code show a path for HUChartDemo project (but its repository name is HUChart, don’t be confused here, we need project’s root directory, not repository name).

s.source_files  = 'HUChart', 'HUChartDemo/HUChart/*.{h,m}'

Send a pull request to Specs

After that, we must send a pull request to Specs to make the pod is visible. To do that, follow these steps:

  • Fork Specs and clone it.
git clone https://github.com/CocoaPods/Specs.git
  • Change directory (cd) to Specs folder.
cd Specs
  • Assigns the original repo to a remote called “upstream”.
git remote add upstream git@github.com:CocoaPods/Specs.git
  • Create our own Pod folder, ex.
mkdir -p HUChart/1.0.0

Note that 1.0.0 is version number which we set for s.version = "1.0.0" in podspec file.

  • Copy .podspec file which we have already made some minutes ago to the version folder. In this case, version folder is Specs/HUChart/1.0.0
cp path/to/.podspec-file Specs/HUChart/1.0.0 
  • Do git add this file and commit with a clear comment. After that, push it to forked Specs repository.
git add Specs/HUChart/1.0.0/*.podspec
git commit -m "Added HUChart library to draw a semi circle chart."
git push origin -u master
  • Lastly, go to Specs pull page and press onto New pull request and fill all require fields to create a pull request. Thereafter, wait for Specs’ developers merge this request. Once they approve the request and merge it, we can use our lib as a Pod by Podfile.

It is so easy, isn’t it? But for me, the first time I created a pod, I wonder how pod work and how many necessary steps to make a pod successfully. Now, you have an own pod. Congratulations! Take time to share it as far as possible!

For Note: A source for setting up a private pod

blog comments powered by Disqus