CocoaPods 简介:
CocoaPods 是一个用 Ruby 写的、负责管理iOS项目中第三方开源库的工具,CocoaPods 能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。
CocoaPods 安装:
下面就正式开始安装 CocoaPods,命令中间可能有空格看不出来,建议直接复制粘贴执行;
因为 Mac 电脑自带 Ruby 环境,我们就只需打开终端开始动手。然而又因为默认情况下我们 Mac 系统自带的 Ruby 环境版本比较低(大概是 2.0.0 或者其他较高版本),但是现在安装CocoaPods 需要 2.2.2 版本及以上的,所以我们需要先升级 Ruby。
打开终端:>_
1、查看当前 Ruby 版本
ruby -v
2、升级 Ruby 环境,首先需要安装 rvm(等待安装完成)
curl -L get.rvm.io | bash -s stable
source ~/.bashrc
source ~/.bash_profile
3、查看 rvm 版本
rvm -v
显示如下:
rvm 1.29.6 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
4、列出 ruby 可安装的版本信息
rvm list known
显示如下:
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.5]
[ruby-]2.5[.3]
[ruby-]2.6[.0-rc1]
ruby-head
......
5、安装一个 ruby 版本(这里选择 2.5.3 版本)
rvm install 2.5.3
注意:安装过程中可能需要输入电脑访问密码,如果你电脑没有安装 Xcode 和 Command Line Tools for Xcode 以及 Homebrew 会自动下载安装,建议提前安装
6、设置为默认版本
rvm use 2.5.3 --default
7、更换源
sudo gem update --system
gem sources --remove https://rubygems.org/
gem sources --add https://gems.ruby-china.com/
8、验证 Ruby 镜像
gem sources -l
显示如下:
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
9、正式安装 CocoaPods
sudo gem install -n /usr/local/bin cocoapods
如果安装了多个 Xcode 使用下面的命令选择(一般需要选择最近的 Xcode 版本)
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
10、安装本地库
pod setup
执行以上命令后,然后经过漫长的等待:
Setting up CocoaPods master repo
$ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Enumerating objects: 289, done.
remote: Counting objects: 100% (289/289), done.
remote: Compressing objects: 100% (225/225), done.
remote: Total 2700260 (delta 110), reused 108 (delta 59), pack-reused 2699971
Receiving objects: 100% (2700260/2700260), 601.58 MiB | 346.00 KiB/s, done.
Resolving deltas: 100% (1595242/1595242), done.
Checking out files: 100% (292818/292818), done.
CocoaPods 1.6.0.beta.2 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.6.0.beta.2
Setup completed
11、可执行下列命令检查是否可用
pod search AFNetworking
成功搜索到相关信息,恭喜你 CocoaPods 已成功完成安装
12、CocoaPods 的具体使用
新建一个 Xcode 工程,使用终端 cd 到工程目录下
创建 Podfile 文件:
pod init
之后就可以在项目目录里看到一个 Podfile 文件
打开 Podfile 文件:
open Podfile
假如你需要添加 AFNetworking 到工程:
pod 'AFNetworking'
保存后退出
开始下载:
pod install
等待添加 AFNetworking,完成导入!
网友评论