iOS pod install 失败深度解决方案

6,118 阅读2分钟

由于众所周知的原因,Github 访问速度很慢,造成 pod install 执行中会遇到类似以下的报错: [!] Error installing SobotKit [!] /usr/local/bin/git clone https://github.com/ZCSDK/SobotKit.git /var/folders/nz/zvr167xs1zx791xd4334hxnh0000gp/T/d20220304-73308-57w6i5 --template= --single-branch --depth 1 --branch 3.0.8 Cloning into '/var/folders/nz/zvr167xs1zx791xd4334hxnh0000gp/T/d20220304-73308-57w6i5'... fatal: unable to access 'https://github.com/ZCSDK/SobotKit.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

失败原因

首先我们需要了解 pod install 的过程。 当执行 pod install 的时候,程序会从工程里找到 Podfile 定义的仓库以及地址,并且在 ~/.cocoapods/repos/master/Specs目录下新建当前仓库的 *.podspec.json 资源文件。 比如 pod install SobotKit 时,首先对 SobotKit 进行 MD5 计算获取摘要,取 MD5 的前三位 5e7 建立文件夹:~/.cocoapods/repos/master/Specs/5/e/7/Sobotkit,从该目录下找到你所需的版本,然后打开 *.podspec.json 找到 source 节点,我们可以看到 source 节点下面是 github 的地址,就是该 github 地址太慢导致的上面的错误,那么该怎么解决呢?

github 加速方案

容我先介绍一下 github 访问慢、失败的三种解决方案。

  1. 全局替换 github 域名:
  • git config --global url."https://hub.fastgit.org".insteadOf https://github.com
  • git config --global url."https://gitclone.com".insteadOf https://github.com
  • git config --global url."https://github.com.cnpmjs.org".insteadOf https://github.com 可以挨个试试,不保证有效。
  1. 将需要的 github 仓库转换成 gitee 仓库,注册一个 gitee 账号,进入 gitee.com/projects/im… ,将 github 仓库导入。git clone 的时候用 gitee 仓库替换 github 地址。

  2. 源替换方式,使用 github.zhlh6.cn/ 工具网站,获取加速地址。

解决方案

github 加速问题可以使用上述三种方式解决。如果第一种方案有效,pod install 即可成功执行。若第一种方案仍失败的话,可以使用第二种或者第三种方案,将 *.podspec.json 的 source 替换成第二种或者第三种生成的源。