1.前言
CocoaPods是一個用Ruby寫的,是一個用來管理第三方依賴庫的工具??梢越鉀Q庫與庫之間的依賴關(guān)系,下載庫的源代碼,同時通過創(chuàng)建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供開發(fā)使用。
MacOS系統(tǒng)版本:12.1
2.安裝步驟
安裝Homebrew --> 安裝rvm --> 安裝ruby -> 安裝cocoapods
3.檢查homebrew或安裝
3.1 查看安裝的版本信息
brew --version
- 終端查詢結(jié)果
Homebrew 3.6.21
Homebrew/homebrew-core (git revision 1d02c20b2a2; last commit 2023-02-08)
Homebrew/homebrew-cask (git revision cd3977d330; last commit 2023-02-08)
3.2 安裝homebrew
- 方式1 官方提供的安裝方法
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
出現(xiàn)如下錯誤,需要進(jìn)入系統(tǒng)hosts文件配置域名ip解析。
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
解決方案
步驟1. 獲取raw.githubusercontent.com IP地址 輸入raw.githubusercontent.com查詢對應(yīng)的ip網(wǎng)址

F2994A50-02BD-4379-9C64-283EA31188F0.png
步驟2.快速進(jìn)入hosts
訪達(dá)->前往文件夾->輸入/etc
步驟3.配置如下,配置完成后重新執(zhí)行命令即可
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
::1 localhost
- 方式2 使用國內(nèi)的源進(jìn)行安裝
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
安裝完成后執(zhí)行
source /Users/apple/.rvm/scripts/rvm
4.檢查rvm
4.1 檢查rvms是否安裝
rvm -v
- 終端結(jié)果,出現(xiàn)如下則說明安裝過
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
4.2 更新rvm
rvm get stable
- 若執(zhí)行失敗,可以在命令前加入sudo權(quán)限
sudo rvm get stable
4.3 安裝rvm,下面命令都需要執(zhí)行
//安裝rvm
curl -L https://get.rvm.io | bash -s stable
//載入rvm
source ~/.rvm/scripts/rvm
5.檢查ruby或安裝
5.1 在終端中查看ruby的版本信息
ruby -v
- 結(jié)果如下,當(dāng)ruby版本低于2.2.2時,安裝cocoapods會報錯
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin21]
5.2 查看ruby在系統(tǒng)中可安裝的版本信息
rvm list known
- 結(jié)果如下
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.0]
ruby-head
.....
5.3 安裝ruby,建議安裝最新版本
rvm install 3.0.0
5.4 查看是否安裝成功
ruby -v
5.5 將ruby該版本設(shè)置默認(rèn)使用
rvm use 3.0.0 --default
5.6 更換ruby鏡像源
//更新成最新
sudo gem update --system
//第一步移除舊源
gem sources --remove https://rubygems.org/
//添加china源
gem sources --add https://gems.ruby-china.com/
//檢查源
gem sources -l
- 終端結(jié)果如下則配置正確
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
6.安裝cocoapods
sudo gem install -n /usr/local/bin cocoa pods
7.執(zhí)行pod install
- 錯誤1:
[!] Unable to add a source with urlhttps://github.com/CocoaPods/Specs.gitnamedmaster.
You can try adding it manually in~/.cocoapods/reposor viapod repo add.
//執(zhí)行
pod repo add master https://github.com/CocoaPods/Specs.git