Beginner’s guide for creating a private cocoa-pod

Zarif Ahmed
8 min readMay 11, 2018

--

The benefits of reusing code are obvious. For every application you build, you can [possibly] reuse the generic code that you created for the one before. This saves time, improves the features in your program, and generally makes for more cost-effective programming. As your libraries grow it becomes easier and easier to lay the framework for more sophisticated applications, which requires less effort. However, there is a need for a proper process to maintain and update the reusable code so that the entire team can utilise it. This can be easily achieved using cocoa-pods which provides the support of creating private pods.

Here is a step by step guide to achieve it.

Make sure you have cocoa-pods installed on your system before moving forward. [Follow this link to get started with cocoapods on your system]

Step 1: Repo Creation

I have used bitbucket which allows us to create private repos for maintaining the codebase in this guide.

To keep things organised, we will first create a project in bitbucket called POD_BASE and add all of our repos inside it. Here is a link for creation of project in bitbucket.

Create 2 private repos in inside the POD_BASE project;

  • the first, to hold the podspec [calling this POD_SPEC_REPO henceforth]
  • the second, to hold the re-usable component [calling this POD1_REPO henceforth] that we wish to share across the team.

Here is link for creation of repo in bitbucket.

Note: The above repo names are generalised names. The actual repos that were created for the purpose of this article in bitbucket have the names BannerSpec [POD_SPEC_REPO] and BannerPod [POD1_REPO].

Step 2: Add the Spec Repository to your CocoaPods Installation

Run the following command from your terminal:

