一.安装
1.首先替换镜像源
# 替换ruby镜像源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
# 查看镜像源, 如下显示说明替换成功
gem sources -l
# *** CURRENT SOURCES ***
# https://gems.ruby-china.com/
2.一行代码安装
sudo gem install -n /usr/local/bin cocoapods
安装成功是这个样子

3.如果没安装成功
那么我宣布, 你的事大了, 不过你也不用担心, 按照我的步骤走就行了
如果你在网络上寻找, 他们会让你安装一大堆垃圾东西, 比如rvm, 然后用rvm去更新gem 这非常麻烦
首先我把出事的代码和原因都告诉你
代码
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.15.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.0 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.15.0/gem_make.out
- 熟悉不? 请回答我熟悉
- 为啥会有这锤子问题? 因为mac10.15.4自带的ruby版本是2.6.0, 无法安装
cocoapods
解决方案
1.首先安装brew
先试试你有没有, 如果有就不用重复安装了
brew -v
这玩意你现在不安早晚也得安, 俩字就是有用, 我们这里走清华大学的安装路线, 如果你觉得的我的命令不官方请查看官方网址, 你安上就行
https://brew.sh
https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/
if [[ "$(uname -s)" == "Linux" ]]; then BREW_TYPE="linuxbrew"; else BREW_TYPE="homebrew"; fi
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/${BREW_TYPE}-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/${BREW_TYPE}-bottles"
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install
2.安装ruby
brew install ruby
安装好之后把ruby设置成环境变量, 如果不会你可以打
brew link --overwrite ruby
然后brew会告诉, 不行, 你自己去搞
objcatdeMBP:~ objcat$ brew link --overwrite ruby
Warning: Refusing to link macOS provided/shadowed software: ruby
If you need to have ruby first in your PATH, run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/objcat/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
取其精华, 请把这句话复制到你的终端里 注意文件夹名要是你自己的
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/objcat/.bash_profile
设置完环境变量就关闭, 重新打开终端执行
sudo gem install -n /usr/local/bin cocoapods
4.下载索引
pod setup
这是可能会遇到一个问题

这里提供的解决办法是手动下载
cd ~/.cocoapods/repos
git clone https://gitee.com/mirrors/CocoaPods-Specs.git trunk --progress
下载完成后搜索试试
pod search afnetworking
效果如图就证明成功了

二.使用
-
新建一个工程
- 进入到工程目录
cd ~/test
- 创建Podfile
pod init
创建完成后是这个样子
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'test' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for test
target 'testTests' do
inherit! :search_paths
# Pods for testing
end
target 'testUITests' do
# Pods for testing
end
end
- 加入AF
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'test' do
pod 'AFNetworking'
end
platform 的意思是系统支持的最低版本
target是你的工程名
do开始 end结束
写完之后在终端输入
pod install
系统就会开始下载了 下载完成如图

图6
之后我们会发现在工程目录中出现了一个workspace,双击那个白色的文件打开项目(workspace)

图7
这时你或许会遇到第一个工程目录怎么也打不开

图8
解决这个问题非常简单 选中xcode cmd+q关闭所有窗口 然后再重新打开workspace就可以了
接下来是引入头文件 如图

图9
用尖括号引入头文件
三.卸载
- 干掉pod文件
sudo rm -rf /usr/local/bin/pod
- 干掉pod安装包文件
=> 查看pod安装包文件列表
gem list

gem list
=> 逐条干掉
gem uninstall cocoapods
gem uninstall cocoapods-core
gem uninstall cocoapods-deintegrate
gem uninstall cocoapods-downloader
gem uninstall cocoapods-plugins
gem uninstall cocoapods-search
gem uninstall cocoapods-stats
gem uninstall cocoapods-trunk
gem uninstall cocoapods-try
到这里 cocoapods 已经被干掉了。
