前言
这篇记录,是我在几次安装Cocoapods中的记录,中间很多都是借鉴和查阅的网友的文章,如有侵权的地方,请各位留言或者私信删除侵权内容。
需要说明的还有,此记录是我在几次安装过程,遇到的问题的记录,不一定适合所有的人,如有朋友遇到了其他问题,也欢迎留言或者私信交流。
此记录,参考的部分链接如下:
https://my.oschina.net/w11h22j33/blog/206129
一、 安装
1. 安装前的说明
1)所有的操作均在终端中运行
2)操作过程中,需要用到超级用户的密码
3)Cocoapods的作用和简介不做过多的介绍,需要了解详情的朋友,请自行百度或者Google。
4)安装过程大致分为安装Homebrew、安装RVM、升级Ruby、安装Cocoapods。
2. 安装Homebrew
基本所有的Mac电脑都自带了Homebrew
验证是否安装的命令:$ brew --version
笔者的验证结果:
$ brew --version
Homebrew 1.1.1
Homebrew/homebrew-core (git revision d10b; last commit 2016-11-23)
如果,没有安装,可参照官网指引进行安装http://brew.sh,把官网命令到终端中执行,即可安装:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"(写作时的官网命令)
3.安装RVM
RVM是Ruby的版本管理器,安装RVM的目的是升级Ruby
1)安装
$ curl -L get.rvm.io | bash -s stable
2)配置RVM
$ source ~/.bashrc
$ source ~/.bash_profile
3)RVM常用命令
$ rvm -v // 查看rvm版本
$ rvm list known // 查看已知的ruby的版本
$ rvm use 2.4.0 -- default // 设置默认使用的版本
$ rvm list // 查看已经安装的ruby版本
$ rvm remove 2.4.0 // 卸载一个已经安装版本
3. 升级Ruby
在最新的Mac 10.11 或者 10.12 系统中安装Cocoapods要求Ruby版本大于2.2.2,而自带Ruby版本不满足要求,所以需要升级Ruby版本
1)安装Ruby版本
$ rvm install 2.3.0 // 具体安装版本号,可在运行查看已知Ruby版本命令之后确定,而且建议不要安装最新的版本,笔者就遇到过安装最新版本带来的坑,是通过降低Ruby版本解决的。
2)查看当前Ruby版本
$ ruby -v // 查看ruby当前版本,版本显示成功,就说明Ruby已经安装成功
4. 安装Cocoapods
1)替换默认Ruby源(原因:国内网络原因,需要替换Ruby源为国内的镜像)
查看当前Ruby源:$ gem sources -l
移除默认Ruby源:$ gem sources --remove https://rubygems.org/ // 后面地址,根据查看的当前Ruby源进行相应替换
使用国内Ruby源: $ gem sources -a https://gems.ruby-china.org // 注:淘宝的Ruby源已不再维护,此地址为淘宝Ruby源网址,公布的新的地址
说明:要验证是否替换成功,可运行查看当前Ruby源命令,进行验证
2)安装Cocoapods
$ sudo gem install cocoapods // Mac OS 10.11之前的命令
$ sudo gem install -n /usr/local/bin cocoapods // Mac OS 10.11之后(包括)的命令
3)将Cocopods Specs repository复制到你电脑上~/.cocopods目录下
$ pod setup
说明:此步骤等待时间非常久,需要有耐心,如果没有耐心时,可以运行如下命令,来查看文件夹的大小变化,来验证是否在安装,主要观察文件夹大小是否在发生变化。
$ cd ~/.cocoapods/
$ du -sh *
安装过程,如发生错误,需要重新setup,请执行以下命令
$ pod repo remove master
$ pod setup
4)验证安装成功命令
$ pod --version
1.1.1 // 这是输出
二、 使用简介
1. Podfile文件的大概格式
#指定使用的平台,版本
platform :ios, '7.0'
#指定工程 最新文件xcodeproj使用project替换
xcodeproj 'DownLoadDemo.xcodeproj'
#指定target
target 'DownLoadDemo' do
#pod 'AFNetworking', '~>3.0' 使用的版本是3.0~4.0,不包含4.0
#pod 'AFNetworking' 永远使用最新版本
#pod 'AFNetworking', '3.0' 只使用3.0版本
pod 'AFNetworking', '~>3.0'
end
注意:Podfile文件存放在当前工程的根目录
2. 常见命令
$ pod init // 初始化Podfile文件
$ pod install // pod 安装第三方库命令
$ pod update // pod 更新第三方库命令
$ pod update AFNetworking // pod 更新指定第三方库命令
$ pod search AFNetworking // pod 搜索第三方库命令
三、 错误汇集
1. 错误一
$ pod setup
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master
Cloning into 'master'...
error: RPC failed; curl 56 SSLRead() return error -36
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
解决方案:
setup之前,先执行下面的命令(应该是和xcode关联)
$ sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/
2. 错误二
证书错误问题
$ gem sources --add https://gems.ruby-china.org/
ERROR: SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR: You must add /C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Certification Authority to your local trusted store
ERROR: SSL verification error at depth 2: self signed certificate in certificate chain (19)
ERROR: Root certificate is not trusted (/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA)
https://gems.ruby-china.org/ added to sources
解决方案:
1.更新Ruby环境到2.2.2以上
// 修改 RVM 的 Ruby 安装源到 Ruby China 的 Ruby 镜像服务器,这样能提高安装速度
$echo"ruby_url=https://cache.ruby-china.org/pub/ruby"> ~/.rvm/user/db
1)安装RVM ruby版本管理器
$ curl -L get.rvm.io | bash -s stable
2)配置RVM
$ source ~/.bashrc
$ source ~/.bash_profile
3)查看版本
$ rvm -v
$ ruby -v // 查看ruby当前版本
$ rvm list known // 查看已知的ruby的版本
3)安装ruby2.4.0 版本
$ rvm install 2.4.0
4)ruby的常见命令
$ rvm use 2.4.0 —default // 设置默认使用的版本
$ rvm list // 查看已经安装的ruby版本
$ rvm remove 2.4.0 // 卸载一个已经安装版本
2.更新证书
$ sudo curl -O http://curl.haxx.se/ca/cacert.pem
$ sudo mv cacert.pem cert.pem
3. 错误三
$ sudo gem install -n /usr/local/bin cocoapods
Building native extensions. This could take a while...
ERROR: Error installing cocoapods:
ERROR: Failed to build gem native extension.
解决方案:
Ruby版本问题,出现问题时,最新版本是2.4.0,而且出现问题时使用的也是2.4.0,降低版本到2.3.0之后就没有问题了
4. 错误四
$ pod install
Analyzing dependencies
[!] Unable to find a specification for `ZXingObjC~>3.1.0`
[!] `xcodeproj` was renamed to `project`. Please update your Podfile accordingly.
解决方案:
1.[!] Unable to find a specification for `ZXingObjC~>3.1.0`
这个是由于Podfile文件中对应的库写错导致的,直接从pod search ZXingObjc中复制之后,解决了这个问题
2.[!] `xcodeproj` was renamed to `project`. Please update your Podfile accordingly.
这个是Podfile文件中‘xcodeproj’文件名称需要更改导致的,更改为project即可解决
5. 错误五
$ pod install
Analyzing dependencies
Downloading dependencies
Using MJRefresh (3.1.12)
Installing ZXingObjC (3.1.0)
[!] Error installing ZXingObjC
[!] /usr/bin/git clone https://github.com/TheLevelUp/ZXingObjC.git /var/folders/t3/gn0lf42s5r99flc2pvq1bgf00000gn/T/d20161226-6840-1f34iof --template= --single-branch --depth 1 --branch 3.1.0
Cloning into '/var/folders/t3/gn0lf42s5r99flc2pvq1bgf00000gn/T/d20161226-6840-1f34iof'...
error: RPC failed; curl 56 SSLRead() return error -36
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
解决方案:
执行下面的命令之后,第二次尝试就OK了,不知道为什么第一次尝试不行
$ sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/