Cocoapods was the most commonly used tool for installing external libraries before the introduction of Swift Package Manager. This means it is very likely to find it in older projects, so this article may be helpful for you, however we recommend to use SPM when possible.
Installing Cocoapods
To use Cocoapods, we need to install it on our Mac. To do this, simply run the following command in the terminal:
sudo gem install cocoapods
Initializing Cocoapods in Our Project
Once installed, we need to initialize it in our project. In the terminal, navigate to the root folder of your Xcode's project and run the following command:
pod init
This should create a podfile, in which we will add the libraries we want to use.
Installing Our First Pod
If we open our podfile with any text editor, we will see some comments (those that start with #) and a "target" with the name of our project (in this example, "EducaSwift"). Right below the comment # Pods for EducaSwift, we can include all the libraries we want, and it should look like this:
# Uncomment the next line to define a global platform for your project
# platform :ios, '18.0'
target 'EducaSwift' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for EducaSwift
pod 'Alamofire'
end
In this case, we will add the Alamofire pod, and once it is added to the list, all that remains is to run the following command in the terminal:
pod install
This should have created a new .xcworkspace
file with an icon similar to the one we use to open the project. However, from now on, we must open our project using this new file, as it includes both our project and all the pods we install from now on.
Updating Our Pods
When we installed our pod, the most recent version of it was installed. In case the owner of this pod releases a new version, we need to use the following command to update it in our project:
pod update
We can also update a specific pod by specifying its name.
pod update Alamofire
Controlling the Version of Pods
Since a new version of a pod could "break" our code, for example, if a method no longer exists, we can lock it to a specific version to prevent anyone from accidentally updating it.
To do this, we specify the version in the podfile, right after the name.
# Uncomment the next line to define a global platform for your project
# platform :ios, '18.0'
target 'EducaSwift' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for EducaSwift
pod 'Alamofire', '5.4.1'
end
There is also the possibility of allowing minor updates, for example, only when the last digit of the version changes (5.4.2, 5.4.3, 5.4.4...). This allows us to keep updating our pod, but without major updates, or at least that's the theory, as it depends on the pod owner.
To do this, we include ~>
next to the version, like so:
pod 'Alamofire', '~> 5.4.1'
// The following versions will be installed
// 5.4.2
// 5.4.3
// 5.4.4
As mentioned, this will only update to versions where the last digit has changed. If you specify only two digits, versions where both the second and third digits change will be updated.
pod 'Alamofire', '~> 5.4'
// The following versions will be installed
// 5.4.2
// 5.4.3
// 5.4.4
// 5.5.1
// 5.5.2
// 5.6.1
// 5.6.2
// 5.6.3
// ...
Be the first to comment