CocoaPods 是 iOS 日常开发所使用的包管理器,它把 Rails 里的 Gem 带进了 macOS 中,让 iOS 和 macOS 管理自己的第三方依赖。
接下来介绍几个概念:
rbenv:管理 Ruby 版本的内容
rails:著名开发框架。详细看 https://ruby-china.github.io/rails-guides/
bundle:是 rails 框架里面安装 Gemfile 指定的各种库的工具。在配置文件 gemfile 里说明你的应用依赖哪些第三方包,他自动帮你下载安装多个包,并且会下载这些包依赖的包
gem:是 ruby 的其中一个“程序”叫 RubyGems,在终端使用的 gem 命令,是指通过 RubyGems 管理 Gem 包
pod:是 CocoaPods 的管理 iOS 的第三方库工具

接下来介绍一下具体的 CocoaPods 安装流程,首先你需要一台 Mac [doge]

基于 rbenv 来安装 CocoaPods

  1. 安装 brew$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  2. 安装 rbenv$ brew install rbenv

  3. 安装 ruby-build$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

  4. 安装 ruby-china 镜像$ git clone https://github.com/andorchen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror

  5. 使用 rbenv 安装 ruby 版本:$ rbenv install 2.2.3。你可以选择安装自己需要的版本。

    然后在 ~/.zsh_rc 里添加:

    1
    2
    3
    # rbenv init 需要
    export PATH="$HOME/.rbenv/bin:$PATH"
    eval "$(rbenv init -)"

    注意是 bash 用户是在 ~/.bashrc 里, zsh 用户是 ~/.zshrc ,然后重启终端

  6. 设置全局的 ruby 版本:$ rbenv global 2.2.3

  7. 去掉原生的 gem 源,替换为 ruby-china 源: $ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
  8. 更新 gem 版本:$ gem update --system

  9. 安装所需要的 CocoaPods 版本:$ gem install cocoapods -v 1.1.0, 不要用 sudo。系统大约会给你安装 20 个左右 gems,都是 cocoapods 所依赖的内容

  10. $ which pod 得到信息是: $ /Users/yourusername/.rbenv/shims/pod

  11. 安装 bundler:$ gem install bundler

首次使用的速度问题

当你运行 pod setup 的时候可能会看到这样的信息:

1
2
3
4
5
6
[Enchanting] pod setup
Setting up CocoaPods master repo
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --depth=1

Cloning into 'master'...
fatal: unable to access 'https://github.com/CocoaPods/Specs.git/': transfer closed with outstanding read data remaining

原因是:master 的 podspec 文件都托管在 https://github.com/CocoaPods/Specs 上面。
当执行 pod setup 或者 pod install 的时候,CocoaPods 自动把托管在 github 上的文件下载到本地的 ~/.cocoapods/repos 目录下,但是因为这个索引文件非常大,所以第一次更新时非常慢。

解决办法(默认你已经有了 shadowsocks)
挂代理:git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
取消代理:git config --global --unset http.https://github.com.proxy
然后再进行 pod setup,这次速度就会快很多。

另外当运行 Xcode 的时候,可能会报 ruby 相关的错误。因为当前 macOS Sierra 使用的是 oh-my-zsh 的命令终端管理,但是 Xcode 本身跑的是 .bash 而非 .zshrc 里的内容。所以需要加上下面这句话,使得 Xcode 在运行的时候跑的是 local 的Ruby 环境,也就是 /Users/xxxxxxx/.rbenv/shims/ruby 下的版本。打开 ~/.bash_profile,添加下面这行:

1
export PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"

Reference:

Enjoy.