CocoaPods 常用命令
- 升級(jí) Gem
sudo gem update -n /usr/local/bin --system
- 切換 Cocoapods 的數(shù)據(jù)源
gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/
gem sources -l
- 安裝 Cocoapods
sudo gem install -n /usr/local/bin cocoapods
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 directory.
- 解決辦法
重新安裝 Ruby,見下文 `rvm install ruby-3.0.0`
- 安裝 Cocoapods 指定版本
sudo gem install -n /usr/local/bin cocoapods -v 1.0.1
- 卸載
sudo gem uninstall -n /usr/local/bin cocoapods
- 查詢
gem list --local | grep cocoapods
-
卸載 pod
- 查看
pod地址:which pod - 刪除
pod:sudo rm -rf [pod 地址] - 查看
gem安裝包:gem list - 按版本號(hào)卸載
cocoapods:sudo gem uninstall -n /usr/local/bin cocoapods -v 1.8.1
-
什么是CocoaPods?
CocoaPods是一個(gè)負(fù)責(zé)管理iOS項(xiàng)目中第三方開源庫的工具。CocoaPods的項(xiàng)目源碼在Github上管理。該項(xiàng)目開始于2011年8月12日,在這兩年多的時(shí)間里,它持續(xù)保持活躍更新。開發(fā)iOS項(xiàng)目不可避免地要使用第三方開源庫,CocoaPods的出現(xiàn)使得我們可以節(jié)省設(shè)置和更新第三方開源庫的時(shí)間。
在我們有了CocoaPods這個(gè)工具之后,只需要將用到的第三方開源庫放到一個(gè)名為Podfile的文件中,然后在命令行執(zhí)行$ pod install
命令。CocoaPods就會(huì)自動(dòng)將這些第三方開源庫的源碼下載下來,并且為我的工程設(shè)置好相應(yīng)的系統(tǒng)依賴和編譯參數(shù)。
-
CocoaPods 的好處
1、在引入第三方庫時(shí)它可以自動(dòng)為我們完成各種各樣的配置,包括配置編譯階段、連接器選項(xiàng)、甚至是ARC環(huán)境下的-fno-objc-arc配置等。
2、使用CocoaPods可以很方便地查找新的第三方庫,方便項(xiàng)目開發(fā)。

-
CocoaPods 安裝步驟
1、升級(jí) Ruby 環(huán)境
sudo gem update -n /usr/local/bin --system
出現(xiàn) RubyGems system software updated 字樣,
恭喜你升級(jí)Ruby成功了!
可能上面升級(jí)Ruby環(huán)境失敗,這個(gè)時(shí)候使用
RVM對Ruby進(jìn)行升級(jí):
(1) 安裝 RVM
RVM (Ruby Version Manager),即Ruby版本管理器,包括Ruby的版本管理和Gem庫管理。
curl -L get.rvm.io | bash -s stable
source ~/.bashrc
source ~/.bash_profile
若提示 -bash: /Users/***/.bash_profile: No such file or directory 找不到 .bash_profile 文件,直接在根目錄創(chuàng)建一個(gè)即可:
cd ~
touch .bash_profile
安裝成功后,可查看rvm版本:
rvm -v
(2) 用 RVM 升級(jí) Ruby
- 查看當(dāng)前ruby版本:
$ ruby -v - 列出已知的ruby版本:
$ rvm list known - 安裝xcode command line:
$ xcode-select --install - 選擇要安裝的版本,如安裝ruby 2.4.1:
$ rvm install 2.4.1 - 設(shè)置默認(rèn)版本:
rvm use 2.4.1 --default - 查看已安裝 ruby:
rvm list - 卸載一個(gè)已安裝ruby版本:
sudo rvm remove 2.4.1
Warning! PATH is not properly set up, $GEM_HOME is not set.
Usually this is caused by shell initialization files. Search for PATH=... entries.
You can also re-add RVM to your profile by running: rvm get stable --auto-dotfiles
To fix it temporarily in this shell session run: rvm use ruby-2.5.3
To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
- 遇到的問題
rvm install ruby-3.0.0
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/11.6/x86_64/ruby-3.0.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system - please wait
Installing required packages: automake, libksba - please wait
There were package installation errors, make sure to read the log.
Try `brew tap --repair` and make sure `brew doctor` looks reasonable.
Check Homebrew requirements https://docs.brew.sh/Installation
- 先執(zhí)行下面命令:
rvm autolibs read-only
There was an error(35).
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/3.0/ruby-3.0.0.tar.gz
No fallback URL could be found, try increasing timeout with:
echo "export rvm_max_time_flag=20" >> ~/.rvmrc
There has been an error fetching the ruby interpreter. Halting the installation.
- 執(zhí)行下面命令解決
echo "ruby_url=https://cache.ruby-china.com/pub/ruby" > ~/.rvm/user/db
- 安裝
rvm install ruby-3.0.0
- 切換版本
rvm use 3.0.0
2、更換 Ruby 鏡像
-
移除現(xiàn)有的
Ruby 鏡像gem sources --remove https://rubygems.org/ -
添加國內(nèi)最新鏡像源
gem sources -a https://gems.ruby-china.com/ -
執(zhí)行完畢之后輸入
gem sources -l來查看當(dāng)前鏡像gem sources -l
輸出結(jié)果如下:
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
恭喜你添加成功啦
3、安裝 CocoaPods
sudo gem install -n /usr/local/bin cocoapods

