[iOS開發]Carthage安裝和使用教程

一 Carthage簡單介紹

主頁: github.com/Carthage/Ca…
做者: Justin Spahr-Summers等
版本: 0.31
目標: 用最簡單的方式來管理Cocoa第三方框架
性質: 第三方框架管理工具(相似於cocoapods) Carthage爲用戶管理第三方框架和依賴,但不會自動修改項目文件和生成配置,把對項目結構和設置的控制權交給用戶。
原理 自動將第三方框架編程爲Dynamic framework(動態庫)
限制 僅支持iOS8+。它只支持框架,因此不能用來針對iOS8之前的系統版本進行開發

二 Carthage的安裝和使用

  • 直接下載Carthage.pkg安裝包,安裝運行 Carthage.pkg下載ios

  • 若是使用的XCode爲7.0+版本,那麼也可使用下面的方法來安裝git

1 安裝homebrew*github

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
複製代碼

2 升級brew編程

$ brew update
複製代碼

3 使用brew來安裝bootstrap

$ brew install Carthage
複製代碼

installCarthage.png

4 查看版本ruby

$ Carthage version
複製代碼

三 Carthage的使用

1 先進入到項目所在文件夾

$ cd 項目路徑
複製代碼

2 建立一個空的Carthage文件

$ touch Cartfile
複製代碼

3 編輯cartfile文件,例如要安裝AFN框架

GitHub庫可在cartfile文件添加如下內容,指定GitHub的關鍵字:bash

github  "AFNetworking/AFNetworking" 
複製代碼

或者填寫其餘git源,指定git關鍵詞:服務器

git "https://github.com/AFNetworking/AFNetworking.git"
複製代碼

4 保存並關閉cartfile文件,使用cartfile安裝框架

$ Carthage update --platform iOS
複製代碼

注: 不追加參數會編譯出 iOS、OSX、tvos多個frameworkapp

installAFN.png

注2: 若是失敗的話,先用Xcode打開你的項目,Product > Scheme > Manage Schemes 在新窗口中,勾選上 Shared,點擊 Close。在終端再次執行這個命令框架

Shared.png

5 打開Carthage 查看生成的文件目錄

$ open Carthage
複製代碼

Carthage文件夾目錄.png

  • 執行安裝依賴命令後的文件夾結構
# 執行文件多出三個文件

Cartfile # 存放須要安裝的依賴列表

Cartfile.resolved # 自動生成的依賴關係文件,需提交到git
# 確保提交的項目可使用徹底相同的配置與方式運行啓用, 跟蹤項目當前所用的依賴版本號

Carthage # 自動生成的Carthage目錄 (不須要提交到 Git)
# 目錄下有兩個文件夾:Build Checkouts
# Build 存放編譯後的文件,包括 iOS/Mac/tvOS/watchOS對應的framework
# Checkouts 存放從git拉取的依賴庫源文件
複製代碼

6 配置項目

打開項目,點擊Target -> Build Phases -> Link Library with Libraries選擇Carthage/Build目錄中要導入的framework

addFramework.png

7 添加編譯的腳本

(該腳本文件保證在提交歸檔時會對相關文件和dSYMs進行復制)

(1)點擊Build Phases,點擊「+」 -> New Run Script Phase

NewRunScriptPhase.png

(2)添加添加腳本 /usr/local/bin/Carthage copy-frameworks

(3)添加"Input Files" $(SRCROOT)/Carthage/Build/iOS/AFNetworking.framework

runScript.png

8 在項目中使用第三方庫

#import <AFNetworking/AFNetworking.h>

其它:

卸載Carthage:$ brew uninstall Carthage

更新第三方框架: 更新多個框架:修改Cartfile文件,並從新執行 $ carthage update 更新某個框架:$ carthage update 具體的框架名稱

carthage update  # 修改了Cartfile文件,並從新編譯
carthage update  Alamofire  # 僅更新Alamofire框架
carthage update --platform ios  # 僅編譯iOS平臺的framework
carthage bootstrap    # 從本地庫從新編譯依賴
複製代碼

