资源地址:
其它使用教程:
Carthage只支持dynamic frameworks,也就是说只支持iOS8及以上的版本
安装:
1.通过homebrew安装 brew install carthage
2.下载.pkg
安装,地址
安装完成后,在终端输入carthage version
,可查看安装的版本
使用:
1.创建Cartfile
文件
Cartfile
文件的说明,参考官方文档
Cartfile
文件描述的是依赖,相当于Cocoapods的Podfile
切换到项目的文件夹,使用如下的命令创建一个Cartfile
文件:
touch Cartfile
使用Xcode打开来编辑:
open -a Xcode Cartfile
内容一般是如下的形式:
github "Alamofire/Alamofire" == 4.5
github "Alamofire/AlamofireImage" ~> 3.2
格式说明:
1.目前支持三种origin:GitHub、Git、二进制framework
2.依赖的版本
>= 1.0
for “at least version 1.0” 至少是1.0版本~> 1.0
for “compatible with version 1.0” 兼容1.0的版本== 1.0
for “exactly version 1.0”- “some-branch-or-tag-or-commit” for a specific Git object (anything allowed by git rev-parse). Note: This form of requirement is not supported for binary origins.
If you specify ~> 1.7.5, then any version from 1.7.5 up to, but not including 2.0, is considered compatible.
如果你指定的是~> 1.7.5
,那么可以是1.7.5
以上的任何版本,但不包括2.0
版本
构建依赖
在终端中输入如下的命令:
carthage update --platform iOS
该命令会clone carthage中指定的git仓库,并把每个依赖build成framework,终端中会有如下类似的输出
*** Fetching AlamofireImage
*** Fetching Alamofire
*** Checking out Alamofire at "4.5.0"
*** Checking out AlamofireImage at "3.2.0"
*** xcodebuild output can be found in /var/folders/cn/tknd724s0fv8pbdcbkg2sb6w0000gn/T/carthage-xcodebuild.no8ytB.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
*** Building scheme "AlamofireImage iOS" in AlamofireImage.xcworkspace
--platform iOS
确保只为iOS构建framework,如果你不指定一个平台,默认会为所有的平台(Mac 和 iOS)构建framework
之后,项目中会多了一个Carthage
文件夹,包含2个子文件夹:
当运行carthage update
命令后,Carthage创建了一系列的文件和文件夹,如下:
Cartfile.resolved
- 定义了Carthage安装依赖的版本,建议将该文件提交到版本控制存储库Carthage
- 包含2个文件夹
Build
- 构建好的framework,可以集成到项目中Checkouts
- 依赖的源码Don’t modify any code inside the Checkouts folder because its contents may be overwritten at any time by a future
carthage update
orcarthage checkout
command, and your hard work would be gone in the twinkling of an eye.
不要修改Checkouts文件夹中的代码,因为它可被carthage update
后者carthage checkout
命令所覆盖
If modifications to your dependencies are a must do, you can run
carthage update
using the --use-submodules
option.
如果必须修改依赖,使用-use-submodules
选项
With this option, Carthage adds each dependency in the Checkouts folder to your Git repository as a submodule, meaning you can change the dependencies’ source, and commit and push those changes elsewhere without fear of an overwrite.
注意:如果有其它用户要使用你的project,如果你没有在code中提交已构建好的framework,需要运行carthage bootstrap
命令
The
bootstrap
command will download and build the exact versions of your dependencies that are specified inCartfile.resolved
.
bootstrap
命令会下载Cartfile.resolved
中指定的版本的依赖
carthage update,
on the other hand, would update the project to use the newest compatible versions of each dependency, which may not be desirable.
carthage update
会使用最新的兼容的依赖
将Frameworks添加到项目中
选择project->target->General->Linked Frameworks and Libraries
将Build\iOS
文件下的framework,拖动到Linked Frameworks and Libraries
中:
之后切换到Build Phases
中,添加一个新的Run Script
/usr/local/bin/carthage copy-frameworks
在Input Files中,为每个framework添加一个entry
$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
$(SRCROOT)/Carthage/Build/iOS/AlamofireImage.framework
结果如下:
为什么这么做呢?
Strictly speaking, this build phase isn’t required for your project to run. However, it’s a slick workaround for an App Store submission bug where apps with frameworks that contain binary images for the iOS simulator are automatically rejected.
The
carthage copy-frameworks
command strips out these extra architectures. w00t!