CocoaPods Tutorial using Swift and Xcode

Learn how to install and use Cocoapods in your Xcode project! Take advantage of third party Swift libraries and GitHub repositories easily.
Written by

Chris C

Updated on

May 29 2023

Table of contents
    Cocoapods

    Somewhere along your iOS development journey you will come across the term cocoapods or pods.

    What is it and what does it do? Cocoapods is an application level dependency manager that runs on objective-c, swift, and any other programming languages that run on Objective-C. It focuses on source-based distribution of third party code and allows automatic integration to your Xcode projects.

    It is in essence, an app that helps integrate third party repositories in your projects.

    This guide will help you to set up your first project cocoapods powered project.

    Setting up

    Step 1:

    To make sure that cocoapods is installed open a Terminal app on your computer

    Terminal

    Step 2:

    Then open cocoapods.org and scroll down to see the instructions for setting up

    Cocoapods website

    Step 3:

    Go back to your terminal and follow to command “sudo gem install cocoapods” it will then ask for to provide your password in order to continue

    Installing Cocoapods via Terminal

    And that’s it! after it completes you should be ready to use cocoapods in your system now.

    Adding a Pod

    Step 1:

    Once we complete setting up are cocoapods we should be ready to add a third party library to our project, you can use cocoapods.org to browse the available libraries, for this example we’ll be using NavigationDrawer, so let’s go ahead and open it the page

    Browsing libraries on Cocoapods.org
    Choosing a pod

    Step 2:

    Once we have selected our pods we should now create/open the project that we want to integrate the pod to. Once our project is ready just open a terminal and navigate to the project folder

    Navigate to your Xcode project folder

    Note: You do not need to have the project open, It is just kept it open in this example to make it clearer

    Step 3:

    Init pod to create a podfile for your project

    Once you have navigated to your project do a pod init”, this will create a Podfile in your project folder (make sure to do this just outside of the project files)

    Step 4:

    Open the podfile and list the pods you want to use

    Open the Podfile in TextEdit or any text editing software you prefer

    Step 5:

    Check the library page to check on how to install the pod and add this line on your Podfile

    The pod name can be found on the library page
    Save the podfile

    Save what you added to the Podfile

    Step 6:

    Finally, do a “pod install” in your terminal and wait for it to install the pod

    Use the pod install command to download the libraries
    Open the xcworkspace project file instead of the xcodeproj file from now on

    Doing so will create multiple files in your project folder

    From now on you should open the project using the .xcworkspace in order to properly build and use the pods in your project. Opening the file should make your project look like this

    In your Xcode file navigator, you'll find the libraries you added

    And that’s it! Just follow the instructions on the library on how to use the pod, remember different libraries have different ways to give samples and usage of their project 

    Check the pod library page for instructions on how to use that particular pod

    Conclusion

    You have now properly set-up and added a cocoapod dependency to our project, There are plenty of libraries out there for you to depend/integrate on, some popular ones are Alamofire and SwiftyJson which we will cover in another article. Perhaps in the future you might want to make a library of your own or contribute to to improve an existing pod, only time can tell but what’s important is the first step that you took today. Happy coding! 🙂


    Further Reading




    Get started for free

    Join over 2,000+ students actively learning with CodeWithChris