1.打開終端
2.移除現(xiàn)有 Ruby 默認源
$ gem sources --remove https://rubygems.org/
3.使用新的源
$ gem sources -a https://ruby.taobao.org/已廢棄
$ gem sources -a https://gems.ruby-china.org/已廢棄
$ sudo gem sources --add https://gems.ruby-china.com/
4.驗證是否替換成功
$ gem sources -l
5.安裝 CocoaPods (注意:此時如果提示ruby版本過低,請參考下邊進行ruby升級)
$ sudo gem install cocoapods(比較費時) 10.11以前系統(tǒng)使用
10.11以后系統(tǒng)使用sudo gem install -n /usr/local/bin cocoapods
$ sudo gem update --system(gem版本太老也可能導(dǎo)致安裝失敗,所以先更新一下)
$ pod setup
cocoa pods 使用(新建工程,并在終端用 cd 指令進入工程根目錄)
1. touch Podfile : 創(chuàng)建 Podfile 文件夾
2. pod search + 第三方庫
3. vim Podfile?:進入Podfile 文件夾
4. Podfile 文件格式如下:
5. 例: platform:ios,'7.0'?
pod 'AFNetworking', '~> 2.5.3'
platform:ios,'7.0'
platform:ios,'7.0'
target "MyProject" do? 注意:此處MyProject為自己工程名
pod 'AFNetworking', '~> 3.1.0'
end
6. 關(guān)閉Podfile文件夾
7. pod install
8. 每次更改了Podfile文件,都需要重新執(zhí)行一次pod update命令
9. 當執(zhí)行pod install之后,除了Podfile,還會生成一個名為Podfile.lock的文件,它會鎖定當前各依賴庫的版本,之后即使多次執(zhí)行pod install也不會更改版本,只有執(zhí)行pod update才會改變Podfile.lock.在多人協(xié)作的時候,這樣可以防止第三方庫升級時候造成大家各自的第三方庫版本不一致。所以在提交版本的時候不能把它落下,也不要添加到.gitignore中.
更新
10.如果使用過程中找不到頭文件
解決辦法:在項目的Target的里設(shè)置一下,添加cocoapods頭文件目錄:目錄路徑直接寫:${SRCROOT}? ,后邊選擇recursive 。就可以了。
注意:是在 User Header Search Paths 里添加,不是上面的 Header Search Paths.
如圖:

注: 使用RVM對Ruby進行升級,升級過程如下:
1、安裝 RVM
(RVM:Ruby Version Manager,Ruby版本管理器,包括Ruby的版本管理和Gem庫管理(gems))
安裝 RVM。
$ curl -L get.rvm.io | bash -s stable
$ source ~/.bashrc
$ source ~/.bash_profile
測試是否安裝正常
rvm -v
2、用RVM升級Ruby
查看當前ruby版本
$ ruby -v
列出已知的ruby版本
$ rvm list known
選擇想要安裝的ruby版本
$ rvm install x.x.x