Tutorial

Using CocoaPods in Your Swift and Objective-C Projects


Understanding CocoaPods, a dependency manager for Swift and Objective-C projects, is a critical skill every iOS developer should have. If you have no experience with CocoaPods, this short post is written for you. We’re going to take a look at what CocoaPods is, why you should start using it, and how to setup a project with cocoa pods installed!

While most of our tutorials are very detailed, this tutorial is much shorter than a traditional article and only intended to get you off the ground with Cocoapods.

What is CocoaPods?

CocoaPods is a dependency manager for Swift and Objective-C projects. If you’ve ever used Node.js, Ruby on Rails, Python, etc., you’re probably familiar with the concept of a dependency manager. If not, that’s okay! A dependency manger is a tool that manages a set of frameworks and packages for developers. So instead of having to manually import files via drop and drop, a dependency manager like CocoaPods takes care of all that for you.

Consider this sample scenario: You’re working on an app and need to make use of a third party framework like Firebase.

Firebase relies on several other frameworks to work. In order to use it, you have to import all the frameworks Firebase requires, plus Firebase itself. This is a tedious process if you have to do it manually.

Let’s take this example even further. If Firebase ships an update to their SDK, you’ll have to redownload the entire SDK and replace it manually.

This is why CocoaPods has its place. It simplifies the whole process by automatically finding and installing the frameworks, or dependencies require. You will understand the power of CocoaPods in a minute.

Setting Up CocoaPods on Your Mac

Setting up Cocoapods is simple and straightforward, and a very rewarding process. To install Cocoapods, navigate to the terminal and type the following commands:

This line of command installs the CocoaPods gem on your system. CocoaPods was built with Ruby, so it relies on the default Ruby available on OS X for installation. If you’re familiar with Ruby, gems in Ruby are similar to pods in CocoaPods.

You may be prompted to enter your root password and then hit enter. Note that the password will not appear on screen as terminal does not display passwords.

cocoapods-install

It’ll take several minutes to finish the installation. Just be patient, grab a cup of coffee, and wait the whole process to complete.

Using CocoaPods for Xcode Projects

Once you have CocoaPods installed on your Mac, let’s see how to use it. We will create a sample project and demonstrate how you can install the Firebase framework in the project using CocoaPods.

First, create a new Xcode project and name it CocoapodsTest. Close the project and back in terminal. Use the cd (change directory) command to navigate to your new Xcode project. Assuming you save the project under Desktop, here is the command:

Next, we need to create what’s called a Podfile. A Podfile is a file that lives in the root of your project and is responsible for keeping track of all the pods you want to install. When you ask CocoaPods to install any pods or updates to your existing pods, CocoaPods will look to the Podfile for instructions.

To create a Podfile, simply type the following command:

CocoaPod then generates the Podfile like this:

That’s the basic structure of a Podfile. All you need to do is edit the file and specify your required pods. We are going to use a text editor called Vim to edit the file. Vim is a program built into every Mac that allows developers to edit content within terminal. That said, you’re free to use other text editors like Atom.

Type the following command to open the file with vim:

Suppose you’re going to use Firebase in your Xcode project. To do so, edit the file content like this to configure the Firebase pod.

That’s it. To exit vim, hit the escape key and then type:

This means write and quit, which will close vim with your changes saved.

Before we move to the last step, let us go through the above configuration:

  • The Podfile describes the dependencies of the targets of your Xcode project. Therefore, we have to specify the target, which is CocoapodsTest for this demo.
  • The use_frameworks option tells CocoaPods to use frameworks instead of static libraries. This is required for Swift projects.
  • The line that we have just inserted (pod 'Firebase') lets CocoaPods know that we need to use the Firebase pod. You may wonder how do you know the pod name. Normally you can look it up in the documentation of the pod or simply search it on cocoapods.org.
pod-cocoapods-org

Now that you should have a better understanding of the pod file, type the following command in the terminal to complete the last step:

