前言
这篇记录,是我在几次安装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文件的大概格式
注意:Podfile文件存放在当前工程的根目录
2. 常见命令
$ pod init // 初始化Podfile文件
$ pod install // pod 安装第三方库命令
$ pod update // pod 更新第三方库命令
$ pod update AFNetworking // pod 更新指定第三方库命令
$ pod search AFNetworking // pod 搜索第三方库命令