CocoaPods|安裝流程與使用

系統環境:macOS CataLina 10.15.2

1. 安裝 RVM

  • 開始安裝
curl -L get.rvm.io | bash -s stable
  • 進入目錄
source ~/.bashrc
source ~/.bash_profile
  • 成功后查看版本
rvm -v

2. Ruby升級

  • 獲取Ruby版本
ruby -v
  • 對比Ruby版本
rvm list known
  • 更新Ruby
rvm install 2.7.0-preview1

3. 更換Ruby源

  • 查看當前源
gem sources -l
  • 移除默認源
gem sources --remove https://ruby.taobao.org/
  • 替換源
gem sources -a https://gems.ruby-china.com/

(尾巴這條斜桿需要斟酌移除)

4. 安裝CocoaPods

  1. OS X 10.11之前系統的安裝cocoapods 指令
$ sudo gem install cocoapods
  1. OS X 10.11以后系統的安裝cocoapods 指令
$ sudo gem install -n /usr/local/bin cocoa pods
  • 安裝
pod setup
  • 完成之后
pod --version //查看版本
  • 測試一下
pod search afnetworking

使用流程

cd 目標路徑
pod init
  • 然后操作Podfile文件,一般使用vim
  # Uncomment the next line to define a global platform for your project
platform :ios, '8.0'

target 'ProjectName' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  pod 'AFNetworking'

end
  • 完成它
pod install
  • 如若比較慢,可使用{??},無須更新本地庫情況下(更新同理)
pod install --no-repo-update

打開白色工程 projectName.xcworkspace

5. 更新CocoaPods

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

6. 更新CocoaPods 1.8.+版本本地庫

  • 1.8+強制安裝了新的源trunk,采用cdn替換了原本的master
  • 直接通過cdn方式更新本地庫
iMac:mac$ pod install
    Analyzing dependencies
    Adding spec repo `trunk` with CDN `https://cdn.cocoapods.org/`
    Downloading dependencies
    Generating Pods project
    Integrating client project
    Pod installation complete! There are 28 dependencies from the Podfile and 32 total pods installed.

附錄

  • 查看已下載各Pod版本
    可直接打開Podfile.lock查看,例如:
PODS:
  - UMCAnalytics (6.0.5):
    - UMCCommon
  - UMCCommon (2.1.1)
  - UMCSecurityPlugins (1.0.6)

問題與解決


更新系統Or Xcode產生的問題
  1. 在更新OS或Xcode之后
    出現 Failed to extract git version from ·git --version· ??
macdeMacBook-Air:~ mac$ pod --version
Traceback (most recent call last):
    7: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/ruby_executable_hooks:24:in `<main>'
    6: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/ruby_executable_hooks:24:in `eval'
    5: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/pod:23:in `<main>'
    4: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/pod:23:in `load'
    3: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/bin/pod:55:in `<top (required)>'
    2: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:49:in `run'
    1: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:130:in `verify_minimum_git_version!'
/Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:118:in `git_version': Failed to extract git version from `git --version` ("xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun\\n") (RuntimeError)
  • 解決方法
xcode-select --install
  1. 更新系統后若發生 No such file or directory??
xiaowen:~ natsuhirofumi$ pod --version
-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory
  • 解決方法
// 指定缺失路徑安裝
sudo gem install -n /usr/local/bin cocoapods

本地存在舊緩存的問題,如?
[!] CocoaPods could not find compatible versions for pod "SDWebImage":
  In Podfile:
    SDWebImage (~> 5.8.1)

None of your spec sources contain a spec satisfying the dependency: `SDWebImage (~> 5.8.1)`.

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.
  • 調用刷新命令
pod update --verbose

更新CocoaPods 1.8+CDN產生的問題
  • 早在1.7.2 CocoaPods就將源方式切換成CDN方式

CDN support was first introduced in the 1.7 release and was finalized in 1.7.2. It aims to speed up initial setup and dependency analysis dramatically.

  • 但在1.8+中CDN方式成為默認選項

CocoaPods 1.8 switches the CDN as the default spec repo source

  1. RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
    更新本地庫出現報錯
  • 解決方法:

git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master

  • 網絡欠佳可以試試別的源
///因Coding停止維護,可切別的源~
* https://code.aliyun.com/Magi/CocoaPods.git   
* https://gitclub.cn/CocoaPods/Specs.git `推薦`
  • 如還是無法解決,可參照下面DNS污染問題嘗試解決(修改host)
  1. 如果pod install中發現:
You have either: 
* out-of-date source repos which you can update with `pod repo update`.
* mistyped the name or version. 
* not added the source repo that hosts the Podspec to your Podfile.

Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.
  • 可嘗試直接更新最新版本
sudo gem install cocoapods --pre
  • 如果還是不行,可移除緩存重新安裝,或者
    ? + ? + G 進入repos目錄直接刪除master/trunk
// 移除本地master
sudo rm -fr ~/.cocoapods/repos/master
// 移除本地緩存
sudo rm -fr ~/Library/Caches/CocoaPods/
-------------方法分隔線-------------
// repos目錄
~/.cocoapods/repos/
  • 移除本地庫后
// 重新setup,如果很慢可使用問題1的解決方法(git clone)
pod setup --verbose

出現最多的連接問題
  1. 更新時,CocoaPods 1.8.0 的trunk連接失敗的問題

[!] CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/ Response: 400
或者是
[!] CDN: trunk Repo update failed - 70 error(s): CDN: trunk URL couldn't be downloaded: [庫信息], error: Failed to open TCP connection to raw.githubusercontent.com:443
亦或是
[!] CDN: trunk Repo update failed - 14 error(s): CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/[庫路徑].podspec.json Response: Couldn't connect to server

解決方法一(使用舊下載方式)
  • 打開podfile文件,頂部添加{??},指定舊方式下載
//source `源地址`
source 'https://github.com/CocoaPods/Specs.git'
  • 因為脫離trunk方式,亦可移除trunk
pod repo remove trunk
  • 可確認是否已移除trunk
pod repo list
解決方法二(解決DNS污染問題)
  • CDN方式以trunk代替Master
  • 編輯Podfile,將CDN設置為主要來源,1.8+已是默認項
- source 'https://github.com/CocoaPods/Specs.git'
+ source 'https://cdn.cocoapods.org/'
  • 移除本地庫
pod repo remove master
  • ping 出ip 地址
ping raw.githubusercontent.com
  • 獲取ip地址后? + c 退出

    捕獲ip地址

  • 或使用nslookup

nslookup raw.githubusercontent.com

----------------------------------->>
Server:     192.168.31.1
Address:    192.168.31.1#53

Non-authoritative answer:
Name:   raw.githubusercontent.com
Address: 151.101.228.133
  • 修改/etc/hosts
sudo vi /etc/hosts
  • i 編輯模式
// 添加在最后一行
151.101.228.133 raw.githubusercontent.com
如圖所示
  • 刷新DNS緩存
sudo killall -HUP mDNSResponder
  • 重試
pod install

---皆大歡喜---
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.1)
Installing UMCAnalytics (6.0.5)
Installing UMCCommon (2.1.1)
Installing UMCSecurityPlugins (1.0.6)
Generating Pods project
Integrating client project
Pod installation complete! There are 4 dependencies from the Podfile and 4 total pods installed.
  • 同理網絡問題,可獲取對應ip地址寫進host直接訪問

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
## github
13.250.177.223 github.com
69.171.244.12 github.global.ssl.fastly.net
199.232.28.133 raw.githubusercontent.com (真實ip)
## cocoa
157.230.35.153 cdn.cocoapods.org
~ 
/** 
* 說明:
* 未標記真實ip,直接通過nslookup獲取
**/

索引文章 ??

一、[模塊化工程] | 工作空間篇 | .xcworkspace | Development Pods | 混合應用 =?姐妹篇=

  • 關于Xcode工作區間的應用,適用于單項目多端開發,以及整合本地 Development Pod 集成使用

二、模塊化工程 | CocoaPods篇|遠程私有庫搭建流程 [通用工具箱] =姐妹篇?=

  • 手把手搭建遠程庫,以及過程中遇見的問題及解決方法

三、iOS | 關于AppDelegate瘦身的想法與實踐


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容