安装cocoapods

181 阅读2分钟

一.CocoaPods是什么?

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。相比以前手动添加第三方库,这个工具就是神器。

二.CocoaPods的安装

Mac电脑自带Ruby环境,只需打开终端开始进行一系列的操作

1.先查看源

gem sources -l

2.如果源路径 https://rubygems.org/  那么则需更换,因为国内用的 gems.ruby-china.org/ 比较好

删除原来的源:

gem sources --remove https://rubygems.org/

添加新的源

gem sources -a https://gems.ruby-china.org/

确定是否添加成功查看源

3.开始安装cocoapods

使用ruby的命令安装即可:

sudo gem install cocoapods
pod setup

常遇到的问题

1.While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/fuzzy_match

执行

sudo gem install -n /usr/local/bin cocoapods

2.Error installing pods:active support requires Ruby version >= x.x.x  这个是ruby版本问题

查看ruby版本

ruby -v

查看目前的所有ruby版本:

rvm list known

如果提示xxx:command not found 请先安装rvm

curl -L get.rvm.io | bash -s stable  

安装2.6.0:

rvm install 2.6.0

终端运行结果:

Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.6.0.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
: 

这里回车:

It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
The current contents of /usr/local are .git
Requirements installation failed with status: 1.

执行:

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

输入电脑开机密码:

这个homebrew下载贼慢,耐心等待一下

安装好了则需要:

sudo gem install cocoapods

给项目添加podfile,在终端cd到项目中,执行命令

pod init

然后打开项目文件,找到 Podfile 然后在里面编辑,添加需要用到的第三方框架,然后在终端执行

pod install

如果执行pod install 卡主的话,可以尝试下面命令:

pod install --verbose --no-repo-update
pod update --verbose --no-repo-update