Setting Up CocoaPods

The following are instructions on how to install CocoaPods to your Xcode project, which will be useful when we start our lectures on networking and external libraries.

CocoaPods

CocoaPods is a Swift dependency manager and it lets us install, manage, and use third party libraries in our code so that we can utlize functionalities already created by other people in our apps. One common way that we use CocoaPods in our course is to use AlamoFire, a popular Swift HTTP networking library, to help us send and receive messages to other servers via APIs. However, there are many other libraries that you can install that can help you do a number of things like loading images, laying out your views, and adding custom animations to your apps.

Setting Up CocoaPods

You must first install CocoaPods on your laptop to be able to use it. To do so, run the following command in your Terminal:

sudo gem install cocoapods

Once this command finishes running, you should have CocoaPods installed and should not have to reinstall it ever again. Now, in order to setup CocoaPods in our Xcode project, navigate to your project’s root directory in your Terminal. (An easy way to do this is to drag the folder from Finder into your Terminal window!) Once inside run the following command:

pod init

Running pod init will create a boilerplate Podfile inside the project's root directory. You can open the Podfile in a simple text editor, or use vim or emacs. Upon opening the Podfile, you should see something like this:

platform :ios, '13.0'
# ignore all warnings from all pods
inhibit_all_warnings!

target ‘SampleProject’ do
  use_frameworks!
 
  # Pods for SampleProject
  pod 'Alamofire'
  pod ‘SnapKit'
end 

The sample Podfile above already has two cocoapods added already on lines 9 and 10 as an example. Immediately after running pod init you won't have any pods in it.

Essentially, anytime you want to add a new cocoapod to your project, all you have to do is come into this Podfile and insert it as pod [name]. In the sample Podfile, we are saying that we want to use the Alamofire and SnapKit cocoapods in the project SampleProject. Once you have added all the cocoapods that you want to use, save the Podfile and return to your Terminal. The next command that we want to run is:

pod install

(Note that the Podfile needs to exist in the directory or else this command will not work!)

This command (1) looks at your Podfile to determine all the cocoapods that you want to use, (2) installs all of them, and (3) integrates them with your .xcodeproj into a .xcworkspace. For example, if our project was called SampleProject and we initially had a file SampleProject.xcodeproj, after running pod install, we would also have a file SampleProject.xcworkspace in the same directory.

After running pod install, you should always be working in the .xcworkspace! Only the .xcworkspace has all of the cocoapods imported for you to use.

If at any point later on you want to install more cocoapods, simply go back to the Podfile, add the cocoapod, and run pod install again.

Some Useful CocoaPods

The following are some useful cocoapods that we'll introduce in the course and highly encourage that you use!

  1. Alamofire

    Alamofire is a HTTP networking library that we'll be using in this course to connect your app to the Internet.

  2. SnapKit

    SnapKit is a library that makes it easier to use AutoLayout! We'll introduce SnapKit very briefly in the course, but we highly recommend that you use it since it simplifies your AutoLayout code significantly.

Last updated