Distribute Framework Having Dependency On Other Pod

CocoaPods

In this tutorial our focus is to consume a framework that have a dependency on other pod. Open terminal and clone the starter project with the following command.

git clone --branch 1.0.3 https://github.com/aliakhtar49/MyFramework.git

This was the framework that we developed in previous part . If you follow previous part you can direct go to the section Getting Started. Framework responsibility was to hit network call using NSUrlSession and return data to the consumer application. In the previous part we saw how to consume framework locally and remotely and in this part we will add one dependency on our framework instead of using NSUrlSession it will use alamofire which comes in a pod.

If you don’t know or know about podfile, podspec and how to create framework it is highly recommended to look previous part. 😐

As shown in Figure 1 the cloned repository project structure on the disk.

Figure 1

You have to do following things if you didn’t create framework repository yet (or see previous part)

  1. Create MyFramework repository on github
  2. Clone the project to some empty folder
  3. Move the clone 1.0.3 MyFramework project contents into the empty clone repository folder
  4. Push the code

You can download the Consumer app (Consumer_Part2_Starter). The application will consume the framework we created

Getting Started

In this section we will add one dependency to our framework. Go to the root folder of MyFramework project in terminal as shown in Figure 2 and run the following command. This will create a podfile as shown in Figure 2

A Podfile defines what your project depends on.

pod init

Figure 2

Open podfile and replace it with the code shown in Figure 3. What we did we add Alamofire as a dependency to our framework.

Figure 3

Go to the root folder of MyFramework project in terminal as shown in Figure 2 and run the following command. This will install the Alamofire and also created a MyFramework.xcworkspace file. Now close the project and open MyFramework.xcworkspace.

pod install

Figure 3

As shown in Figure 4 we added new functionality in our framework. Now it has one additional method as compare to previous version. This method uses Alamofire to get data from server and Alamofire comes in a pod which act as a dependency to this framework.

Figure 4

Go to the root folder of the MyFramework project and open MyFramework.podspec file and replace code with the following code as shown in Figure 5. We did couple of things

  1. Cocoapods download the source with this git url and look for a tag 1.0.6
  2. When consumer is installing our framework using cocoapods then cocoapods first look the dependency and install it automatically by looking follow the step 1. Consumer application don’t need to worry about it

A pod specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.

Figure 5

Push changes to the repository you created. I pushed on my repo

Figure 6

Pushed tags to create a history points. This step is important. Since we will install pod on consumer app using this tag as shown in Figure 7

Figure 7

You can download the Consumer app (Consumer_Part2_Starter). The application will consume the framework we created. Open consumer application

Got to the root folder of Consumer application project and open podfile. As you can see we update the tag version to 1.0.6 as shown in Figure 7 this tag we pushed.

Figure 8

Got to the root folder of Consumer application project. First delete the podfile.lock and run the following command on terminal on a root folder of Consumer application project

pod install

As shown in Figure 9 Cocoapods will perform a number of tasks

  1. Read podfile from consumer app project and identify MyFramework as a dependency
  2. Go to the https://github.com/aliakhtar49/MyFramework.git (1.0.6 tag) and read podspec file
  3. On podspec identify Alamofire as a dependency of the MyFramework project
  4. Download all the dependencies
  5. Integrate it to the client
Figure 9

As shown in Figure 10. Now consumer app using our framework which has a dependency on other pod .

Figure 10

Next

In the next part we will see how to create a private pod for the organization usage