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:
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
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
Instead of creating the file manually, we can use CocoaPods to create it for us by performing the following steps:
Podfilefile using the following command:
Podfilewas generated in the root project folder with the
ls -allcommand 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
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:
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:
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:
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: