CocoaPods 是用 Ruby 构建的,并且可以使用 macOS 上可用的默认 ruby 进行安装。
使用ruby -v可查看 是否安装ruby
//查看ruby源 gem sources -l
ruby源在墙内是访问不到的,需要置换为国内
//删除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 源(国内连接官方的 RubyGems 源有时不太稳定,可以更换为国内的镜像源,比如 Ruby China 的镜像源。)
gem source --remove https://rubygems.org/ (移除默认源)
gem source -a https://gems.ruby-china.com/ (添加 Ruby China 镜像源)
再次运行 gem source -l 命令,确认新的源已经添加成功,并且列表中没有默认的官方源。
或者可尝试使用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索引安装到到本地。
没几秒就出现成功: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
中间如果出现 port 443: Connection refused则需要配置一下hosts
sudo vi /etc/hosts加上140.82.112.3 github.com
二、CocoaPods使用
1、进入要安装框架的项目的.xcodeproj同级文件夹,在该文件夹中新建一个文件Podfile, 执行如下命令
pod init
2、编辑Podfile
vim Podfile
platform :ios, '9.0'
target 'B' do
pod 'AFNetworking','~>4.0.1'
end
3、执行pod,终端输入 pod install
三、后续更新升级系统之后pod update出现下面错误。猜测可能是因为系统自带的Ruby太旧了,而且新系统可能也将不再自嵌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其余相关的组件
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
结果报错了: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```
照终端提示的内容,将相关的配置添加到你的 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.设置全局使用的 Ruby 版本:```rbenv global 3.2.2``` ```rbenv rehash```
6.验证 Ruby 版本是否切换成功,```ruby -v```
如果出现切换不成功,检查步骤2的rbenv init初始化配置。