Dependency Management in iOS with Carthage

Datetime:2016-08-23 01:38:02          Topic:          Share

Carthage was the capital city of the ancient Carthaginian civilization, situated on the eastern side of the Lake of Tunis in what is now Tunisia. The city developed from a Phoenician colony into the center of an empire dominating the Mediterranean during the first millennium BC.

As interesting a topic as this might be, this article will not cover ancient history, but a dependency management for iOS.

Carthage helps you install dependencies for your project and dependencies of those dependencies. If you install package A, that package may depend on package B, which may depend on package C. If you install package D it may depend on package C, but on a different version. Finding the appropriate version is a job that package managers handle for you.

Packages in iOS are ‘frameworks’, and there are two types, dynamic and static. Carthage only supports dynamic frameworks which arrived in iOS 8.

Installation

You can install Carthage on OS X with Homebrew or an installer package.

With Homebrew:

brew update
brew install carthage

If you don’t have Homebrew, download the installer from Carthage’s release page .

Setting up a New Project

Setting up Carthage with your project is simple. Start a new iOS project and create a file named Cartfile in the root directory of the project. Open that file and add:

github "Alamofire/Alamofire" ~> 3.4

In this example I have used Alamofire as a dependency to my project, with github as the source of the package. Carthage supports github and other git based sources. For a package hosted on another git repo, prefix the sentence with git instead of github . By the way, Carthage was created by a group of developers from Github.

To install or update the packages run carthage update and wait for Carthage to download and build the packages.

In the Carthage/Build folder, Carthage builds a framework for every ‘deploy’ target it supports and Alamofire.framework is the file you need for the project.

Link the package with the project by using the Linked Frameworks and Libraries pane in the project settings, click the plus button, and select Add Other to add Alamofire.

You are now ready to use Alamofire in your project, and follow the sma eprocess for any other package that supports Carthage.

Carthage vs Cocoa Pods

It’s likely that you have used CocoaPods for one of your XCode based projects, and it works well. Why does XCode need another dependency manager?

There’s no harm in multiple software projects doing the same thing. Options can act as a fail safe if one packaging manager fails. CocoaPods has had it’s own problems with github in the past .

Carthage is a decentralized dependency manager and CocoaPods is centralized. With CocoaPods you can search for packages on their site , but you cant with Carthage, where Github will serve as the main way of finding packages. This is a disadvantage to some, but this way, Carthage can’t remove packages itself.

Carthage is written in Swift and CocoaPods in Ruby, making it a more ‘native’ project.

Empirical Choice?

In this article, I introduced you to Carthage and showed how to install it, how to use it, and the main differences with CocoaPods. If you want to know more about Carthage, I recommend you read their docs .

In a future article I will cover how to make your own packages ‘Carthage-compatible’, but in the meantime I would love to hear your experiences using it in the comments below .