一. 簡介
我之前用過 CocoaPods,很大一部分原因就是因?yàn)槁闊?,僅個(gè)人感覺,需要牽扯 XCode 項(xiàng)目文件;而 Carthage 就很好的解決了我之前的煩惱,輕耦合,更靈活;使用 xcodebuild工具來編譯依賴項(xiàng)目成二進(jìn)制 Framework,再引入到項(xiàng)目中去。
Carthage是由 Swift 語言寫的,只支持動(dòng)態(tài)框架,只支持 iOS8+。
**Carthage **的大致工作流程如下:
創(chuàng)建一個(gè) Cartfile
文件,寫好你要哪些依賴庫
執(zhí)行 carthage update
命令拉取源代碼并編譯為 Framework
把編譯后的 .framework
拖到項(xiàng)目中去即可 (官方是這么說的,不過更好的辦法是把 Carthage 編譯的 Framework 路徑加入到 Build Setting 中的 Framework Search Path,詳情見后文)
執(zhí)行 update
命令后,你的項(xiàng)目目錄結(jié)構(gòu)大致如下:

Cartfile 文件用來標(biāo)注你需要哪些依賴庫,對應(yīng)版本或者 Git 分支 (需要提交到 Git)
Cartfile.resolved 文件用來跟蹤項(xiàng)目當(dāng)前所用的依賴版本號(hào),為了保持多端開發(fā)一致 (需要提交到 Git)
Carthage 文件夾用來存放依賴庫的源文件和編譯后的文件 (不需要提交到 Git)
其實(shí)工作方式有點(diǎn)和 Cocoapods 大致相似,只不過 Cartfile 多了一個(gè)自動(dòng)編譯過程且與 Project 耦合更低
二. 安裝
推薦使用 Homebrew 進(jìn)行安裝,簡單方便,也便于維護(hù):
brew install carthage
安裝前最好先 update 一下,之前我就是因?yàn)闆]有 update,結(jié)果安裝了老版本的 Carthage
brew update
三. 使用
添加 Cartfile 文件
類似于 CocoaPods 中的 Podfile文件,把需要的包寫進(jìn)去就行了,具體可參閱官方說明,如:
#必須最低 2.3.1 版本
github "ReactiveCocoa/ReactiveCocoa" >= 2.3.1
# 必須 1.x 版本
github "Mantle/Mantle" ~> 1.0 # (大于或等于 1.0 ,小于 2.0)
# 必須 0.4.1 版本
github "jspahrsummers/libextobjc" == 0.4.1
# 使用最新的版本
github "jspahrsummers/xcconfigs"
# 使用一個(gè)私有項(xiàng)目,在 "development" 分支
git "https://enterprise.local/desktop/git-error-translations.git" "development"
暫只支持 GitHub 和 git 源,在執(zhí)行carthage update命令后會(huì)在根目錄創(chuàng)建一個(gè)Cartfile.resolved文件,這個(gè)文件是生成后的依賴關(guān)系,不能修改。
Carthage導(dǎo)入第三方時(shí)候會(huì)顯示編譯OSX、Mac、iOS等很耗時(shí),只選擇一種(目前沒試過)
carthage build --platform iOS
引入 Framework
在項(xiàng)目中引入依賴的 Framkework,只需要在對應(yīng) Target 中的 Build Setting 中的 Framework Search Path 項(xiàng)加入以下路徑,Xcode 便會(huì)自動(dòng)搜索目錄下的 Framework:
$(SRCROOT)/Carthage/Build/iOS
如果是 OSX 項(xiàng)目則把末尾的 iOS 改為 Mac
我試過不用下面的方式也是可以直接運(yùn)行成功,目前還在測試中
在項(xiàng)目中選中Target -> Build Phases -> Link Library with Librarie點(diǎn)擊+號(hào),然后點(diǎn)擊左下角的add Other.. 找到。framework添加就行

添加腳本 在 Build Phases -> +(左上角) -> New Run Script Phase 然后,點(diǎn)開Run Script 添加腳本
/usr/local/bin/carthage copy-frameworks
最后添加"Input Files”
$(SRCROOT)/Carthage/Build/iOS/Masonry.framework

添加的腳本的作用 在App Store提交通用二進(jìn)制觸發(fā)bug時(shí)這個(gè)腳本將運(yùn)行,保證在歸檔時(shí)必要的bitcode相關(guān)文件被拷貝。 This script works around an App Store submission bug triggered by universal binaries and ensures that necessary bitcode-related files are copied when archiving. 另外需要注意Carthage 支持OS X的各個(gè)版本,但是對于iOS只支持iOS 8及其以上 only officially supports dynamic frameworks. Dynamic frameworks can be used on any version of OS X, but only on iOS 8 or later.
在項(xiàng)目中使用第三方庫

在 Git 中忽略
如果不想把 Carthage 的依賴庫 push 到 Git 倉庫里,則修改 .gitignore 文件,增加忽略 Carthage 文件夾就行了:
四. 基本命令
archive: Archives a built framework into a zip that Carthage can use
bootstrap: Check out and build the project's dependencies
build: Build the project's dependencies
checkout: Check out the project's dependencies
copy-frameworks: In a Run Script build phase, copies each framework specified by a SCRIPT_INPUT_FILE environment variable into the built app bundle
fetch : Clones or fetches a Git repository ahead of time
help: Display general or command-specific help
update: Update and rebuild the project's dependencies
version: Display the current version of Carthage
有什么問題可以直接回復(fù)我