Carthage安装使用教程

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/14084080.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Carthage和CocoaPods相比,轻量级一些。Carthage 使用于 Swift 语言编写,只支持动态框架,只支持 iOS8+的Cocoa依赖管理工具。

与现在流行的 CocoaPods 不同,Carthage编译你的依赖,并提供框架的二进制.framework文件,但你仍然保留对项目的结构和设置的完整控制,Carthage不会自动的修改你的项目文件或编译设置。是一个去中心化的Cocoa依赖管理工具

1.brew

安装carthage之前需要安装brew,进入官网(https://brew.sh/index_zh-cn.html) 有详细的安装步骤。

安装 Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

或打开终端Terminal,输入以下命令:

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

获取最新版本

brew update
  • 注意:如果遇到Error: The /usr/local directory is not writable.错误,就执行以下命令sudo chown -R $(whoami):admin /usr/local,再更新。
其他brew命令
brew install git      // 使用brew安装软件

brew uninstall wget  // 使用brew卸载软件

brew search /wge*/ // 使用brew查询软件,其中/wge*/是个正则表达式,需要包含在/中

brew list    // 列出已安装的软件

brew home // 用浏览器打开brew的官方网站

brew info  // 显示软件信息

brew deps // 显示包依赖

2.安装Carthage

安装brew之后,就可以安装Carthage了。

brew install carthage

安装Carthage之后,查看版本

carthage version   //目前的版本号为0.15.2

3. 使用Carthage

进入项目所在文件夹
cd ~/路径/项目文件夹
创建一个空的carthage文件
touch Cartfile
此时项目文件夹中会有一个名字为Carthage的文件
使用Xcode打开该文件
open -a Xcode Cartfile
编辑Cartfile
github "SVProgressHUD/SVProgressHUD" ~> 1.0
Cartfile格式说明
依赖源 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

branch名称 / tag名称 / commit名称,意思是使用特定的分支/标签/提交,比如可以是分支名master,也可以是提交5c8a74a。

4. 运行Carthage

保存并关闭Cartfile文件,回到终端,执行以下命令:
carthage update --platform iOS
carthage会clone文件中对应的git第三方库,把每一个第三方库编译成二进制文件的framework文件。

  • 其中--platform iOS命令是可选的,作用是保证只为iOS编译framework,如果不指定平台,会为全平台编译framework文件。如果想要了解更多的命令,可以运行carthage help update查看。

当命令执行完毕,在Cartfile文件同级别的文件夹中生成一个名为“Carthage”文件夹和“Cartfile.resolved”文件。打开Carthage文件夹,可以看到两个文件夹Build和Checkouts。

Cartfile.resolved:这个文件是辅助Cartfile的,需要被提交到版本库中,它有助于其他开发者使用和你相同版本的第三方库。
Build:包含每一个第三方库创建生成的framework,可以被集成到项目中,每一个framework都是依赖于源文件或者GitHub上的Releases版本。
Checkouts:这里包含的是转换成framework之前的源文件,Carthage有自己的缓存机制,所以不需要在不同的项目中对同一个的第三方库clone多次。

对于是否把BuildCheckouts文件夹提交到版本库取决于你,这不是必须的。如果提交的话,其他人clone了你的资料库就可以使用这两个文件中的内容。

不要改变Checkouts文件夹中的内容,因为如果使用carthage update 或者 carthage checkout命令的话,这个文件夹中的内容可以随时被复写,那么改动工作就白费了。如果一定要改动的话,在使用carthage update命令时,可以使用--use-submodules选项。如果加上这个选项的话,Carthage在添加每个依赖库的时候就会作为一个字模块。

如果其他人想要使用你的工程,你不需要在你的代码中提交已经编译好的framework,他们需要在check out你的工程之后执行carthage bootstrap命令。

bootstrap命令会根据Cartfile.resolved文件下载和编译依赖库的精确版本。另一方面,carthage update命令会更新项目中的第三方库的最新的编译版本,这是不可取的。

"Carthage/Build/iOS"文件夹中会生成.framework文件。

 
image.png
  • 注意, 如果遇到如下问题,解决方法如下:
*** Skipped downloading Alamofire.framework binary due to the error:

 "Bad credentials"

这个错误的原因是carthage通过git向 Github API 发送请求时,要先取得GitHub凭证。

解决办法一

打开命令行工具terminal

1.检查git和osxkeychain helper是否已经安装
$ git credential-osxkeychain

如果出现以下提示说明已经安装,进入第3步

Usage: git credential-osxkeychain <get|store|erase>

如果出现以下提示说明未安装,进入第2步

xcode-select: note: no developer tools were found at '/Applications/Xcode.app',requesting install. Choose an option in the dialog to download the command line developer tools.
2.如果第一步提示未安装,则使用Homebrew来安装git。

$ brew install git

3.告诉git通过全局的credential.helper的配置来使用osxkeychain helper。

$ git config --global credential.helper osxkeychain

  • Set git to use the osxkeychain credential helper
4.通过HTTPS URL方式 clone 在github上一份代码下来,这个过程你有可能被提示输入GitHub的用户名和密码来授权访问OSX keychain,此时,你的用户名和密码就会被保存在keychain中了。

可以进入系统桌面,创建一个临时文件夹,用来clone代码,之后再删除即可

$ cd

$ cd Desktop

$ mkdir tempdir

$ cd tempdir

$ git clone https://github.com/SVProgressHUD/SVProgressHUD.git
5.再次进入项目所在目录,执行carthage update命令,就可以看到问题解决了。

GitHub名称密码已更改,需要更新OSX Keychain

https://help.github.com/articles/updating-credentials-from-the-osx-keychain/

解决办法二

5. 添加FrameWorks到项目中

点击"项目名称"->"target" -> "Gerneral",在最底部找到"Linked Frameworks and Libraries"

打开Carthage文件夹,进入BuildiOS,拖拽SVProgressHUD.framework到Xcode的Linked Frameworks and Libraries 中。

 
image.png

目的是告诉Xcode链接你的app到这个framework,允许你在代码中使用。

下一步选择菜单上的Build Phases,并添加一个新的Run Script,并添加以下命令:

/usr/local/bin/carthage copy-frameworks

点击Input Files下面的+号为每一个framework添加条目。

$(SRCROOT)/Carthage/Build/iOS/SVProgressHUD.framework

严格来讲,build phase对项目运行来说不是必须的,但是,这巧妙的解决了APP因为使用的frameworks包含二进制图像的iOS模拟器在提交APP Store时会被自动拒绝的问题。

carthage copy-frameworks命令剔除了额外的框架。

 
image.png

command + B编译项目,比已成功之后就可以使用了。

import SVProgressHUD

升级指定Frameworks

carthage update SVProgressHUD --platform iOS
  • 参考

参考1:https://github.com/Carthage/Carthage

参考2:https://www.raywenderlich.com/109330/carthage-tutorial-getting-started

  • 在新的工程中添加Carthage

In a Terminal, cd to the root directory of the project that you want to add the framework to. This should be the same directory as your *.xcodeproj file.

cd ~/Path/To/Your/Project/
Create an empty file called Cartfile like this:
touch Cartfile
Open that file with Xcode:
open -a Xcode Cartfile
Paste the framework info that you need into the Cartfile. For example:
github "stephencelis/SQLite.swift" ~> 0.10.1
Close the file in Xcode and in Terminal run the following command to make Carthage update the dependencies in your project. (You should still be in the same directory as your Cartfile.)
carthage update --platform iOS


作者:chjxidian
链接:https://www.jianshu.com/p/72db5849f351
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/strengthen/p/14084080.html