由于之前電腦出現(xiàn)問題,被迫 格式化硬盤,所以電腦要重新配置
上圖為敬

1、設(shè)置 ruby 源
Mac 系統(tǒng)自帶的會(huì)安裝好 ruby 環(huán)境
1.1、查看自己電腦的 ruby 源
ruby -v
ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]
- 傳說 CocoaPods 支持的 ruby 最低版本是2.2.2,如果自己電腦版本低于這個(gè)版本就升級(jí) ruby ,上面顯示我的電腦版本不需要升級(jí),可以忽略下面的升級(jí)操作
sudo gem update --system
1.2更換 ruby 鏡像
- ruby 默認(rèn)的原地址是國外網(wǎng)絡(luò)地址,通過下面命令查看當(dāng)前的鏡像
gem sources -l
gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
- 移除當(dāng)前鏡像
gem sources --remove https://rubygems.org/
gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
- 添加國內(nèi)的 ruby 鏡像
gem sources -a https://gems.ruby-china.com/
gem sources -a https://gems.ruby-china.com/
https://gems.ruby-china.com/ added to sources
- 再次查看當(dāng)前鏡像,發(fā)現(xiàn)已經(jīng)替換成功
gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
2、安裝 CocoaPods
ruby 環(huán)境安裝后,就可以安裝 CocoaPods 了
2.1、真假 CocoaPods
不要著急敲命令,往下看,有坑
不要著急敲命令,往下看,有坑
不要著急敲命令,往下看,有坑
方法一
- 輸入安裝 CocoaPods 命令
sudo gem install cocoapods
sudo gem install cocoapods
等待了幾分鐘后,顯示下面的信息,便表示已經(jīng)安裝成功了。
Installing ri documentation for cocoapods-1.8.4
Done installing documentation for thread_safe, tzinfo, concurrent-ruby, i18n, activesupport, nap, fuzzy_match, httpclient, algoliasearch, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, atomos, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 21 seconds
29 gems installed
- 最后一步 ,下載文件
pod setup
pod setup
可是,最怕的事情還是發(fā)生了,輸入 pod setup 后什么也沒有發(fā)生,不是說幾百兆的文件等著我下載嗎,難道5G真的這么快,NO,NO,NO,我并沒有5G。
看到這里可以滑到底部看下 Alan的世界 的評(píng)論,原因是 CocoaPods 1.8將CDN切換為默認(rèn)的規(guī)范回購源,并進(jìn)行了一些增強(qiáng)!不在需要 git clone 去拉去龐大的master分支的所有代碼,具體可以參考 http://blog.cocoapods.org/CocoaPods-1.8.0-beta/
此時(shí),你的電腦上已經(jīng)集成了 CocoaPods 并且能正常使用
下面我新建一個(gè)TestDemo ,集成AFNetworking
- 依次輸入
pod setup、pod init - 接下來就是我們正常的創(chuàng)建Podfile文件步驟了
- 會(huì)顯示下面紅框中的內(nèi)容,下面的
AFNetworking也集成成功了

方法二
Mac OS X 10.11前
sudo gem install cocoapodsMac OS X 10.11后
sudo gem install -n /usr/local/bin cocoapods由于本機(jī)的系統(tǒng)是10.15所以選擇后者命令輸入
sudo gem install -n /usr/local/bin cocoapods
sudo gem install -n /usr/local/bin cocoapods
Successfully installed cocoapods-1.8.4
Parsing documentation for cocoapods-1.8.4
Done installing documentation for cocoapods after 1 seconds
1 gem installed
納尼,這么快,我不信
2.2、檢測(cè)是否安裝成功
pod search AFNetworking
pod search AFNetworking
[!] Unable to find a pod with name, author, summary, or description matching `AFNetworking`
- 看來還是失敗了,天不助我啊,別慌,咱們還有 Git 大法
git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master
git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master
Cloning into '/Users/gao/.cocoapods/repos/master'...
remote: Enumerating objects: 1130099, done.
remote: Total 1130099 (delta 0), reused 0 (delta 0)iB | 4.28 MiB/s
Receiving objects: 100% (1130099/1130099), 408.72 MiB | 2.84 MiB/s, done.
Resolving deltas: 100% (563214/563214), done.
Checking out files: 100% (141834/141834), done.
激動(dòng)的心,顫抖的手,一個(gè)蘋果沒有吃完就 clone 好了,
- 檢測(cè) 是否安裝成功
pod search AFNetworking
pod search AFNetworking
Creating search index for spec repo 'master'.. Done!
第一次會(huì)出現(xiàn)上面的 Creating search index for spec repo 'master'.. 等到 出現(xiàn) Done 后 就會(huì)出現(xiàn)我們搜索的結(jié)果,再次輸入命令就不會(huì)出現(xiàn)這行代碼了,直接就能搜索了。
pod search AFNetworking
[!] Unable to find a pod with name, author, summary, or description matching `AFNetworking`
-> AFNetworking (3.1.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.1.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,
3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2,
2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,
2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4,
1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2,
1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]
- Subspecs:
- AFNetworking/Serialization (3.1.0)
- AFNetworking/Security (3.1.0)
- AFNetworking/Reachability (3.1.0)
- AFNetworking/NSURLSession (3.1.0)
- AFNetworking/UIKit (3.1.0)
-> AFNetworking+AutoRetry (0.0.5)
Auto Retries for AFNetworking requests
pod 'AFNetworking+AutoRetry', '~> 0.0.5'
- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
- Source: https://github.com/shaioz/AFNetworking-AutoRetry.git
:
到此就代表 CocoaPods 真正安裝成功嘍。
3、使用 CocoaPods
- 未使用前項(xiàng)目結(jié)構(gòu)

3.1、添加 Podfile 文件
- 終端中 cd 到當(dāng)前目錄 輸入
touch Podfile

輸入 touch Podfile 后,我們的工程目錄中會(huì)出現(xiàn) 一個(gè) Podfile 文件

3.2、編輯 Podfile 文件
在文件中輸入自己需要的第三方項(xiàng)目庫,我們以 AFNetworking 為例
platform :ios, '10.0'
target 'CocoaPodsDemo' do
pod 'AFNetworking'
end
3.3、執(zhí)行 pod
終端輸入 pod install
pod install
Analyzing dependencies
Adding spec repo `trunk` with CDN `https://cdn.cocoapods.org/`
Downloading dependencies
Installing AFNetworking (3.2.1)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `CocoaPodsDemo.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
輸出上面的內(nèi)容就表示,已經(jīng)成功了,提示我們關(guān)掉當(dāng)前的看到上面的 項(xiàng)目,打開 CocoaPodsDemo.xcworkspace 這個(gè)項(xiàng)目,再次回到我們的項(xiàng)目目錄里面查看,已經(jīng)多了幾個(gè)目錄

- 點(diǎn)擊
CocoaPodsDemo.xcworkspace打開項(xiàng)目,以后也是點(diǎn)擊這個(gè)文件來打開通過 CocoaPods 生成的 項(xiàng)目,會(huì)發(fā)現(xiàn),項(xiàng)目目錄已經(jīng)有了我們需要的AFNetworking第三方文件

“人生哪能多如意,萬事只求半稱心”———最近挺喜歡這句話,分享給大家吧。