What is CocoaPods?
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 48 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.
CocoaPods就是一个依赖/包管理工具,用来发布、分享可复用的代码(库、框架等)。类似RubyGems for Ruby, Composer for PHP, NPM for NodeJS, Gradle for Java.
上面的依赖管理都是出自官方,苹果公司没有针对Object-c和Swift语言出具官方的依赖管理解决方案,所以才有了Cocoapods 和 Carthage 这样的第三方依赖管理工具。
至于Cocoapods 和 Carthage 有什么区别以及如何选择,请参考该篇博文:Carthage or CocoaPods: That is the question
在Mac 10.13.5上安装Pods
新版Mac自带了Ruby,所以不需要额外配置安装运行环境。
Step1
执行下面的命令查看Ruby版本以及更新Ruby环境。
ruby --version
gem update --system
Step2
由于官方自带的源访问经常出错而且比较慢,所以先进行源的替换
//删除官方自带的源
gem sources --remove https://rubygems.org/
// 添加国内的源
gem sources -a https://gems.ruby-china.org
//此处注意添加的国内源去除最后的/否则会报错如下:
/*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 */
//检查替换
gem sources -l
Step3
执行安装命令以及常见报错处理
sudo gem install cocoapods
/*执行上述命令安装cocoapods的时候显示没有权限:
You don't have write permissions for the /Library/Ruby/Gems......
*/
//请使用命令
sudo gem install -n /usr/local/bin cocoapods
Step4
配置CocoaPods
pod setup
/* 报错如下
Cloning into ‘master’…
error: RPC failed; result=18, HTTP code = 200
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
*/
//这个是因为从github上面克隆资源到本地,资源太大了,超过的git限制的大小,使用以下语句把默认的限制变大即可(52428000=500×1024×1024,即500M)
git config http.postBuffer 524288000
git config --list
Step5
安装配置完成,使用pod search命令搜索库验证pod是否能够正常使用:
pod search Masonry