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)
解决办法可使用brewhome安装(brewhome安装方法请看另一篇文章)
brew install cocoapods
安装完之后还需要输入命令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(参考上面)