pod repo add POD_SPEC_REPO [POD_SPEC_REPO_URL]

  • POD_SPEC_REPO is the spec repo name
  • POD_SPEC_REPO_URL is the bitbucket URL of the repo. [https://bitbucket.org/<username>/<spec_repo_name>.git]

You can run the above command without worrying about your current directory. However, navigating to the spec repo directory before running the above command let’s you track all the added specs repo.

To navigate to cocoapods spec repo directory, run:

cd ~/.cocoapods/repos/

This is the place where all of your local podspec repos are located.

Once the spec repo has been succesfully added you can validate it by running the following command:

cd SPEC_REPO_NAME/
pod repo lint .
2.1 pod repo add

The master repo in the ‘~/.cocoapods/repos/’ folder is the public podspec repo of cocoa-pods.

Step 3: Create Your Pod

Clone the POD1_REPO that you created in Step 1.

git clone https://bitbucket.org/<username>/<pod1_repo>.git

Open Terminal, Navigate to the POD1_REPO directory which you just cloned.

To generate your Pod, run the following command while standing at your pod1 directory.

pod lib create [POD1_NAME]

  • where [POD1_NAME] is the name of the pod you’re creating and want to share within your organization.

Once you run the above command, you would be prompted with a series of questions relating to the setup of your POD. On answering all the questions, cocoapod will run ‘pod install’ on the sample project, provided if you answered, Yes, to include a demo app.

3.1 pod lib create

After the completion of this command the .workspace project will open up automatically. If the workspace doesn’t open up, You can navigate to the Example folder in the project repo and open the <POD1_NAME>.xcworkspace.

3.2 xcworkspace location

The pod lib create command also adds a .git file for source code management as it is using the default pod-template. As a result, there would be 2 .git files in our project. Go ahead and delete .git file inside the Pod [i.e inside ZBanner] folder so that the entire code falls under the POD1_REPO that we created in Step 1.

3.3 pod template .git

To remove the git tracking from ‘ZBanner/’ folder, run:

rm -rf .git

3.4 Remove additional .git file

Also, let’s re-organize the folder structure, so that the .podspec and .git files are under the same folder [screenshot attached: 3.6/3.7].

3.5 folder structure 1
3.6 folder structure 2

Step 4: Edit the Podspec File

Open the podspec file that has been created. Thankfully Cocoapod has generated a well completed pod spec template for us.

A Podspec file, or Spec, describes a version of a Pod library. It includes details about where the source files are located, which files to use, the build settings to apply, dependencies, frameworks used and other general metadata such as the name, version and description for the Pod.

Below is the Podspec generated by cocoapod for our private pod:

4.1 default podspec file

Now, open up the terminal again and go to the folder where the .podspec file is located and run the following command without changing anything in the .podspec file.

pod lib lint <POD1_NAME>.podspec

When you do that, you might see the following output.

4.2 “lint” default pod spec

If you do see the error shown in the screenshot, that means we have to correct our spec file. Make the following changes:

  • Add the summary and description. ‘Description’ should be longer than ‘Summary’ or you would be thrown an error while linting.
  • By default, spec ‘source’ and ‘homepage’ value will be set to github URLs. Change this to your bitbucket URLs.
  • In-order to set the validator to use swift-4, add these lines:
s.platform = :ios, “9.0”s.pod_target_xcconfig = { ‘SWIFT_VERSION’ => ‘4.0’ }`echo “4.0” > .swift-version`

Updated podspec file:

4.3 updated podspec

Now, run the pod lib lint <POD1_NAME>.podspec command again.

This time you’ll see that the command is successful and your output will be as shown below:

4.4 lib lint success

All good with podspec file. Let’s add the shareable code to the POD now.

Step 5: Add Code in your Pod

You would find ReplaceMe.swift file in the ‘Classes’ folder inside the POD [ZBanner]. Navigate to this ‘Classes’ folder and delete the ReplaceMe.swift file.

5.1 ReplaceMe.swift location

Drag and drop the component code [in this case BannerView folder] that you want to share across the team inside the Classes folder.

5.2 add your sharable code

NOTE: If you don’t have any assets to be added, Delete the ‘Assets’ folder.

Now we want to test these files in the Example project we created. For this,

  • Navigate to Example folder inside the repo
  • Run pod install from your Terminal.
5.3 installing pod locally

This will install the files in the Example project.

Test the shareable code in the Example project. Once you have completed your testing, you are ready to push your code.

5.4 testing the installed pod

Step 6: Push your Code

Now that you’ve built and tested your Pod, it’s time to deploy it to your private bitbucket repository.

Step 6a: Pushing to Pod Repo and Tagging

To push the shareable code into your POD1_REPO, Launch the Terminal and run following commands:

- Navigate to POD1_REPO in your system [cd /../../BannerPod]- git add .- git commit -m “<Your commit message>”- git push origin master

Tagging:

Tag the last commit with the version number specified in the spec file.

- git tag ‘0.0.1’- git push --tags

Step 6b: Push to Spec Repo

Before you actually push the POD to spec repo, You would first need to run the following command:

pod spec lint <POD1_NAME>.podspec

This will validate whether the spec in bitbucket which you just pushed is proper. You can read the difference between pod lib lint and pod spec lint here.

6.1 pod spec lint

If the pod spec lint command succeeds then you are ready to push your pod to the spec repo.

Run the following command to send the POD to your private Podspec repo that you created in Step 1.

pod repo push [POD_SPEC_REPO] [POD1_NAME].podspec

6.2 pod repo push

And That’s it! Your team can make use of the component that you just created in any of the projects by following Step 7.

Step 7: Using the Private POD

In the podfile of your Xcode project, specify the URL of your private spec repo at the top.

NOTE: You would also need to add the URL of the public spec repo of Cocoa-Pod if you want to install other public pods.

7.1 pod file

Once you have done that, just specify the pod name that you want to use and run pod install using Terminal from your podfile directory.

Additional Info:

1. Custom Template

If you take a closer look at the logs after running pod lib create, you would notice that the pod we are trying to create is using a default template specified here.

pod template

To specify your own pod-template use,

pod lib create <POD1_NAME> --template-url=URL

  • where URL is the git repo containing a compatible template.

2. Project Structure

You need to create the POD_SPEC_REPO only once. You can push the podspecs of multiple pods onto the same POD_SPEC_REPO.

But, each pod will have it’s own private repo!

pod base structure

--

--