然后執(zhí)行 pod setup
終端輸入:$ pod setup
提示如下信息說明執(zhí)行完成:
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0.beta.2
Setup completed
可以
command+n新建一個(gè)終端窗口,執(zhí)行cd ~/.cocoapods/進(jìn)入到該文件夾下,然后執(zhí)行du -sh *來查看文件大小,當(dāng)出現(xiàn)Setup completed的時(shí)候說明已經(jīng)完成了。
4、CocoaPods 的使用
-
搜索第三方庫
pod search *** -
創(chuàng)建
Podfile文件cd 工程目錄進(jìn)入工程文件目錄下,然后創(chuàng)建Podfile文件
touch Podfile或者
pod init -
使用
vim編輯Podfile文件vim Podfile
An example of a more complex Podfile can be:
platform :ios, '9.0'
inhibit_all_warnings!
target 'MyApp' do
pod 'ObjectiveSugar', '~> 0.5'
target "MyAppTests" do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
5、下載庫到項(xiàng)目中
pod install
or
pod update
pod installted 完成后,工程目錄會(huì)看到多了一個(gè)后綴名為 *.xcworkspace 文件。以后打開此文件即可開發(fā)啦~
6、文件和指令說明
-
Podfile.lock文件最后一次更新 Pods 時(shí), 所有第三方框架的版本號(hào) 常用指令的區(qū)別
pod install
1.會(huì)根據(jù) Podfile.lock 文件中列舉的版本號(hào)來安裝第三方框架
2.如果一開始 Podfile.lock 文件不存在, 就會(huì)按照 Podfile 文件列舉的版
本號(hào)來安裝第三方框架
3.安裝框架之前, 默認(rèn)會(huì)執(zhí)行pod repo update指令
pod update
1.將所有第三方框架更新到最新版本, 并且創(chuàng)建一個(gè)新的 Podfile.lock 文件
2.安裝框架之前, 默認(rèn)會(huì)執(zhí)行 pod repo update 指令
pod install --no-repo-update
pod update --no-repo-update
1.安裝框架之前, 不會(huì)執(zhí)行pod repo update指令
7、原理和說明
a. 第三方庫會(huì)被編譯成 .a 靜態(tài)庫供我們真正的工程使用
* CocoaPods 會(huì)將所有的第三方庫以 target 的方式組成一個(gè)名為 Pod 的工程,該工程就放在剛才新生成的Pods目錄下。整個(gè)第三方庫工程會(huì)生成一個(gè)名稱為 libPods.a 的靜態(tài)庫提供給自己的工程用。
* 對于資源文件,CocoaPods 提供了一個(gè)名為 Pods-resources.sh 的bash 腳本,該腳本在每次項(xiàng)目編譯的時(shí)候都會(huì)執(zhí)行,將第三方庫的各種資源文件復(fù)制到目標(biāo)目錄中。
b. 我們的工程和第三方庫所在的工程會(huì)由一個(gè)新生成的workspace 管理
為了方便我們直觀的管理工程和第三方庫,CocoaPodsTest 工程和Pods 工程會(huì)被以 workspace 的形式組織和管理,也就是我們剛才看到的*.xcworkspace文件。
c. 原來的工程設(shè)置已經(jīng)被更改了,這時(shí)候我們直接打開原來的工程文件去編譯就會(huì)報(bào)錯(cuò),只能使用新生成的 workspace 來進(jìn)行項(xiàng)目管理。
d.CocoaPods 通過一個(gè)名為 Pods.xcconfig 的文件來在編譯時(shí)設(shè)置所有的依賴和參數(shù)。
8、其他
1、pod install 提速
每次執(zhí)行 pod install 和 pod update 的時(shí)候,cocoapods 都會(huì)默認(rèn)更新一次 spec 倉庫。這是一個(gè)比較耗時(shí)的操作。在確認(rèn) spec 版本庫不需要更新時(shí),給這兩個(gè)命令加一個(gè)參數(shù)跳過 spec 版本庫更新,可以明顯提高這兩個(gè)命令的執(zhí)行速度。
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
2、關(guān)于 Podfile 文件編輯時(shí),第三方庫版本號(hào)的各種寫法
pod ‘AFNetworking’ //不顯式指定依賴庫版本,表示每次都獲取最新版本
pod ‘AFNetworking’, ‘2.0’ //只使用2.0版本
pod ‘AFNetworking’, ‘>2.0′ //使用高于2.0的版本
pod ‘AFNetworking’, ‘>=2.0′ //使用大于或等于2.0的版本
pod ‘AFNetworking’, ‘<2.0′ //使用小于2.0的版本
pod ‘AFNetworking’, ‘<=2.0′ //使用小于或等于2.0的版本
pod ‘AFNetworking’, ‘~>0.1.2′ //使用大于等于0.1.2但小于0.2的版本,相當(dāng)于>=0.1.2并且<0.2.0
pod ‘AFNetworking’, ‘~>0.1′ //使用大于等于0.1但小于1.0的版本
pod ‘AFNetworking’, ‘~>0′ //高于0的版本,寫這個(gè)限制和什么都不寫是一個(gè)效果,都表示使用最新版本
1、查看 CocoaPods 組件安裝位置:
which pod
結(jié)果:
/usr/bin/pod
2、手動(dòng)移除這個(gè)組件:
sudo rm -rf /usr/bin/pod
3、查看 gems 中本地程序包
gem list
還可以輸入如下命令查看:
pod --version
4、移除程序包
有了版本號(hào),就可以根據(jù)當(dāng)前版本號(hào)移除 CocoaPods 了:
sudo gem uninstall cocoapods -v 1.3.1
結(jié)果:
Successfully uninstalled cocoapods-1.3.1
5、安裝指定版本的 CocoaPods
sudo gem install cocoapods -v 1.3.1
結(jié)果:
Successfully installed cocoapods-1.3.1