- GitHub: CocoaPods
- star: 12.4k
它是用 Ruby 寫(xiě)的,并劃分為多個(gè) gem 包。它依賴于 Ruby 環(huán)境。
安裝步驟
1. 更新系統(tǒng) Ruby 環(huán)境
# 這一步驟需要科學(xué)上網(wǎng)
$ sudo gem update --system
# 查看已安裝的 Ruby 版本(最新版本:3.0.6,截止20200430)
$ gem -v
2. 安裝 CocoPods 前先替換鏡像源
默認(rèn)的鏡像資源服務(wù)器被天朝給墻了。所以需要先更換源地址,然后再安裝。
- 移除原先的 Ruby 源:
$ gem sources --remove https://rubygems.org/
- 指定為 Ruby China 的鏡像源:
$ gem sources --add https://gems.ruby-china.com/
?? 相關(guān)鏈接:Ruby China 的 RubyGems 鏡像上線
- 驗(yàn)證新源是否替換成功:
$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
3. 安裝 CocoaPods
使用 ruby 的 gem 命令下載并安裝 CocoaPods。
$sudo gem install cocoapods
備注:以上命令在 Mac OS 系統(tǒng)升級(jí)到 OS X EL Capitan 版本后需要改為 :$ sudo gem install -n /usr/local/bin cocoapods
$ pod setup
使用 CocoaPods 的鏡像索引
所有項(xiàng)目的 Podspec 文件都托管在 https://github.com/CocoaPods/Specs 中。第一次執(zhí)行 pod setup
時(shí),CocoaPods 會(huì)將這些 Podspec 索引文件更新到本地的 ~/.cocoapods/
目錄下,該索引文件較大且更新非常緩慢。
將 CocoaPods 設(shè)置成 gitcafe 或者 occhina 鏡像,執(zhí)行索引更新時(shí)會(huì)快很多。
pod repo remove master
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update
也可以將以上代碼中的 https://gitcafe.com/akuandev/Specs.git
替換成 http://git.oschina.net/akuandev/Specs.git
即可使用 occhina 上的鏡像。
添加新源時(shí)報(bào)錯(cuò):
? ~ pod repo add master https://git.coding.net/CocoaPods/Specs.git
[!] To setup the master specs repo, please run `pod setup`.
? ~ git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master
—— 參考自:https://lamjack.github.io/2016/cocoapods-install-and-use/
搜索相關(guān)框架
打開(kāi)終端,輸入以下命令:
pod search 框架名
# 如,搜索網(wǎng)絡(luò)框架 AFNetworking
pod search AFNetworking
鏈接:mac終端命令
移除 trunk 源
如果執(zhí)行 pod
相關(guān)命令時(shí),顯示 CDN 無(wú)法連接、連接超時(shí)之類(lèi)的情況,可以移除 trunk 源,然后在 Podfile
文件中第一行指明依賴庫(kù)的來(lái)源地址,不使用默認(rèn) CDN。
$ pod repo remove trunk
項(xiàng)目中使用:
-
創(chuàng)建 Podfile 文件。
新建 Xcode 項(xiàng)目,在終端里 cd 到項(xiàng)目的主文件夾(就是包含 項(xiàng)目.xcodeproj 的文件)。執(zhí)行命令:
# 1. 創(chuàng)建 Podfile 文件 $ pod init # 2. 編輯 Podfile 文件 $ vim Podfile
-
編輯 Podfile 文件。
執(zhí)行
vim Podfile
命令后,會(huì)打開(kāi)上一步驟創(chuàng)建的Podfile
文件,你需要通過(guò)vim
編輯此文件,默認(rèn)進(jìn)入命令模式。編輯 Podfile 文件時(shí),至少需要會(huì)使用的幾個(gè) shell 命令:
鍵盤(pán)命令 描述 i
input,輸入模式 ESC
從輸入模式退出到命令模式 shift
+:
在命令模式下,輸入該鍵盤(pán)組合命令,就會(huì)進(jìn)入末行模式 wq
在末行模式下,輸入 wq
,即 write and quit,保存并退出! 保存文件后,安裝第三方庫(kù)。
更新依賴庫(kù),執(zhí)行pod update
(記得cd 到項(xiàng)目主文件下)。
安裝依賴庫(kù),執(zhí)行pod install
。安裝完成,此時(shí)項(xiàng)目文件中會(huì)多出 .xcworkspace 文件,以后就通過(guò)它來(lái)打開(kāi)項(xiàng)目。
Podfile 文件示例
# 指明依賴庫(kù)的來(lái)源地址,不使用默認(rèn) CDN
source '[https://github.com/CocoaPods/Specs.git](https://github.com/CocoaPods/Specs.git)'
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
# 屏蔽所有第三方框架警告
inhibit_all_warnings!
target 'ProjectName' do
# Uncomment this line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for ProjectName
# 網(wǎng)絡(luò)庫(kù)
pod 'AFNetworking'
pod 'YTKNetwork'
pod 'SDWebImage'
pod 'Reachability', '~> 3.2'
# 服務(wù)類(lèi)
pod 'QQ_XGPush'
pod 'Bugly'
# UI
pod 'Masonry'
pod 'MBProgressHUD'
pod 'MJRefresh'
pod 'EAIntroView'
pod 'SDCycleScrollView'
pod 'DZNEmptyDataSet'
pod 'XLForm'
pod 'SHSPhoneComponent'
pod 'BEMCheckBox'
pod 'SCLAlertView-Objective-C'
# 工具組件類(lèi)
pod 'ChameleonFramework'
pod 'FDFullscreenPopGesture'
pod 'IQKeyboardManager'
pod 'YYKit'
pod 'UIAlertController+Blocks'
pod 'LBXScan/LBXNative'
pod 'LBXScan/UI'
pod 'UITableView+FDTemplateLayoutCell'
end
target 'ProjectNameTests' do
inherit! :search_paths
end
"Podfile" 52L, 999C
More:
Podfile 版本號(hào)含義:
= version 要求版本大于或者等于version,當(dāng)有新版本時(shí),都會(huì)更新至最新版本
< version 要求版本小于version,當(dāng)超過(guò)version版本后,都不會(huì)再更新
<= version 要求版本小于或者等于version,當(dāng)超過(guò)version版本后,都不會(huì)再更新
~> version 比如 version=1.1.0 時(shí),范圍在[1.1.0, 2.0.0)。注意2.0.0是開(kāi)區(qū)間,也就是不包括2.0.0。
常用命令 Cheatsheet
以下是我整理的一份 Cocoapods 常用命令。
###### 安裝 Cocoapods ######
# 更新 gem 版本
$ sudo gem update --system
# 查看已安裝的 Ruby 版本
$ gem -v
# 替換 Ruby 源
$ gem sources --remove [https://rubygems.org/](https://rubygems.org/)
$ gem sources -add [https://gems.ruby-china.com/](https://gems.ruby-china.com/)
# 查看驗(yàn)證鏡像源
$ gem sources -l
# 卸載 Cocoapods
$ sudo gem uninstall cocoapods
# 重新安裝 Cocoapods 到指定目錄
$ sudo gem install cocoapods -n /usr/local/bin
# 查看當(dāng)前 Cocoapods 版本
$ pod --version
# 更新本地的 Cocoapods 列表
$ pod repo update
# 移除 trunk 源
$ pod repo remove trunk
###### 項(xiàng)目中使用 ######
# 搜索框架
$ pod search 框架名
# 終端導(dǎo)航到項(xiàng)目目錄下
$ cd [Project]
# 創(chuàng)建 Podfile 文件
$ pod init
# 編輯 Podfile 文件
$ vim Podfile
# 安裝
$ pod install
# 更新所有依賴的開(kāi)源庫(kù)
$ pod update
$ pod install --no-repo-update
$ pod update --no-repo-update
# 查看依賴庫(kù)版本信息
$ pod install --verbose --no-repo-update
常見(jiàn)錯(cuò)誤
錯(cuò)誤 1
Error fetching http://ruby.taobao.org/:
bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)
解決方案:把安裝流程中 gem sources -a http://ruby.taobao.org/
改為 gem sources -a https://ruby.taobao.org/
錯(cuò)誤 2
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod
解決方案:蘋(píng)果系統(tǒng)升級(jí)OS X EL Capitan后會(huì)出現(xiàn)的插件錯(cuò)誤,將安裝流程安裝CocoaPods 的 (1) sudo gem install cocoapods
——>改為 sudo gem install -n /usr/local/bin cocoapods
即可。
錯(cuò)誤 3
ERROR:The dependency is not used in any concrete target
The dependency AFNetworking is not used in any concrete target
解決方案:
-
安裝cocoapods的預(yù)覽版本
sudo gem install cocoapods --pre
-
修改Podfile格式
platform :ios, '8.0' target 'MyApp' do pod 'AFNetworking', '~> 2.6' pod 'ORStackView', '~> 3.0' end
*里面的 MyApp 記得替換為自己攻城里面的target。
-
更新pod
pod install / pod update
錯(cuò)誤 4
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
SSL證書(shū)錯(cuò)誤,解決方案參考:https://gems.ruby-china.org/
vim ~/.gemrc # 配置該文件,關(guān)閉SSL驗(yàn)證
文件如下:
---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://gems.ruby-china.org/
:ssl_verify_mode: 0 # 新增此處代碼
:update_sources: true
:verbose: true
錯(cuò)誤 5
RPC failed; curl 18 transfer closed with outstanding read data remaining
解決方案,增加緩沖區(qū)內(nèi)存:
git config http.postBuffer 524288000
錯(cuò)誤 6
Xcode 9.3 下運(yùn)行 pod init
報(bào)錯(cuò):
Error
RuntimeError - [Xcodeproj] Unknown object version.
/usr/local/lib/ruby/gems/2.4.0/gems/xcodeproj-1.5.2/lib/xcodeproj/project.rb:217:in `initialize_from_file'
/usr/local/lib/ruby/gems/2.4.0/gems/xcodeproj-1.5.2/lib/xcodeproj/project.rb:102:in `open'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/lib/cocoapods/command/init.rb:41:in `validate!'
/usr/local/lib/ruby/gems/2.4.0/gems/claide-1.0.2/lib/claide/command.rb:333:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/lib/cocoapods/command.rb:52:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/bin/pod:55:in `<top (required)>'
/usr/local/bin/pod:23:in `load'
/usr/local/bin/pod:23:in `<main>'
解決方法:安裝Cocoapods預(yù)覽版本:
sudo gem install cocoapods --pre
參考:RuntimeError - [Xcodeproj] Unknown object version.
錯(cuò)誤 7
pod update
報(bào)錯(cuò),(Gem::GemNotFoundException):
$ pod update
Traceback (most recent call last):
2: from /usr/local/bin/pod:23:in `<main>'
1: from /usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)
解決方案:
# 更新 gem 版本
sudo gem update --system
# 卸載 cocoapods
gem uninstall cocoapods
# 重新安裝 cocoapods 到指定目錄
sudo gem install cocoapods -n /usr/local/bin
默認(rèn)情況下,安裝 cocoapods 時(shí)(sudo gem install cocoapods
)會(huì)被安裝到 /usr/bin
目錄下,但是蘋(píng)果為了系統(tǒng)安全,該目錄禁止任何寫(xiě)入,root 用戶也不能。
參考
- 用CocoaPods做iOS程序的依賴管理 @唐巧 (閱讀難度:★★)
- iOS 最新版 CocoaPods 的安裝流程 (閱讀難度:★)
- CocoaPods 公有庫(kù)
- Cocoapods 入門(mén) @不會(huì)開(kāi)機(jī)的男孩
-
看一遍就會(huì)的CocoaPods的安裝和使用教程
概述:第三方庫(kù)依賴管理工具,管理第三方庫(kù)。 - CocoaPods安裝和使用教程
- iOS 靜態(tài)庫(kù),動(dòng)態(tài)庫(kù)與 Framework
- http://www.lxweimin.com/p/8af475c4f717
- http://ios.jobbole.com/90957/