四 Carthage優缺點

Carthage的優勢

  1. 使用了CocoaPods的項目是高度集成的,而Carthage更靈活強調儘量將任務委託給Xcode和Git。
  • CocoaPods在使用中會自動建立和更新workspace、依賴和Pod項目並進行整合;

  • Carthage在使用中不須要建立和集成相應的workspace和project,只須要依賴打包好的framework文件便可。

  • 總結一下,CocoaPods的方法更容易使用,而Carthage更靈活且對項目沒有侵入性。

  1. CocoaPods相對來講功能要比Carthage多不少,所以也更復雜,而CocoaPods配置簡單項目乾淨。

  2. CocoaPods有一箇中心倉庫,而Carthage是去中心化的,沒有中心服務器也就避免了可能因中心節點錯誤而帶來的失敗,即Carthage每次配置和更新環境,只會去更新具體的庫,時間更快。

  3. Carthage 管理的依賴只需編譯一次,項目乾淨編譯時,不會再去從新編譯依賴,節省時間

  4. 與 CocoaPods 無縫集成,一個項目能同時擁有 CocoaPods 和 Carthage

Carthage的不足

  • 僅支持 iOS8 +
  • 它只支持框架,因此不能用來針對 iOS 8 之前的系統版本進行開發
  • 支持的 Carthage 安裝的第三方框架和依賴不如 CocoaPods 豐富
  • 沒法在 Xcode 裏定位到第三方庫源碼

五 Carthage的工做過程說明

  • 1.建立一個Cartfile文件,在該文件中列出您想使用的框架
  • 2.運行Carthage,獲取並編譯Cartfile文件中列出的框架
  • 3.把框架的二進制文件配置到項目中

六 關於版本指定

Carthage 支持如下幾種版本指定方法:

>= 1.0 表明 「最低 1.0版本」
~> 1.0 表明 「表示使用版本1.0以上可是低於2.0的最新版本,如1.5, 1.9」
== 1.0 表明 「必須是 1.0 版本」
複製代碼

"some-branch-or-tag-or-commit"指定一個 Git 對象 (任何被 git rev-parse 容許的) 若是沒有版本要求,任何版本的依賴是容許的。

版本好的兼容性是根據語語義化版本控制決定的。這意味着任何大於或等於1.5.1版本,但小於2.0,將認爲與1.5.1「兼容」。

Cartfile示例

# Require version 2.3.1 or later 最低2.3.1版本
github "ReactiveCocoa/ReactiveCocoa" >= 2.3.1

# Require version 1.x 必須1.x版本
github "Mantle/Mantle" ~> 1.0    # (大於或等於 1.0 ,小於 2.0)

# Require exactly version 0.4.1 必須0.4.1版本
github "jspahrsummers/libextobjc" == 0.4.1

# Use the latest version 使用最新版本
github "jspahrsummers/xcconfigs"

# Use the branch 使用git分支
github "jspahrsummers/xcconfigs" "branch"

# Use a project from GitHub Enterprise 使用一個企業項目,在 "development" 分支
github "https://enterprise.local/ghe/desktop/git-error-translations"

# Use a project from any arbitrary server, on the "development" branch 使用一個私有項目,在 "development" 分支
git "https://enterprise.local/desktop/git-error-translations2.git" "development"

# Use a local project 使用一個本地的項目
git "file:///directory/to/project" "branch"
複製代碼

七 Git 中忽略不須要提交到版本庫的文件與文件夾

修改 .gitignore 文件,增長忽略 Carthage 文件夾就好了:

#Carthage
Carthage
複製代碼

八 Carthage的其餘命令

archive           Archives built frameworks 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 outdated Check for compatible updates to the project's dependencies
 update            Update and rebuild the project's dependencies version Display the current version of Carthage 複製代碼
相關文章
相關標籤/搜索