在搞毕设的交代码之际发现新自用机没装 Cocoapods,经过一些步(cai)骤(keng)总算搞定√ 随手做个记录。
时间:2019.5.22
系统:macOS Mojave 10.14.4
参考:CocoaPods安装方法-2019.01.03 - 简书 & stackoverflow 的各种问题解答
一、升级 Ruby 版本
1. 安装 rvm
curl -L get.rvm.io | bash -s stable
source ~/.bashrc
source ~/.bash_profile
2. 查看目前的 rvm 版本
rvm -v
3. 看 rvm 里的 ruby 可安装版本
rvm list known
提前安装 Homebrew,不然下一步安装容易报错(已踩坑)
安装 Homebrew 命令:
/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
4. 选非 preview 的 ruby 最新版本安装,并设置为默认版本
rvm install 2.6.3
rvm use 2.6.3 —default
5. 更换 ruby 源为 China
sudo gem update —system
gem sources —remove https://rubygems.org/
gem sources —add https://gems.ruby-china.com
gems.ruby-china.com 是目前的最新镜像源,加不加“/”会有域名相关坑(errno 54 等),保守起见建议和镜像源一致,不加“/”
验证更换是否成功:
gem sources -l
应该输出:
** CURRENT SOURCES **
https://gems.ruby-china.com
二、安装 CocoaPods
1. 安装
sudo gem install -n /usr/local/bin cocoapods
没有 -n /usr/local/bin 的话会显示权限问题: You don’t have write permissions for the /usr/bin directory.
2. 本地库
pod setup
这一步时间非常长,还容易在后期报错,可能出现的报错有:
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
和
error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
根据各种探索,有以下解决方法,建议各位依次尝试和排查
1) 首先排查网络问题,换个网速没问题的网,再 pod setup
(尝试后无效)
2)检查上面第一条第5点 Ruby 源的域名设置,是否多加了“/”,再 pod setup
(可以排除errno 60 的报错,但对 errno54 无效)
3) 有威P N的话连上,再 pod setup
(尝试后无效)
4) 按照CocoaPods 安装方法-2019.01.03 - 简书评论 & 官方 issue 中对 errno54 的解决,应该如下手动 clone:
git clone git://cocoapodscn.com/Specs.git ~/.cocoapods/repos/master
这个方案的原理是手动进行的 pod setup,因此原评论中说 “clone 后 pod setup”实际是不合理的,应该是 clone 后去有 Podfile 的工程目录(还没建的话先把第三条的1和2做完)进行 pod install
。
但我尝试 clone 后执行 pod setup
或者pod install
的时候一直卡在
Performing a deep fetch of the `master` specs repo to improve future performance
搜索对于卡在这里的解决方式,大多是删库重装,无奈下耐心等了一番,然后报错:
[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master-1`.
cd ~/.cocoapods/repos
git clone https://github.com/CocoaPods/Specs.git master
也是一种手动 clone 的方式,我猜想因为不是手动建的 master 文件夹所以能够解决一部分问题,因此删库继续尝试,clone 后直接去工程 pod install,但仍然没有成功,猜想是网络很慢的问题。
第二天重启后,确认网络状态,用威PN再次pod install
,终于成功(可能威PN并不是必要因素,只是网速+clone 成功+环境配置好后重启)
3. 检查 pod 是否可用(首次会很慢,可跳过直接去下面的 pod install)
pod search AFNetworking
三、使用 CocoaPods
1. cd 到工程目录下,创建 Podfile 文件
pod init
2. 打开 Podfile 文件,添加 pod 库,保存退出(引号中是工程名)
target ‘Demo’ do
pod ‘AFNetworking’
end
3. 最后终于来到这位熟悉的朋友:pod install
pod install
四、由 project -> workspace 的一些小问题
如果本来项目使用 project 单工程开发,后期引入 pod 库,形成 workspace,会出现一些小问题,随手记录。
1. 导入第三方库后 import 第三方库找不到文件报错
这个问题的根源在于我 pod install
后过于兴奋以至于无视掉了这个警告
[!] The `xxx(工程名)` target overrides the `FRAMEWORK_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-xxx(工程名)/Pods-xxx(工程名).debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
解决方法就像这个可爱的警告所言,在该工程的 target 中: Build Setting —> Search Paths 把两个都修改为 $(inherited) 或者 清除掉原有的设置。
实际上这个设置是头文件的搜索范围,这样就很好理解了。
2. ld: framework not found Pods 报错
解决方法:
成为 workspace 后,现在有两个一级目录,你的工程名和 Pods,把工程名一级目录下的 Pods 二级目录和 Frameworks 二级目录(可能已被标红且在 Finder 找不到了)删除(不放心的话可重新 pod install
)就可以啦
猜想原因: 应该是原有 project 生成的静态库在成为 workspace 后有了别的去处和生成,原有的没有及时删除会影响查找导致报错