COCOAPODS: a dependency manager for IOS development


IOS - Dependency Management Problem 


During my IOS development experience i've immediately dealt with the "dependencies management" problem.

Firstly, my needs were to share code among projects and the only way that i found out was to build "static link libraries" that are files with ".a" extension also because IOS doesn't support dynamic link ones.

But very soon i understood that building static libraries, as well as being a little bit complicated, was not the better way to manage project's dependencies. This because, often, my IOS projects were developed, reusing a bunch of code gathered by open source software and not all (almost no one) were distributed/packaged as static library, but provides just source code

So my short-term strategy was to copy&paste the code, coming in from third party projects, directly into my project. this strategy seemed working well until i had to deal with maintenance and bug fixing because it became very hard (almost impossible)  stay in sync with evolution/fix of third-party software included. The result was that i have had to maintain all code becoming the only responsible of its fix/evolution, disconnecting them from original owners.

Finally, a solution

To solve problem i started to seek a solution for "dependency management for IOS" and i discovered the unbelievable tool named "CocoaPods

Their site states: 
CocoaPods is the dependency manager for Objective-C projects. It has thousands of libraries and can help you scale your projects elegantly.

Concepts

CocoaPods is a tool built with Ruby and is installable with the default Ruby available on OS X. It is based upon Pod concept. A Pod is a dependency that refers to a Podspec. A Podspec is a dependency's descriptor that give detailed information about how to gather the dependency's stuff. Finally there is a Podfile that contains one or more Pod.

Currently there are more than three thousand projects "CocoaPods compliant" (i.e.  that own a Podspec) and they are collected within the  Official Podspec Repo

How to Use it

Install

Install and use it is a quite straightforward, to install it is enough run the following command:
$ [sudo] gem install cocoapods
A complete tutorial can be found here

Add dependency

The most interesting topic is how to add a new dependency to the project. 
Assuming that we have created a new project using XCode 5 let's start with an example that briefly describes all required steps. In this example i would use a library that make easier work  with IOS Core Data and i have chosen the MagicalRecord framework, that has been defined by its owner as .. Super Awesome Easy Fetching for Core Data. 

1. Check if the library is "cocoapods compliant" running the command :

$ pod search MagicalRecord

-> MagicalRecord (2.2)
   Super Awesome Easy Fetching for Core Data.
   pod 'MagicalRecord', '~> 2.2'
   - Homepage: http://github.com/magicalpanda/MagicalRecord
   - Source:   https://github.com/magicalpanda/MagicalRecord.git
   - Versions: 2.2, 2.1, 2.0.8, 2.0.7, 2.0.4, 2.0.3, 2.0, 1.8.3 [master repo]


As you see whether the library is present in the public repository a summary will be showed containing all the versions available for use.

2. Create a Podfile cointaining required dependecies

$ touch Podfile ; edit Podfile

After created a Podfile  to add a dependency we have to edit it and  type the pod specification (red line above) that is: 
pod 'MagicalRecord', '~> 2.2' 
3. Install(update) dependency 

Once saved the Podfile run the command:

$ pod install

Analyzing dependencies
Downloading dependencies
Installing MagicalRecord (2.2)
Generating Pods project
Integrating client project

That's all, but cocoapods behind the scene has performed the following tasks:
  1. Downloads the dependencies (i.e. source code/resources)
  2. Creates (if missing) a new Project called Pods
  3. Creates (if missing) a new Workspace  that join our project and Pods
  4. Adds(update) to Pods project all downloaded dependencies
After that, it is very important to close original project and open the workspace that is the file <project name>.xcworkspace. Once opened the new project layout in Xcode will be like this: 


As you can see the Pods project contains the required dependency and its products are more "static libraries".

libPods.a that will contain all dependencies, while libPods-MagicalRecord.a will contain only the stuff belong to MagicalRecord framework

Conclusion

I've given you just an idea of the several and powerful features provided by cocoapods. This tools is rapidly becoming a  first citizen in the "IOS development eco-system",  its simplicity both for publish and for use the pods, contribute at growing of its popularity.
I consider cocoapods a "MUST TO HAVE" whether your objective is to develop an App that you want maintain and evolve in elegant, efficient and productive way


So ... try it and let me know


  

Comments

Popular posts from this blog

POLYMER + TYPESCRIPT the marvelous couple

PlantText: The new "Online" UML Editor powered by PlantUML

PlantUML & Maven - Enrich your project's documentation