Carthage

本文详细介绍了Carthage的安装和使用方法,包括通过Homebrew安装、创建Cartfile文件、构建依赖、以及如何将构建好的framework添加到项目中。特别强调了Carthage仅支持iOS8及以上版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Carthage

资源地址:

其它使用教程:

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. >= 1.0 for “at least version 1.0” 至少是1.0版本
  2. ~> 1.0 for “compatible with version 1.0” 兼容1.0的版本
  3. == 1.0 for “exactly version 1.0”
  4. “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个子文件夹:

  • Build
  • Checkouts

build
checkout

当运行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 or carthage 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 in Cartfile.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中:

gif
之后切换到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

结果如下:

Run Script

为什么这么做呢?

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值