您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

CocoaPods 安裝全攻略與常見踩坑筆記

編輯:iOS 開發基礎

以前在微博上整理過一篇 CocoaPods 的文章(主要是給自己看的)。昨天重灌電腦,安裝過程又踩了幾個坑,正好今天比較空,就重新整理一份。

一、CocoaPods 是什麼

CocoaPods 是用 Ruby 寫的依賴管理工具,專門負責管理 iOS 專案中的第三方開源庫。它能讓我們集中、統一地管理依賴,省下大量手動設定與升級的時間。

二、安裝流程

Mac 自帶 Ruby 環境,所以直接開終端動手就好。

(1)先把 Ruby 的軟體源換成國內鏡像

官方源 rubygems.org 用的是亞馬遜雲服務,不翻牆往往很慢甚至連不上。可以換成淘寶源,或者由 Ruby China 社群維護的 gems.ruby-china.org。(我最初用淘寶源,結果卡死,可能是失效了也可能是網速問題,後面講坑的時候會再提到。)以下範例使用 Ruby China 的源。

注意:複製指令時不要把提示符 $ 一起複製,終端本身就自帶。

先移除預設源:

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

執行後會看到:

https://rubygems.org/ removed from sources

再新增可用的鏡像源:

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

驗證是否替換成功:

gem sources -l

輸出:

*** CURRENT SOURCES ***
https://gems.ruby-china.org/

到這裡源就換好了。

(2)開始安裝 CocoaPods

其實就是一行 sudo gem install cocoapods,但這一步最容易出問題。

問題一:Errno::EPERM,Operation not permitted - /usr/bin/fuzzy_match

解法:加上 -n 指定安裝目錄。

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

看到 gems installed 就算成功。

問題二:Error installing pods: activesupport requires Ruby version >= 2.2.2

先確認目前 Ruby 版本:

ruby -v

查看所有可用版本:

rvm list known

如果提示 command not found,先安裝 rvm:

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

已安裝的話會列出所有 Ruby 版本:

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[.8]
[ruby-]2.2[.4]
[ruby-]2.3[.0]
[ruby-]2.2-head
ruby-head ...

安裝 2.2.2:

rvm install 2.2.2

如果直接成功就可以跳過下面關於 Homebrew 的處理。否則終端會停在要求安裝 Homebrew 的提示:

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.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation ...

回車後若看到:

It appears Homebrew is already installed. ...
Requirements installation failed with status: 1.

這才需要執行卸載:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Warning: This script will remove:
/Library/Caches/Homebrew/
/usr/local/.git/
Are you sure you want to uninstall Homebrew? [y/N] y
==> Removing Homebrew installation...
==> Removing empty directories...
==> Homebrew uninstalled!

接著再跑一次:

rvm install 2.2.2

這次會重新安裝 Homebrew,過程中會要求輸入電腦密碼:

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
Password:

安裝完成後會看到:

==> Downloading and installing Homebrew...
==> Installation successful!
==> Next steps
Run `brew help` to get started
Installing requirements for osx.
Updating system.....
Installing required packages: autoconf, automake, libtool, pkg-config, libyaml, readline, libksba, openssl...
Requirements installation successful.
ruby-2.2.2 - #configure
ruby-2.2.2 - #download
ruby-2.2.2 - #validate archive
ruby-2.2.2 - #extract
ruby-2.2.2 - #validate binary
ruby-2.2.2 - #setup
ruby-2.2.2 - #gemset created
ruby-2.2.2 - #generating default wrappers........

至此 Ruby 2.2.2 就裝好了。想要更高版本也沒問題,把版本號換掉即可。然後繼續 sudo gem install cocoapods。

問題三:卡在 Setting up CocoaPods master repo

出現這行代表 CocoaPods 正在把 spec 資訊下載到 ~/.cocoapods,這一步非常耗時,等到出現 Setup completed 才算完成。

等待期間可以開一個新終端視窗,查看下載進度:

cd ~/.cocoapods
du -sh *

這樣就能判斷是網速問題還是源失效了。

解法:

1. 我第一次用淘寶源時,檔案大小一直不變(可能是網速,也可能是源已失效),換成 gems.ruby-china.org 後重新走一遍,下載速度就正常了,檔案大約 800 多 MB。

2. 也可以直接使用 CocoaPods 索引鏡像。所有 podspec 都托管在 github.com/CocoaPods/Specs,第一次執行 pod setup 時會把索引同步到本地 ~/.cocoapods,這份索引很大,國外節點下載很慢。有人在国内架了鏡像:

pod repo remove master
git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master
pod repo update

把其中的倉庫位址替換成 OSChina 上的鏡像即可。

問題四:git pull 失敗

[!] An error occurred while performing `git pull` on repo `master`.
[!] /usr/bin/git pull --ff-only

原因通常是 CocoaPods 的 master 分支與當前最新版 Xcode 不相容。解法是刪掉 master 再重建:

sudo rm -fr ~/.cocoapods/repos/master

然後執行:

pod setup

問題五:升級 EI Capitan 後 pod: command not found

這個問題我是在別人的文章裡看到的。升級系統後 CocoaPods 的 pod 指令失效,解法三步:

1. 先卸載:sudo gem uninstall cocoapods
2. 重新安裝:sudo gem install -n /usr/local/bin cocoapods
3. 若仍無執行權限:sudo chmod +rx /usr/local/bin/

三、日常使用

(1)搜尋第三方庫:

pod search AFNetworking

(2)建立 Podfile。先用 cd 切換到專案目錄,再執行:

touch Podfile

也可以用更標準的方式產生:

pod init

編輯它:

open Podfile

然後執行 pod install 即可。

(3)移除已安裝的庫或整個卸載 CocoaPods,可以參考相關文章。

pod install 與 pod update 的差別:

1. 用 pod install 安裝或增減庫。即使你的專案已經有 Podfile 且執行過 pod install,新增或移除庫時也應該用它。

2. 只有在需要把庫升級到新版本時,才用 pod update [PODNAME]。

小技巧

最近執行 pod install 或 pod update 時常卡在 Analyzing dependencies,原因是這兩個指令會順手升級 CocoaPods 的 spec 倉庫。加上參數跳過這一步,速度會明顯變快:

pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved