CocoaPods 是用?Ruby?構(gòu)建的,并且可以使用 macOS 上可用的默認(rèn) ruby 進(jìn)行安裝。
使用ruby -v可查看 是否安裝ruby
//查看ruby源 gem sources -l
ruby源在墻內(nèi)是訪問不到的,需要置換為國內(nèi)
//刪除gem sources -- remove https://rubygems.org/
//添加gem source -a https://gems.ruby-china.com
使用rvm -v可查看 是否安裝rvm,安裝步驟下面有。
一、安裝CocoaPods
sudo gem install cocoapods// Mac OS X 10.11前 輸入這一條
sudo gem install -n /usr/local/bin cocoapods//Mac OS X 10.11后 輸入這一條
如遇到
ERROR:? While executing gem ... (Gem::RemoteFetcher::FetchError)? ? Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://gems.ruby-china.com/quick/Marshal.4.8/cocoapods-0.0.1.gemspec.rz)
解決辦法
更換 RubyGems 源(國內(nèi)連接官方的 RubyGems 源有時不太穩(wěn)定,可以更換為國內(nèi)的鏡像源,比如 Ruby China 的鏡像源。)
gem source --remove https://rubygems.org/ (移除默認(rèn)源)
gem source -a https://gems.ruby-china.com/ (添加 Ruby China 鏡像源)
再次運行?gem source -l?命令,確認(rèn)新的源已經(jīng)添加成功,并且列表中沒有默認(rèn)的官方源。
或者可嘗試使用brewhome安裝:brew install cocoapods(brewhome安裝方法請看另一篇文章)
如遇到
ERROR:? Error installing cocoapods: The last version of securerandom (>= 0.3) to support your Ruby & RubyGems was 0.3.2. Try installing it with `gem install securerandom -v 0.3.2` and then running the current command again securerandom requires Ruby version >= 3.1.0. The current ruby version is 2.7.2.137.
解決辦法升級ruby版本。
安裝完之后還需要輸入命令pod setup??//這條命令是將Github上的開源庫都托管都安裝Podspec索引安裝到到本地。
沒幾秒就出現(xiàn)成功:Setup completed 顯然不對勁。
果然pod search AFNetworking提示錯誤信息
[!]Unable to find a pod with name,author,summary,or description matching`AFNetworking`
解決辦法:
第一種方式:
1、git clone?https://git.coding.net/CocoaPods/Specs.git?~/.cocoapods/repos/master
第二種方式:
1, pod repo remove master
2, cd ~/.cocoapods/repos
3, git clone --depth 1 https://github.com/CocoaPods/Specs.git master
4, rm ~/Library/Caches/CocoaPods/search_index.json
5, pod search AFNetworking
如遇到報錯
Creating search index for spec repo 'master'..?Done!xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
解決辦法:
pod repo update
中間如果出現(xiàn) port 443: Connection refused則需要配置一下hosts?
sudo vi? /etc/hosts加上140.82.112.3??github.com
二、CocoaPods使用
1、進(jìn)入要安裝框架的項目的.xcodeproj同級文件夾,在該文件夾中新建一個文件Podfile, 執(zhí)行如下命令
pod init
2、編輯Podfile?
vim Podfile
platform :ios, '9.0'
target 'B' do
?? pod 'AFNetworking','~>4.0.1'
end
3、執(zhí)行pod,終端輸入 pod install
三、后續(xù)更新升級系統(tǒng)之后pod update出現(xiàn)下面錯誤。猜測可能是因為系統(tǒng)自帶的Ruby太舊了,而且新系統(tǒng)可能也將不再自嵌Ruby 等腳本語言。
Ignoring ffi-1.15.5 because its extensions are not built. Try: gem pristine ffi --version 1.15.5
解決辦法是卸載cocoapods再重裝。
1、卸載cocoapods
sudo gem uninstall cocoapods
//查看Cocoapods其余相關(guān)的組件
gem list --local | grep cocoapods
cocoapods-deintegrate (1.0.5) cocoapods-downloader (1.6.3)cocoapods-plugins (1.0.0) cocoapods-search (1.0.1) cocoapods-trunk (1.6.0) cocoapods-try (1.2.0)
//需使用如下命令逐個刪除
sudo gem uninstall cocoapods-deintegrate
2、安裝 RVM
//安裝
?curl -L https://get.rvm.io | bash -s stable
可能遇到的問題:Failed to connect to raw.githubusercontent.com port 443 after 16 ms: Couldn't connect to server
解決辦法:修改Host文件
sudo vi /etc/hosts
185.199.108.133 raw.githubusercontent.com
安裝完查看版本
source ~/.rvm/scripts/rvm
rvm -v
//列出已知Ruby版本
?rvm list known
//安裝一個 Ruby 版本?
rvm install 2.7.2??
結(jié)果報錯了:Try `brew tap --repair` and make sure `brew doctor` looks reasonable.
解決辦法:粗暴的直接卸載homebrew重裝。
如果遇到下面的錯誤:
UCDdeMacBook-Pro:~ ucd$ rvm install 2.7.2
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/12.6/x86_64/ruby-2.7.2.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system............Failed to update Homebrew, follow instructions at
? ? https://docs.brew.sh/Common-Issues
and make sure `brew update` works before continuing.
.
Error running 'requirements_osx_brew_update_system ruby-2.7.2',
please read /Users/ucd/.rvm/log/1692856450_ruby-2.7.2/update_system.log
Requirements installation failed with status: 1.
解決辦法:先運行下面命令再安裝
rvm autolibs read-only
rvm install 2.7.2
3、安裝CocoaPods(參考上面)
最新情況:使用rvm安裝ruby3.0.0一直報錯。可選擇使用rbenv去管理ruby。但你需要刪除rvm之前安裝的ruby。```sudorvm remove ruby-2.7.2```
1.安裝?rbenv?和?ruby-build:```brew install rbenv ruby-build```
2.初始化?rbenv:``` rbenv init```
照終端提示的內(nèi)容,將相關(guān)的配置添加到你的 shell 配置文件中(比如?~/.zshrc?或?~/.bash_profile)。添加完成后,運行?source ~/.zshrc(如果是 zsh shell)或?source ~/.bash_profile(如果是 bash shell)使配置生效。
首先打開~/.zshrc文件:```vim ~/.zshrc``` ?, 加上 eval "$(rbenv init -)" 和 export PATH="$PATH:$HOME/.rbenv/bin:$PATH" 這2句話 ?,再運行```source ~/.zshrc```使配置生效。
3.查看可用的 Ruby 版本:```?rbenv install -l```
4.安裝可用版本:```rbenv install 3.2.2```
5.設(shè)置全局使用的 Ruby 版本:```rbenv global 3.2.2``` ?```rbenv rehash```
6.驗證 Ruby 版本是否切換成功,```ruby -v```
如果出現(xiàn)切換不成功,檢查步驟2的rbenv init初始化配置。