CocoaPods是一个很出名的库管理工具。安装教程可以看后面参考文章里面唐巧博客写的文章,安装之前先检测下电脑环境可以避免问题,可以这几个检测之后再开始安装。

一、开始安装之前检查你的RubyGems 版本

请尽可能用比较新的 RubyGems 版本,建议 2.6.x 以上。

执行命令行:

gem -v

屏幕快照 2016-07-08 16.20.14.png

如果版本过低,执行命令行升级RubyGems版本

gem update --system

这个命令行需要翻墙。

二、修改Ruby源

执行下面这个命令行删除以前的源,因为这个源被墙了

gem sources --remove https://rubygems.org/

执行下面这个命令行添加一个新的源,淘宝的已经不好用了,推荐用这个

gem sources --add https://gems.ruby-china.org/

添加完毕之后查看源列表

gem sources -l

确保只有 gems.ruby-china.org

三、升级Ruby的版本

执行下面这个命令行查看当前电脑的ruby版本,需要高于2.2,否则到时候安装cocoaPods的时候会报错

ruby -v

点击查看原图

可以使用RVM对Ruby升级

执行这个命令行下载相关组件

curl -L get.rvm.io | bash -s stable

执行下面命令行添加配置

source ~/.bashrc

执行下面命令行添加配置

source ~/.bash_profile

执行下面命令行列出已知的ruby版本可安装版本

rvm list known

执行下面命令行安装ruby 2.3 

rvm install 2.3

结束之后再次查看ruby版本即可。

下面就可以开始安装了,安装方法查看参考文章里面文章即可。

四、cocoapods安装

执行下面这条命令开始安装

sudo gem install cocoapods

安装完毕之后执行下面这条命令下载cocoapods的数据

pod setup

执行完毕之后把数据下载完毕即可,如果一直卡在Setting up CocoaPods master repo可以看下面

五、cocoapods可能出现的错误

1、卡在Setting up CocoaPods master repo

当执行pod setup的时候,页面在Setting up CocoaPods master repo是正在下载cocoapods的数据到.cocoapods/这个文件夹中,可以进入这个文件夹中,执行du -sh *来查看下载进度

cd .cocoapods/
du -sh *

1.1、如果报下面的错

xcrun: error: active developer path ("/Volumes/Xcode/Xcode.app/Contents/Developer") does not exist, use `xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools (or see `man xcode-select`)
xcrun: error: active developer path ("/Volumes/Xcode/Xcode.app/Contents/Developer") does not exist, use `xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools (or see `man xcode-select`)
Creating shallow clone of spec repo `master-1` from `https://github.com/CocoaPods/Specs.git`
[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master-1`.
You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.

这是因为xcode的路径错误,需要设置xcode的路径

sudo xcode-select -switch /Applications/Xcode.app 
pod update --no-repo-update

1.2、如果报下面错

SSLRead() return error -xx

这种一般都是网络问题,也可以升级下ssl和crul

$ brew remove git
$ brew remove curl
$ brew install --with-openssl curl

1.3、一直卡在56K不动,或者报下面错

Unable to add a source with url https://github.com/CocoaPods/Specs.git named master . You can try adding it manually in ~/.cocoapods/repos or via pod rep

这个还是网络的问题,下载不到github这个库,可以改变下载地址继续下载

解决方法可以使用国内别人下载过来的:

1.执行:

pod repo remove master

2.添加:

pod repo add master https://git.coding.net/CocoaPods/Specs.git

3.下载:

git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master

4.最后执行:

pod repo update

 2、搜索时报错

pod search AFNetworking

输入过后它可能会报:

[!] Unable to find a pod with name, author, summary, or descriptionmatching `AFNetworking`

解决方案是:

输入:

rm ~/Library/Caches/CocoaPods/search_index.json

后在一次输入:

pod search AFNetworking

就可以咯。

3、使用时报错

cocoapods的使用,首先来到你的项目文件:

cd 项目文件的位置

然后,添加一个Podfile文件: vim Podfile

然后按i进入插入模式,进行编辑,在文件中输入以下内容:

注意百度上很多旧版本输入的类容:

platform :ios, '8.1'
pod 'AFNetworking', '~> 2.0'

它会报:[!] The dependency `AFNetworking (~> 3.0)` is not used in any concrete target.

现在版本升级官方给的文档是:

platform :ios, '8.0'
target '你的项目名称' do
pod 'AFNetworking', '~> 3.0'
end

输入完成后按ESC退出编辑模式,最后输入:wq保存并退出文件。此时可以发现在项目目录下多了一个Podfile的文件,请注意这个文件必须与.xcodeproj在同一目录下。

或者直接cd到工程的文件,执行

pod init

会自动创建该文件

最后:

pod install

没报错就可以了。

当修改这个文件的时候,执行一次

pod update

即可

如果太慢了,就可以使用 --no-repo-update禁止其做索引更新操作,比如下面操作

pod install --no-repo-update
pod update --no-repo-update

五、参考文章

  1. 用CocoaPods做iOS程序的依赖管理

  2. RubyGems 镜像- Ruby China

  3. Mac上更新Ruby

  4. 2016 cocoapods的安装和使用以及版本升级遇到的问题

  5. curl 无法访问 https 协议

  6. git: how to solve "SSLRead() return error -9806" in OSX using brew

  7. xcode 升级 cocoapods无法使用

  8. 无限卡在Setting up CocoaPods master repo

  9. 主题 : 使用CocoaPods时遇到pod setup失败的解决办法(2016.8.12更新

  10. https://github.com/CocoaPods/Specs.git 国内镜像,每日自动更新


☟☟可点击下方广告支持一下☟☟

最后修改:1970 年 01 月 01 日
请我喝杯可乐,请随意打赏: ☞已打赏列表