CocoaPods是iOS最常用的类库管理工具。
Cocoapods安装过程
首先把终端打开,然后在$后面输入
1.升级Ruby环境
gem update --system
输入结果

如果没有权限去升级Ruby ?就输入
sudo gem update --system
这时候会让你输入密码,切记输入密码的时候,光标是不会动的,直接按回车
显示的结果是这样的就妥了

可能会遇到一下错误:
gem update --system ===> ERROR
详细信息如下:
Updating rubygems-update
Fetching: rubygems-update-2.6.7.gem (100%)
Successfully installed rubygems-update-2.6.7
Parsing documentation for rubygems-update-2.6.7
Installing ri documentation for rubygems-update-2.6.7
Installing darkfish documentation for rubygems-update-2.6.7
Parsing documentation for rubygems-update-2.6.7
Installing RubyGems 2.6.7
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /Library/Ruby/Site/2.0.0/rubygems/basic_specification.rb
或者 sudo gem update --system ===> ERROR
详细信息如下:
Updating rubygems-update
Fetching: rubygems-update-2.6.7.gem (100%)
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/update_rubygems
bobbobbob:~ apple$ sudo gem update --system
Updating rubygems-update
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/update_rubygems
解决上述Operation not permitted,使用sudo gem update -n /usr/local/bin --system
2.换掉Ruby镜像
首先移除现有的Ruby镜像
gem sources --remove https://rubygems.org/
添加国内最新镜像源
gem source -a https://gems.ruby-china.com/
查看当前镜像
gem sources -l
显示的结果是这样的就妥了
*** CURRENT SOURCES ***
3.安装CocoaPods
sudo gem install cocoapods
如果出现这个了,指的是你没有权限

输入这个
sudo gem install -n /usr/local/bin cocoapods
如果显示这个表示安装成功
9 gems installed
再执行这个
pod setup
当出现Setup completed的时候说明已经完成了(中间有个下载的过程小有几百M要等会)
重点来了,注意
最新版的MacOS Catalina系统命令行执行pod setup命令直接结束啦;
莫着急,我们手动安装本地库,速度绝对快
命令行执行以下操作
git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
或者使用下面的,国内镜像,速度快些
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
4.CocoaPods的使用
1.首先我们搜索一下第三方库
pod search AFNetworking
结果是这样的就妥了(输入q回到终端控制界面)

2.在工程中创建一个Podfile文件
要想在你的工程中创建Podfile文件,必须先要进到该工程目录下
//把自己项目直接拖拽就阔以了
cd /Users/liyang/Desktop/demo
在输入,你的工程目录下你就看到了一个Podfile文件
touch Podfile
3.编辑你想导入的第三方库的名称及版本
用vim编辑Podfile文件
vim Podfile
就出现这个编辑页面了

然后输入i键
下面的"Podsfile" 0L, 0C将变成-- INSERT --
就可以编辑文字啦,输入以下文字
platform :ios, '8.0'
target 'demo' do
pod 'SDWebImage',
end
这时候要退回去啦,先按esc键,再按shift键+:键,再输入wq,最后按回车键
4.把该库下载到Xcode中
pod install
这时候就开始下载了,需要一段时间就好了
打开工程目录会看到多了一个后缀名为.xcworkspace文件就稳了

五、卸载当前版本
sudo gem uninstall cocoapods
后续
xcode卸载后从官网下载的然后路径在Downloads里面 pod九报错了:
/usr/local/lib/ruby/gems/2.3.0/gems/cocoapods-1.5.3/lib/cocoapods/command.rb:118:in git_version': Failed to extract git version fromgit --version` ("xcrun: error: active developer path ("/Users/changcai/Downloads/Xcode.app/Contents/Developer")
好可爱的问题;啊不知道是哪个意思;网上百度后有说一句命令搞定的,就试了试:sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer 然后就OK啦
在终端中使用xcode-select -print-path 可以xcode查看路径
移除已添加的三方库:
pod install --verbose --no-repo-update
只安装新增库,不更新已安装的库
pod install
把Podfile内全部的库更新重新安装
pod install --verbose --no-repo-update
该命令只安装新添加的库,已更新的库忽略
pod update 库名 --verbose --no-repo-update
该命令只更新指定的库,其它库忽略
链接:https://www.jianshu.com/p/0ba9edf8428d
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。