使用Carthage管理iOS第三方库

最近在研究Swift,一不小心发现一个好的的管理iOS第三方库Carthage,就跟第一次使用CocoaPods时一样兴奋不已,在研究了大半天后终于能用了,使用起来还是很方便的,比起CocoaPods有不足也有优点,两个配合起来用也是很爽的^ - ^

初识Carthage

最近在用Swift3.0做项目,想导入一些支持Swift3.0的第三方库来使用,就想到Alamofire,看README时发现了一个新的名词Carthage,What?,这是个什么东西,居然也可以用它来管理第三方库,之前都没见到过,赶紧百度了一下补充了下知识:

Carthage:迦太基(地中海地区古城),好吧这是百度百科的解释,至于为什么用这个单词,我也不太清楚,可能比较屌吧,看下图你就知道它是用来干什么的了:

这里写图片描述

Carthage的目标是用最简单的方式来管理Cocoa第三方框架

是的,你没看错,它可以用来管理第三方库Alamofire,然后查了一下,还有很多第三方库都可以用它来管理,跟CocoaPods一样可以用来管理第三方库,看了一下它的崛起时间大概是iOS8之后Swift出来的时候,与现在流行的 CocoaPods 不同,它是一个去中心化的解决方案。CocoaPods的方法更易于使用,但Carthage更灵活并且是非侵入性的。

大家都知道Swift是支持动态库的,所以Carthage应势而生,它没有总项目的列表,这能够减少维护工作并且避免任何中心化带来的问题)。不过,这样也有一些缺点,就是想修改第三方库源码的话很费劲,还需要重新编译打包。

安装和使用Carthage

首先到github上Download或者Clone Git 或者使用HomeBrew(没有安装的可以自行百度去装一个)安装~

<code class="language-shell hljs cmake has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">brew update <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#先更新</span>
brew <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">install</span> carthage <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#安装Carthage</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

当你安装好Carthage后,在终端执行来判断是否安装成功:

<code class="language-shell hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">carthage <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">version</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

即可看到当前的Carthage版本, 当我写这篇文章的时候,Carthage的版本为0.18(还是很年轻的嘛) 。

现在,你已经安装好了Carthage,接下来就是在你的项目中使用carthage了:

<code class="language-shell hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">vim Cartfile <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#创建一个叫Cartfile的文件</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

输入你想要编译成Framework的Swift第三方库,比如说SnapKitAlamofire,如下所示:

<code class="language-shell hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">github <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Alamofire/Alamofire"</span> ~> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.0</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#支持Swift3.0</span>
github <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SnapKit/SnapKit"</span> ~> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.0</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#支持Swift3.0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

github代表源,而后面的就是用户名和版本库的名字,而”~>” 后面是版本号哈!

保存退出,执行:

<code class="language-shell hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">carthage update --platform iOS <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#因为是打包iOS的所以后面加iOS,有的库支持mac、watch的话会打几个包</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

之后会发现在跟目录下多出了Carthage文件夹和Cartfile.resolved文件。进入Carthage/Checkouts下可以看到刚刚得到的AlamofireSnapKit,如下图所示:

这里写图片描述现在打开你的项目,点击project,选择target, 再选择上方的General,将需要的framework文件拖到 Linked frameworks and Binaries内:

这里写图片描述

然后这是很重要的一步,点击Build Phrase tab选项,添加相应的run script

<code class="language-shell hljs applescript has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">/usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span>/bin/carthage <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">copy</span>-frameworks
$(SRCROOT)/Carthage/Build/iOS/你的framework</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

这里写图片描述

然后就可以去项目里面import你得第三方库了~~可以愉快的使用了,不过有一点要吐槽一下`build的速度真是有点慢- . -

CocoaPods的优势与劣势

优势

  • 使用方便,除编写 Podfile 以外其他几乎都是自动完成;
  • 软件包数量多,主流支持;
  • 支持 iOS 8 Framework,当然也支持旧的静态编译;
  • 开发者使用比较简单,但是如果创建兼容 CocoaPods 的库,就会相对繁琐一些(尽管有了命令行)~

劣势

  • 每次clean后都得重新编译,编译比较耗时,Archive也会变慢;
  • 每次更新环境都需要连接到中心仓库,比较耗时~

Carthage的优势与劣势

优势

  • build一次后就能在项目里面使用,增加编译速度;
  • 可以与CocoaPods无缝集成;
  • 它是去中心化的,没有中心服务器,这意味着每次配置和更新环境,只会去更新具体的库,而不会有一个向中心服务器获取最新库的索引这么个过程,如此一来,又省了很多时间;
  • 不会影响你的项目配置的,相对CocoaPods而言更加独立,Carthage 使用的是一种非侵入性的哲学;

劣势

  • 库依然不如 CocoaPods 丰富:尽管很多库不需要声明并改造就直接可以被 Carthage 用,但依然有大量 CocoaPods 能用的库不支持,我相信时间能解决这个问题;
  • 只支持 Framework,所以是 iOS 8 Only 了,随着时间推移,这个也不会是问题;
  • 无法在 Xcode 里定位到源码:如果你在写代码过程中,想跳转到一个第三方库去看具体的实现,这是无法办到的,Carthage 的配置只能让你看到一个库的头文件~

总结

我觉得不管怎样,好不好用,做为iOS程序员我们都应该去尝试的了解一下它,通过它去学到更多的东西,至于什么第三方库用CocoaPods,什么去用Carthage,我觉得一些比较稳定大的库可以用Carthage,而一些变动比较大,自己还有可能去修改的库我们肯定首选CocoaPods。最后希望大家看到这篇博客能学到东西,大家有问题可以提出来,谢谢大家的阅读~~

参考链接

http://www.jianshu.com/p/5ccde5f22a17

https://imtx.me/archives/1939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值