- GitHub: CocoaPods
- star: 12.4k
它是用 Ruby 寫的盟庞,并劃分為多個 gem 包阶剑。它依賴于 Ruby 環(huán)境跃巡。
安裝步驟
1. 更新系統(tǒng) Ruby 環(huán)境
# 這一步驟需要科學上網(wǎng)
$ sudo gem update --system
# 查看已安裝的 Ruby 版本(最新版本:3.0.6,截止20200430)
$ gem -v
2. 安裝 CocoPods 前先替換鏡像源
默認的鏡像資源服務器被天朝給墻了牧愁。所以需要先更換源地址素邪,然后再安裝。
- 移除原先的 Ruby 源:
$ gem sources --remove https://rubygems.org/
- 指定為 Ruby China 的鏡像源:
$ gem sources --add https://gems.ruby-china.com/
?? 相關鏈接:Ruby China 的 RubyGems 鏡像上線
- 驗證新源是否替換成功:
$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
3. 安裝 CocoaPods
使用 ruby 的 gem 命令下載并安裝 CocoaPods递宅。
$sudo gem install cocoapods
備注:以上命令在 Mac OS 系統(tǒng)升級到 OS X EL Capitan 版本后需要改為 :$ sudo gem install -n /usr/local/bin cocoapods
$ pod setup
使用 CocoaPods 的鏡像索引
所有項目的 Podspec 文件都托管在 https://github.com/CocoaPods/Specs 中。第一次執(zhí)行 pod setup
時苍狰,CocoaPods 會將這些 Podspec 索引文件更新到本地的 ~/.cocoapods/
目錄下办龄,該索引文件較大且更新非常緩慢。
將 CocoaPods 設置成 gitcafe 或者 occhina 鏡像淋昭,執(zhí)行索引更新時會快很多俐填。
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 上的鏡像。
添加新源時報錯:
? ~ 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/
搜索相關框架
打開終端翔忽,輸入以下命令:
pod search 框架名
# 如英融,搜索網(wǎng)絡框架 AFNetworking
pod search AFNetworking
鏈接:mac終端命令
移除 trunk 源
如果執(zhí)行 pod
相關命令時,顯示 CDN 無法連接歇式、連接超時之類的情況驶悟,可以移除 trunk 源,然后在 Podfile
文件中第一行指明依賴庫的來源地址材失,不使用默認 CDN痕鳍。
$ pod repo remove trunk
項目中使用:
-
創(chuàng)建 Podfile 文件。
新建 Xcode 項目龙巨,在終端里 cd 到項目的主文件夾(就是包含 項目.xcodeproj 的文件)笼呆。執(zhí)行命令:
# 1. 創(chuàng)建 Podfile 文件 $ pod init # 2. 編輯 Podfile 文件 $ vim Podfile
-
編輯 Podfile 文件。
執(zhí)行
vim Podfile
命令后旨别,會打開上一步驟創(chuàng)建的Podfile
文件诗赌,你需要通過vim
編輯此文件,默認進入命令模式秸弛。編輯 Podfile 文件時铭若,至少需要會使用的幾個 shell 命令:
鍵盤命令 描述 i
input洪碳,輸入模式 ESC
從輸入模式退出到命令模式 shift
+:
在命令模式下,輸入該鍵盤組合命令奥喻,就會進入末行模式 wq
在末行模式下偶宫,輸入 wq
,即 write and quit环鲤,保存并退出纯趋! 保存文件后,安裝第三方庫冷离。
更新依賴庫吵冒,執(zhí)行pod update
(記得cd 到項目主文件下)。
安裝依賴庫西剥,執(zhí)行pod install
痹栖。安裝完成,此時項目文件中會多出 .xcworkspace 文件瞭空,以后就通過它來打開項目揪阿。
Podfile 文件示例
# 指明依賴庫的來源地址,不使用默認 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)絡庫
pod 'AFNetworking'
pod 'YTKNetwork'
pod 'SDWebImage'
pod 'Reachability', '~> 3.2'
# 服務類
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'
# 工具組件類
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 版本號含義:
= version 要求版本大于或者等于version咆畏,當有新版本時南捂,都會更新至最新版本
< version 要求版本小于version,當超過version版本后旧找,都不會再更新
<= version 要求版本小于或者等于version溺健,當超過version版本后,都不會再更新
~> version 比如 version=1.1.0 時钮蛛,范圍在[1.1.0, 2.0.0)鞭缭。注意2.0.0是開區(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/)
# 查看驗證鏡像源
$ gem sources -l
# 卸載 Cocoapods
$ sudo gem uninstall cocoapods
# 重新安裝 Cocoapods 到指定目錄
$ sudo gem install cocoapods -n /usr/local/bin
# 查看當前 Cocoapods 版本
$ pod --version
# 更新本地的 Cocoapods 列表
$ pod repo update
# 移除 trunk 源
$ pod repo remove trunk
###### 項目中使用 ######
# 搜索框架
$ pod search 框架名
# 終端導航到項目目錄下
$ cd [Project]
# 創(chuàng)建 Podfile 文件
$ pod init
# 編輯 Podfile 文件
$ vim Podfile
# 安裝
$ pod install
# 更新所有依賴的開源庫
$ pod update
$ pod install --no-repo-update
$ pod update --no-repo-update
# 查看依賴庫版本信息
$ pod install --verbose --no-repo-update
常見錯誤
錯誤 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/
錯誤 2
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod
解決方案:蘋果系統(tǒng)升級OS X EL Capitan后會出現(xiàn)的插件錯誤,將安裝流程安裝CocoaPods 的 (1) sudo gem install cocoapods
——>改為 sudo gem install -n /usr/local/bin cocoapods
即可甸饱。
錯誤 3
ERROR:The dependency is not used in any concrete target
The dependency AFNetworking is not used in any concrete target
解決方案:
-
安裝cocoapods的預覽版本
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
錯誤 4
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
SSL證書錯誤,解決方案參考:https://gems.ruby-china.org/
vim ~/.gemrc # 配置該文件柜候,關閉SSL驗證
文件如下:
---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://gems.ruby-china.org/
:ssl_verify_mode: 0 # 新增此處代碼
:update_sources: true
:verbose: true
錯誤 5
RPC failed; curl 18 transfer closed with outstanding read data remaining
解決方案搞动,增加緩沖區(qū)內存:
git config http.postBuffer 524288000
錯誤 6
Xcode 9.3 下運行 pod init
報錯:
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預覽版本:
sudo gem install cocoapods --pre
參考:RuntimeError - [Xcodeproj] Unknown object version.
錯誤 7
pod update
報錯,(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
默認情況下渣刷,安裝 cocoapods 時(sudo gem install cocoapods
)會被安裝到 /usr/bin
目錄下鹦肿,但是蘋果為了系統(tǒng)安全,該目錄禁止任何寫入辅柴,root 用戶也不能箩溃。