How to manage iOS dependency libraries using Carthage

How to manage iOS dependency libraries using Carthage


Many IOS developers are probably no stranger to CocoaPods, we almost have been used to add a line to a library into your project in the pod file, have to say, in the IOS development in the history of CocoaPods contributed, it will occupy half of the country in the development of even in the future. But I’m here to introduce a new third party library management tool – Carthage, whether you are a rookie or veteran, all hope that this article can open a new world for you.

Many people will ask, CocoaPods with their own artistic habit, and did not find what is wrong, why should you use Carthage? So, in order to unlock your queries, I will first introduce the differences between the two third party library management tools, so that we can have a preliminary understanding of the new rising star.

Carthage said, we must first say swift, in 2014, Apple released the initial swift, swift prepared by the third party libraries can only drag the source code to your own project called, because at that time the only scanty open source libraries, and can not be packaged with swift1.0 is not so good, many people hold the wait-and-see attitude to swift. As swift gradually stabilized, and began to write their own swift support will be packaged into a framework from the ios8.0, more and more pure swift framework began to appear at this time, Carthage suddenly appeared in people’s field of vision, and has continued to open source framework almost all started to support carthage.

Carthage GitHub page actually for all questions have a detailed answer, a few friends English not too good, here I use Chinese to introduce Carthage and CocoaPods different:

First, CocoaPods will create and modify the configuration of the workspace project, all for the sake of convenience, we only need to modify the pod file does not need too much care about other things, CocoaPods created a highly integrated project. Carthage is characterized by flexibility and low coupling, so it doesn’t need to integrate the corresponding project when it is integrated. It doesn’t need to create workspace, but only need to depend on the packaged framework file.

Second, CocoaPods function is relatively much more than Carthage, in the country due to the wall, we have changed the Taobao source to update the CocoaPods, if you don’t believe me, over the wall, a lot of things or not, the update is not down, the wrong version and a series of reasons will let you have to give up some it looks very good for third party libraries. It seems that Carthage only need to download the project can be from GitHub, configuration is simple, the use of the project is clean, as good third party libraries like Apple’s native framework of all, not since you no longer need to take the new CocoaPods library, do not have to spend a lot of time to fix problems with CocoaPods package. If you use CocoaPods when you start with Carthage, you will fall in love with this tool.

Install, use Carthage

Well, said so much, I began to introduce how to use Carthage.

To install Carthage, simply open the terminal and enter the following instructions:

$brew update $brew install Carthage

If you don’t like using the terminal, you can also download the latest version of Carthage.pkg from the site to update it.

When you install the Carthage, execute it at the terminal

$Carthage version

You can see the current version of Carthage, and when I write this article, the version of Carthage is 0.11.0.

Now that you’ve installed Carthage, then you’ll use Carthage in your project:

1, through the terminal to enter the folder where the project is located:

$CD ~/Path/Project

The path behind is replaced with the path in which your project is located

2, create an empty Carthage file:

$touch Cartfile

At this point in your project folder, you create a file called Cartfile.

3. Open the cartfile file using Xcode

$open -a Xcode Cartfile

And add the following in the open file

GitHub “Alamofire/Alamofire” ~> 3,
, GitHub, “SwiftyJSON/SwiftyJSON”,

Version meaning

~> 3 said that using version 3 or more but less than 4 of the latest version, such as 3.5, 3.9 = = 3 that use the 3 version of > = 3 means use 3 or higher version if you don’t specify the version number, it will automatically use the latest version

4, save and close the cart file file and execute the command at the terminal

$Carthage update –platform iOS

Carthage will download and compile the third party libraries you need. When the command is complete, you will create a folder named Carthage in your project folder

In ~/Carthage/Build/iOS, the xxx.framework file has been created for you.

Of course, you can also enter this folder through the command line:

$open Carthage

5, now open your project, click project, select target, and then select the top of the General, drag the required framework file to Linked frameworks and Binaries

How to manage iOS dependency libraries using Carthage

6, click the Build Phrase tab option to add the corresponding run script

How to manage iOS dependency libraries using Carthage

(thanks @TommyYaphetS for pointing out the new version of the problem. If the article is wrong or you have any questions when you use it, please leave a message for me)

If you are using a version after 0.11, you might be wrong:

/bin/sh, failed, with, exit, code, 1,

You need to configure it in the following way:

How to manage iOS dependency libraries using Carthage

At this point, you can already use the imported third party Library

If you need to use Alamofire and SwiftyJSON, add them to the appropriate class

Import Alamofire import SwiftyJSON


Now began to enjoy Carthage.

If you have any questions, you can send me an email:

Also recommended pure swift coredata library I developed, has supported the use of Carthage, love words to every one.