1、Carthage简单概念介绍以及与Cocopods的区别:github地址 参考博文地址
- Carthage 类似于 CocoaPods,为用户管理第三方框架和依赖,但不会自动修改项目文件和生成配置
- Carthage 是去中心化的依赖管理工具,安装依赖时不需要去中心仓库获取 CocoaPods 所有依赖的索引,节省时间
- 对项目无侵入性,Carthage 设计上也比较简单,利用的都是 Xcode 自身的功能,开发者在创建依赖时,相比 CocoaPods 也简单许多
- Carthage 管理的依赖只需编译一次,项目干净编译时,不会再去重新编译依赖,节省时间,大大增加了Achive和Build的时间
- 是由Swift语言编写的,自动将第三方框架编程为 Dynamic framework( 动态库 )
- 与 CocoaPods 无缝集成,一个项目能同时拥有 CocoaPods 和 Carthage
- 缺点:
- 仅支持 iOS8 +
- 它只支持Framework,所以不能用来针对 iOS 8 以前的系统版本进行开发
- 支持的 Carthage 安装的第三方框架和依赖不如 CocoaPods 丰富
- 无法在 Xcode 里定位到源码,只可以看到一个库的头文件
- 安装包的大小比用CocoaPods安装的包大
- 每次update时,需要从git仓库中重新拉取下载,比较耗时
- CocoaPods是现在主流的 iOS/Mac 的包管理工具
- 使用方便,除编写 Podfile 以外其他几乎都是自动完成
- 软件包数量多,主流支持;
- 缺点:
- 每次更新环境都需要连接到中心仓库,比较耗时
- 繁琐的维护过程
- 每次编译都会把所有第三方库都重新编译一次
2、Carthage的安装和使用
- 使用homebrew进行安装
brew install carthage
- 进入项目所在文件夹
cd ~/路径/项目文件夹
- 创建一个空的Cartfile文件
touch Cartfile
- 编辑Cartfile(标注需要哪些依赖库对应版本或者Git分支)
1、方法一:打开Xcode并编辑
open -a Xcode Cartfile
2、方法二:vim编辑,输入I进入编辑模式,按esc离开,并输入:wq保存)
vim Cartfile
github "texturegroup/texture"
-
- 依赖源 Dependency origin
Carthage支持两种类型的源,一个是github,另一个是git。
github 表示依赖源,告诉Carthage去哪里下载文件。依赖源之后跟上要下载的库,格式为Username/ProjectName
git 关键字后面跟的是资料库的地址,可以是远程的URL地址,使用git://, http://, ssh://,或者是本地资料库地址。
-
- 依赖版本号 Dependency Version
告诉Carthage使用哪个版本,这是可选的,不写默认使用最新版本
== 1.0 表示使用1.0版本
>= 1.0 表示使用1.0或更高的版本
~> 1.0 表示使用版本1.0以上但是低于2.0的最新版本,如1.2,1.6
- 运行Carthage
carthage update --platform iOS
TF013550:trunk name$ carthage update --platform ios
*** Fetching texture
*** Cloning SwiftyJSON
*** Cloning ObjectMapper
*** Fetching SDWebImage
*** Fetching PINCache
*** Cloning libwebp
*** Fetching PINOperation
*** Cloning libwebp
*** Checking out PINRemoteImage at "3.0.0-beta.6"
*** Checking out SDWebImage at "4.3.3"
*** Checking out ObjectMapper at "3.1.0"
*** Checking out SwiftyJSON at "4.0.0"
*** Checking out PINCache at "3.0.1-beta.2"
*** Checking out texture at "2.3.1"
*** xcodebuild output can be found in /var/folders/xh/0p311s3j6rl5nmm29_brfc340000gn/T/carthage-xcodebuild.W3tEmd.log
说明:
1、carthage会clone/fetch文件中对应的git第三方库,把每一个第三方库编译成二进制文件的framework文件。
2、--platform iOS命令是可选的,目前每次更新时建议加上,作用是保证只为iOS编译framework,如果不指定平台,会为全平台编译framework文件。
3、当命令执行完毕,在Cartfile文件同级别的文件夹中生成一个名为“Carthage”文件夹和“Cartfile.resolved”文件。打开Carthage文件夹,可以看到两个文件夹Build和Checkouts。
4、Cartfile.resolved 文件用来跟踪项目当前所用的依赖版本号,为了保持多端开发一致
- 查看carthage更多指令
carthage help
TF013550:trunk name$ carthage help
Available commands:
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
升级指定的FrameWork:
carthage update SVProgressHUD --platform iOS
- 添加Framework到项目中
1、添加到“Linked Frameworks and Libraries”,点击加号,从/Carthage/Build/iOS文件夹中选择Framework添加
2、添加一个新的“Run Script”,并添加命令
说明:carthage copy-frameworks 命令剔除了额外的框架
/usr/local/bin/carthage copy-frameworks
![]()
点击Input Files下面的+号为每一个framework添加条目
build phase对项目运行来说不是必须的,但是,这个配置解决了APP因为使用的frameworks包含二进制图像的iOS模拟器在提交APP Store时会被自动拒绝的问题。$(SRCROOT)/Carthage/Build/iOS/MJRefresh.framework
3、Carthage安装过程中的报错(持续更新中......)
- 错误一:在执行update时报错如下:报错解决办法参考链接:
TF013550:trunk name$ carthage update --platform ios
*** Fetching texture
*** Cloning WQPhotoAlbum
*** Cloning SDWebImage
A shell task (/usr/bin/env git clone --bare --quiet https://github.com/rs/SDWebImage.git /Users/yuge/Library/Caches/org.carthage.CarthageKit/dependencies/SDWebImage) failed with exit code 128:
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
1、打印当前xcode的路径:
xcode-select --print-path
2、输入一下命令:(如果有多个xcode版本路径 请选择其中一个)
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
TF013550:trunk name$ xcode-select --print-path
/Applications/Xcode.app/Contents/Developer
TF013550:trunk name$ sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
Password:
TF013550:trunk name$ gem update --system
- 错误二:执行carthage build --platform ios报错如下:
** Skipped downloading ObjectMapper.framework binary due to the error:
"API rate limit exceeded for 115.236.71.65. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"