Distribute Framework Using Private Cocoapods (Part 1)

CocoaPods

In this part we will see how to setup private cocoapods repo that can be used with the people that have access to it.

CocoaPods is a popular dependency manager for iOS projects. It’s a tool for managing and versioning dependencies.

Nowadays many organization are following Modular Architecture for developing iOS application. They created many reusable generic component as a framework that can be used in any project. Like Network framework , Extensions etc as a private pods which can be access within the organization. The benefits of this as follows

  1. Reduced huge amount of time/cost when new project arrives
  2. Standardized coding practice
  3. Reduced development cost
  4. Reduced compile time
  5. Built once and be reused an infinite number of times, which will eventually increase it’s stability/confident level
  6. Best for teams working parallely

As shown in Figure 1 ModuleExtensions and ModuleNetwork created once and used in many application using the power of cocoa pods

For the demo purpose we will create two frameworks name ModuleExtension and ModuleNetwork.After that we will create private cocoapods repo and add these framework into it and will consumes these framework using pod install command only in the consumer application.

If you don’t know how to create framework please refer to the previous part we did. Now we created ModuleNetwork and ModuleExtension frameworks as shown in Figure 2. You can download these frameworks as well

Now we need to add functionality to both the framework. For the simplicity we created a class and make a method that return hardcoded values as shown in Figure 3 and 4 to both the frameworks

Note: I forgot to write public init methods on both the framework which will create a problem when we accessing these classes from our consumer app so add these method on both frameworks.

Steps to configure Framework

Step 1 Setup Private Git Repository

Go to the github and create repository with name ModuleNetwork as shown in Figure 5

Create an empty folder and Clone the repository to that location as shown in Figure 6

Step 2 PodSpec File Creation

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

Open terminal and go to the root folder of ModuleNetwork and run the following command as shown in Figure 7. This command will create the podspec file of the framework.

pod spec create ModuleNetwork

Open ModuleNetwork.podspec file in the text editor and replace it with the following code as shown in Figure 8. Make sure you replace framework private git repo with yours. Save and close the file

Step 3 push with tag

Now go to the root folder of ModuleNetwork and copy the contents and paste it to the empty clone framework folder as shown in Figure 9

Run the following commands in Terminal to commit those files to the repository and push them back to the server and tag it . make sure this tag should be match with podspec file

Go to the github server and check everything pushed and tagged as shown in Figure 10

At this point we setup ModuleNetwork framework. Now repeat step 1 to 3 for the ModuleExtension as well. In Figure 11 and 12 I showed some proof

Now at that point we are done with the framework configuration. In the next section we will create a private pod repository to host our private framework.

Steps to configure Private Pods

CocoaPods is a great tool not only for adding open source code to your project, but also for sharing components across projects. You can use a private Spec Repo to do this.

There are a few steps to getting a private pods setup for your project; creating a private repository for them, letting CocoaPods know where to find it and adding the podspecs to the repository.

Step 1. Create a Private Spec Repo

As we already know how to create a repo on github so create a private repository with name “PrivateFrameworkSpecRepo” as shown in Figure 13 .
https://github.com/CocoaPods/Specs . this is the public cooca pods repo where all publicly available pods are host (podspec file). In this step we created our private repo where our organization private pod will host

Step 2 Add your Private Repo to your CocoaPods installation

As shown in Figure 14 after run pod repo it will list down all the cocoa pods repo as you can see i have one public and twp private repo installed but PrivateFrameworkSpecRepo was not installed yet

Now run the following command to install our private repo. Make sure you change the git url with yours . It’s the same as we first install our public cocoapods repo like sudo gem install cocoapods

To check if your installation is successful and ready to go: as shown in Figure 16

Step 3 Add your Podspec to your repo

Run the following command to push our framework ModuleNetwork.podspec to this private repo as shown in Figure 17

As shown in Figure 18 it is added to our private repo

Run the following command to push our framework ModuleNetwork.podspec to this private repo

At this point our framework is added to private pods and all setup. Now in our organization new project arrived now we create a new application and install our private pods which will reduce the development cost

Download the consumer application project with name of a folder “PrivateFrameworkSpecRepoConsumer” or you can create a single view application contains pod file

Now open the podfile and replace the code as shown in Figure 19. We are telling podfile to look both public and private pod repo for the pod we need for this project .

Now run pod install and it will install dependency from the private pod repo as shown in Figure 20

As shown in Figure 21 we are now accessing both the frameworks using private pod .

What happened

When you run pod install following steps will happen

  1. First read the podfile and list down all the dependencies
  2. Look all the sources specified in the podfile. In our case it found our dependencies in private source (private pod)
  3. Read podspec file with the name of the dependencies . In our case read ModuleExtension.podspec and ModuleNetwork.podspec and in the file we specified git url where it will find the source code
  4. Download the source from the private git url and install it to the client

Useful links

https://guides.cocoapods.org/making/private-cocoapods.html
https://github.com/artsy/eigen/blob/master/Podfile

Senior iOS Engineer | HungerStation | Delivery Hero