Cocoapods will now install the Firebase pod! After downloading the Firebase pods, it creates a workspace file named CocoapodsTest.xcworkspace. This workspace file bundles your original Xcode project, the Firebase library, and its dependencies.

pod-install

So from now on, you have to use CocoapodsTest.xcworkspace instead of CocoapodsTest.xcodeproj.

Opening the Xcode Workspace

If you open CocoapodsTest.xcworkspace with Xcode, you should find both the CocoapodsTest project and the Pod project, which contains the Firebase library.

pod-xcode-workspace

Now all there’s left to do is use the Firebase pod. Let’s navigate back to Xcode and inside the IDE, go to ViewController.swift. At the top, type:

And look at that! You are up and running with CocoaPods!

Wrapping Up

CocoaPods is an incredibly simple tool that every iOS developer should have in his/her backpocket. I hope this tutorial is clear cut and easy to follow. If you have any thoughts or questions, please leave them in the comment.

Editor’s note: Apple is going to release the Swift Package Manager, which is very similar to CocoaPods, and is integrated into the Swift build system to automate the process of dependencies management. The Swift Package Manager will be available this fall. We will talk more about that when it is officially released. Stay tuned.
iOS
How to Access Photo Library and Use Camera in SwiftUI
SwiftUI
Introduction to UI Testing in SwiftUI Using XCTest Framework
SwiftUI
Building Collection Views in SwiftUI with LazyVGrid and LazyHGrid
  • Иван Кондраль

    Isn’t new swift package manager the same thing as CocoaPods?


  • J4L3B4

    J4L3B4J4L3B4

    Author Reply

    I liked this article and especially appreciated it’s conciseness. As you showed in your article, CocoaPods is simple and quick to setup, however I feel that any new articles on CocoaPods should show the use of the CocoaPods App in favor of the traditional usage.


  • Pragadeesh Durai

    can you please do a tutorial with the latest firebase version
    its a bit confusing…


  • Krunal thakkar

    its very useful tutorial….thanks….


  • anamika@gmail.com

    I’ve one doubt ., I used cocoapods to install animation framework.,
    1. Can I modify the framework’s object file as per my wish?
    2. can I add more frameworks after creating workspace using pod install?


  • Stevechang64

    Very good article. Just one clarification. If I now want to creat another project say using a different pod like alamofire do I need to do the cocopods setup step again, or do I just move to the step of creating a pod file in my project directory.


    • Ranjeet Singh

      you don’t need install cocopods again just go to the project do pod init and further steps.

      cocopods is to setup in your MAC machine .


  • mark

    markmark

    Author Reply

    Hey I have created my own library in Objective c and set it on Cocoa pod. I have also use use_frameworks! flag in my pod file still i am not able to access file add by pod in my viewcontroller.swift file. how can i do this. do i need to set any other flag ?


  • David M. Helmuth

    Is import Firebase actually FirebaseCore


  • mobile app development

    Thanks for sharing such a great blog..Keep posting


  • stewartcristan

    This post is helpful to every iOS app developer to enhance their skills…


  • Hanry

    HanryHanry

    Author Reply

    Thanks for such an amazing introduction to CocoaPods. It does help me a lot as a beginner iOS Developer. I’m looking forwards to seeing more and more of these sorts of articles.


  • Hannah Cuthbertson

    Very Informative Article. Thanks for sharing Amazing info. Keep it up.


  • Andrew Haris

    Thanks for sharing comprehensive post & valuable content, I agree with your points, I appreciate your efforts that you have selected this topic to write an article.


  • MLM software company

    It was great reading this stuff. Thanks for sharing.


  • Edward

    EdwardEdward

    Author Reply

    Great piece of content!! thanks for sharing with us.


  • mlm software

    The best and most accepted mlm software in the industry


  • Infinite MLM Software

    Great content and keep writing. Hope to get more content in future too.


  • Suresh Yadav

    Nice, thank you for this.


  • Frank

    FrankFrank

    Author Reply

    Good to read


Shares