CocoaPods – Installation & Upgrade

Photo by Shania Pinnata on Unsplash
Photo by Shania Pinnata on Unsplash
CocoaPods is a package management tool for Xcode. This article will explain how to install CocoaPods, how to create a CocoaPods project, and how to update packages.

CocoaPods is a package management tool for Xcode. Package management tools are a very important part in software development. This article will explain how to install CocoaPods, how to create a CocoaPods project, and how to update packages.

Imagine if there is no package management tool, every third-party package may have a different way to integrate. In addition, each package has a dependency relationship with other packages. This means that you have to integrate those dependent packages, and you have to integrate the correct versions.

The most famous are:

  • CocoaPods
  • Carthage
  • Swift Package Manager

Swift Package Manager is officially maintained by Swift. However, the number of users of CocoaPods will be still very large in next two years.

Installing CocoaPods

First of all, the command to install CocoaPods is as follows:

% sudo gem install cocoapods

pod init: Creating a CocoaPods Project

To create a CocoaPods project, you must first create a Xcode project, and then import CocoaPods into the project.

Open Xcode, and create a Xcode project named CocoaPodsExample. Build the project and close Xcode.

Open terminal, and switch to the directory of the CocoaPodsExample project.

% cd CocoaPodsExample
CocoaPodsExample % ls
CocoaPodsExample  CocoaPodsExampleTests
CocoaPodsExample.xcodeproj  CocoaPodsExampleUITests

Initialize CocoaPods to the project. A Podfile file will be added under the directory. Podfile is the configuration file of a CocoaPods project.

CocoaPodsExample % pod init
CocoaPodsExample % ls
CocoaPodsExample  CocoaPodsExampleUITests
CocoaPodsExample.xcodeproj  Podfile  CocoaPodsExampleTests

Let’s include Alamofire framework into the Podfile.

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'CocoaPodsExample' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for CocoaPodsExample
  pod 'Alamofire', '~> 5.1'

  target 'CocoaPodsExampleTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'CocoaPodsExampleUITests' do
    # Pods for testing
  end

end

pod install: Installing Packages

Install packages addressed in the Podfile.

CocoaPodsExample % pod install
Analyzing dependencies
Downloading dependencies
Installing Alamofire (5.1.0)
Generating Pods project
Integrating client project
...
CocoaPodsExample % ls
CocoaPodsExample  CocoaPodsExample.xcodeproj
CocoaPodsExample.xcworkspace  CocoaPodsExampleTests
CocoaPodsExampleUITests  Podfile  Podfile.lock Pods

After the installation, three files will be added under the directory. They are:

  • Podfile.lock records which packages are installed and which version is installed. You will not edit this file directly.
  • The package files are under the Pods directory.
  • CocoaPodsExample.xcworkspace is a new project file. After that, you must use Xcode to open the .xcworkspace instead of the original .xcodeproj file.

CocoaPods Version Rules

When we added Alamofire package to the Podfile, we saw ‘~> 5.1’. This means that any version of Alamofire that is greater than or equal to 5.1 and less than 6.0 can be installed. The version rules are:

  • ‘> 0.1’: Any version greater than 0.1
  • ‘>= 0.1’: Any version greater than or equal to 0.1
  • ‘< 0.1’: Any version less than 0.1
  • ‘<= 0.1’: Any version less than or equal to 0.1
  • ‘~> 0.1.2’: Any version greater than or equal to 0.1.2 and less than 0.2
  • ‘~> 0.1’: Any version greater than or equal to 0.1 and less than 1.0
  • ‘~> 0’: Any version greater than 0, that is, any version is fine. You can also skip it directly.

In other words, the actual installed version is recorded in Podfile.lock, and Podfile describes the rules for installable versions. So when you want to confirm the currently installed version of a certain package, check it in Podfile.lock.

pod outdated: Listing Updatable Packages

When you want to know which installed packages have new versions, you can use the following command. This command will list those packages in Podfile.lock that have new versions available for download.

CocoaPodsExample % pod outdated

pod update: Upgrading Packages

When you think of updating a certain package, you can use the following command to upgrade. It will download and upgrade the package, and the new version number will be recorded in Podfile.lock.

CocoaPodsExample % pod update PODNAME

However, it is also very troublesome to upgrade each package by commands. Use the following command to upgrade all packages at once.

CocoaPodsExample % pod update

Conclusion

CocoaPods automatically help you manage dependencies and install the correct package versions. You only need to add packages to Podfile, or change versions in Podfile, CocoaPods will help you accomplish the rest.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Photo by Alex Alvarez on Unsplash
Read More

Dispatch Queue Tutorial

Grand Central Dispatch (GCD) provides efficient concurrent processing so that we don’t need to directly manage multiple threads. Its dispatch queues can execute tasks serially or concurrently.
Read More