Introducing CocoaPods in Swift project

The underlying frameworks in iOS provide a solid foundation for such routine problems, but this is not always the case. Thus, the community builds different software solutions (some of them really robust) which are published as open source projects. These solutions are based on the iOS base layer, but solve a common problem well.

CocoaPods is a dependency manager for iOS projects written in Swift and/or Objective-C. It has more than 36,000 libraries (and growing) and is being used in more than 2.5 million apps.

We will be using it to add some extra functionality to our projects, but before that, we should understand how to add it to a regular project.

Ruby is the programming language behind the CocoaPods implementation. It comes bundled with your macOS and it's a no-brainer to install CocoaPods on your machine. This is the main reason why CocoaPods is so popular and widespread.

Here are the simple steps which you should follow to install CocoaPods on your machine:

  1. Open the Terminal
  2. Type the following command: sudo gem install cocoapods

CocoaPods should be integrated in your project. You can do this by simply adding a single file in the project root folder. In this special file, you should describe all dependencies that you need. Under dependency, we will understand an external framework (set of classes). Then, the rest will be handled by the dependency tool. It will fetch the referenced classes and assets and will link those versions to your project. The configuration file is called Podfile.

Open your project and add a new empty file, whose name is Podfile. You can use Xcode to create this file. Just click on File | New | Empty and name this new file Podfile.

Instead of creating the file manually, we can use CocoaPods to create it for us by performing the following steps:

  1. Open the Terminal.
  2. Navigate to the project folder using the following command: cd /path/to/your/project
  3. Generate the Podfile file using the following command: pod init
  4. That's it. You can verify that Podfile was generated in the root project folder with the ls -all command in the Terminal.

Here is what a very basic (empty) Podfile file should look like:

target 'SandBox' do
  use_frameworks!

  # Pods for SandBox
  pod 'Alamofire', '~> 5.0.0-beta.5'

end

Now, let's add our first dependency, Alamofire. To add your first dependency, which helps us to implement a remote communication with a public web service, we have to add the following line to our Podfile file:

pod 'Alamofire', '~> 5.0.0-beta.5'

When you have a Podfile file and all dependency projects listed, then it's time to install them. It's a piece of cake to do this. You have to open a Terminal window in the project root file Podfile file is located and execute the following command:

pod install

Our project was transformed slightly from CocoaPods. A workspace file was created. It includes our project and all dependencies, which are part of a pods project.

From now on, the workspace should be opened. The file which shows that there is a workspace from several projects is the .xcworkspace file. Instead of the well-known .xcodeproj file, we have to get used to open the workspace file that includes our project and additional project file, which includes all dependencies.

Each dependency declared in the Podfile file has a version rule next to it. Based on the version rules of all dependencies, CocoaPods decides what should be fetched and added to our projects. The following is a short explanation of the rules:

  • You can specify a particular version, for example, "2.0"
  • You can use logical operators (>,>=,<,<=), for example, > 0.2 which means a version higher than 0.2
  • You can use the optimistic operator ~> 0.2 - this means that any version higher than 0.2 and lower than 1.0 can be used

If CocoaPods can't find a resolution, then it will let you know that there is a problem when fetching the dependencies.

Now is the time to check a few commands which we should memorize when using CocoaPods.

The following command is used to update all dependencies (based on the versioning rules) or to update a specific one if we pass its name:

pod update

If you are looking for all outdated pods, then you should invoke the following command to see the list of dependencies which could be updated:

pod outdated