主页: | github.com/Carthage/Ca… |
---|---|
做者: | Justin Spahr-Summers等 |
版本: | 0.31 |
目标: | 用最简单的方式来管理Cocoa第三方框架 |
性质: | 第三方框架管理工具(相似于cocoapods) Carthage为用户管理第三方框架和依赖,但不会自动修改项目文件和生成配置,把对项目结构和设置的控制权交给用户。 |
原理 | 自动将第三方框架编程为Dynamic framework(动态库) |
限制 | 仅支持iOS8+。它只支持框架,因此不能用来针对iOS8之前的系统版本进行开发 |
直接下载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
复制代码
4 查看版本ruby
$ Carthage version
复制代码
$ cd 项目路径
复制代码
$ touch Cartfile
复制代码
GitHub库可在cartfile文件添加如下内容,指定GitHub的关键字:bash
github "AFNetworking/AFNetworking"
复制代码
或者填写其余git源,指定git关键词:服务器
git "https://github.com/AFNetworking/AFNetworking.git"
复制代码
$ Carthage update --platform iOS
复制代码
注: 不追加参数会编译出 iOS、OSX、tvos多个frameworkapp
注2: 若是失败的话,先用Xcode打开你的项目,Product > Scheme > Manage Schemes
在新窗口中,勾选上 Shared,点击 Close。在终端再次执行这个命令框架
$ open Carthage
复制代码
# 执行文件多出三个文件
Cartfile # 存放须要安装的依赖列表
Cartfile.resolved # 自动生成的依赖关系文件,需提交到git
# 确保提交的项目可使用彻底相同的配置与方式运行启用, 跟踪项目当前所用的依赖版本号
Carthage # 自动生成的Carthage目录 (不须要提交到 Git)
# 目录下有两个文件夹:Build Checkouts
# Build 存放编译后的文件,包括 iOS/Mac/tvOS/watchOS对应的framework
# Checkouts 存放从git拉取的依赖库源文件
复制代码
打开项目,点击Target -> Build Phases -> Link Library with Libraries
选择Carthage/Build
目录中要导入的framework
(该脚本文件保证在提交归档时会对相关文件和dSYMs进行复制)
(1)点击Build Phases,点击“+” -> New Run Script Phase
(2)添加添加脚本 /usr/local/bin/Carthage copy-frameworks
(3)添加"Input Files" $(SRCROOT)/Carthage/Build/iOS/AFNetworking.framework
#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的优势
CocoaPods在使用中会自动建立和更新workspace、依赖和Pod项目并进行整合;
Carthage在使用中不须要建立和集成相应的workspace和project,只须要依赖打包好的framework文件便可。
总结一下,CocoaPods的方法更容易使用,而Carthage更灵活且对项目没有侵入性。
CocoaPods相对来讲功能要比Carthage多不少,所以也更复杂,而CocoaPods配置简单项目干净。
CocoaPods有一个中心仓库,而Carthage是去中心化的,没有中心服务器也就避免了可能因中心节点错误而带来的失败,即Carthage每次配置和更新环境,只会去更新具体的库,时间更快。
Carthage 管理的依赖只需编译一次,项目干净编译时,不会再去从新编译依赖,节省时间
与 CocoaPods 无缝集成,一个项目能同时拥有 CocoaPods 和 Carthage
Carthage的不足
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"
复制代码
修改 .gitignore 文件,增长忽略 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 复制代码