An Introduction to Cocoapods

Introduction

So my job is making me learn Swift as part of a new project I am supporting. This is fine. One of the many aspects of this job is the use of CocoaPods. Google Developer's put together a series of quick videos that give a nice overview of CocoaPods. I have gathered together into a YouTube playlist below to make this cliff notes review. Enjoy.

Why CocoaPods?

  1. The old way of using third party libraries?
  2. Download and unzip files
  3. Drag frameworks and bundles to your project
  4. Link with new iOS libraries
  5. Add linker flags
  6. Go to step 1 if the 3rd party library also uses its own libraries

What a pain!

The Solution CocoaPods

This provides developers writing code for iOS the ability to publish and bring in third party libraries via a Podspec file. This provides the definition of how the library will install into your application. A consumer of a CocoaPods library will write a Podfile that identifies what third party libraries (a.k.a. pods) your application will consume.

Installing CocoaPods

Now the video identifies using Ruby Gem but people run into problems. It is recommended to homebrew. Follow the steps below to setup CocoaPods.

  1. Open the terminal program on your mac
  2. Navigate to Homebrew in your web browser and follow the instructions to install Homebrew
  3. In your terminal execute the command brew install cocoapod. This installs cocoapods onto your system.
  4. In your terminal execute the command pod setup. This will download every single Podspec file.

Adding a Library via CocoaPods

  1. You can either type "pod init" within your application or execute "touch Podfile" to start with an empty file
  2. Edit the Podfile to identify the deployment "target: platform :ios, '7.0'". This is the first line where platform is the keyword, after the ':' is the target, and after that is the minimum version.
  3. Now to search for a library you can either execute "pod search [library_name]" or search from the CocoaPods website
  4. To add the pod to your Podfile type "pod '[library_name]'" or "pod '[library_name]', '~> [ver]'" in your Podfile
  5. To install the pods from your Podfile, type "pod install" in the terminal

Podfile Example from Video

platform :ios, '7.0'
pod 'AFNetworking'
pod 'GoogleAnalytics-iOS-SDK', '~> 3.10'

Podfile Version Updating

Semantic Versioning

This provides information about the pod (a.k.a. library) you are adding to your application via the Podfile. The definition is as follows (NOTE: this is a general rule but is not enforced by CocoaPods)

Given:

X.Y.Z
| | > Patch
| > Minor Release
> Major Release

Where:

  • Patch: Bug fix to library that should not break backwards compatibility
  • Minor Release: When a new feature is added to the library. Should not break backwards compatibility
  • Major Release: A new feature that does break backwards compatibility

Squiggly Arrow

So what this dictates is it tells CocoaPods what version you are comfortable letting it update the library for you based on the last version number specified (a.k.a patch, minor, or major). As a general rule you should probably only allow CocoaPods upgrade between Patch and Minor releases but with any upgrade, something can break so make sure to test the upgrade before you release your app.

You can also use other operations besides to squiggly arrow to more tightly control your applications library usages. Examples are:

  • '= [version]'
  • '> [version]'
  • '>= [version]'
  • '< [version]'
  • '<= [version]'

Wrapping Up Information

Podfile Lockfile

These files represent the exact version of libraries installed via Cocoapods. It allows for synchronization between teams and when checked in, allows for rolling back and determining if a library introduces a breaking change into your code.

Pod Try

The command pod try [library_name] will run a demo or sample application if it is available. This allows you to try a library before adding it to your library.

Conclusion

This provides a nice cliff notes version for how to utilize CocoaPods to utilize 3rd party libraries in your iOS application. This seems to be the standard as of 2023 but Swift has released the Swift Program Manager which is competition and released by Apple. So something to be aware of when writing or maintaining your iOS application. Was this helpful? Comment below.

References

Popular posts from this blog

Everything You Forgot About in Algebra One