Dependencies are the root of all evils.. Well it is not an absolute truth but sometimes it is.
If you use many external libraries in your iOS Apps, then you may have felt the need of a tools that could manage all the dependencies for you. Those who come from .NET or Java worlds know NuGet or Maven very well, these tools let you download and install on your project the libraries you need and manage all the dependencies by automatically download and install all the necessary modules. CocoaPods is a terrific tool that let you achieve these results in a nuts. But let’s dive into the installation process without hesitations. CocoaPods is a ruby gem so we need to accomplish a few steps before we can use it. These operations must be done only once.
Open XCode, go to the preferences, select the Downloads tab and install the command line tools if they are not, as shown below.
When installation is finished, open the Terminal and first of all update RubyGem
now install CocoaPods gem
and set it up
You have succesfully installed CocoaPods! Create a new empty project in XCode.
Now you have to setup the dependencies of your project, for this article I will explain how to create a project with a dependency to the Foursquare iOS API. We can use the pod command line tool to search in the CocoaPods specs repository on GitHub (for more information please visit CocoaPods web site).
Write in the Terminal window
as shown below
the tool will answer with all the entries that contains the word “foursquare”. In this case you’ll find only one entry.
In Terminal go to the folder where you’ve created the XCode project and create a podfile that will contain all the dependencies.
the file must be named podfile, the open command will open it in TextEdit for editing.
Now add the name of the library and the version required as shown.
the ~> characters means that we want version 1.1.0 or higher if available.
Save the file and go to the Terminal, now we need to install the dependencied in our project. The process is straightforward, just type
CocoaPods will create an XCode workspace with same name of our project, it will contains the project you have created and a second project with all the libraries needed to use and compile the foursquare API. It will also setup the project to manage all the references and the dependencies to our main project in order to compile it, please notice that CocoaPods added automatically a reference to JSOKit!
So now close the project and open the workspace, you will see both the projects as shown below.
Et voilà! Now build the project with Cmd-B and enjoy your dependencies!