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
一、安装CocoaPods
sudo gem install cocoapods// Mac OS X 10.11前 输入这一条
sudo gem install -n /usr/local/bin cocoapods//Mac OS X 10.11后 输入这一条
安装完之后还需要输入命令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
中间如果出现 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
//列出已知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