一.CocoaPods是什么?

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。

二.CocoaPods的安装

因为Mac电脑自带Ruby环境,我们就只需打开终端开始动手。

(1)、将Ruby 的软件源替换成国内的

ruby 的软件源( https://rubygems.org )使用的是亚马逊的云服务,所以自带的需要翻墙,但我们可以使用如下代码将官方的 ruby 源替换成国内淘宝的源(https://ruby.taobao.org/ )或者是由 Ruby China 社区专注维护的这个源(https://gems.ruby-china.org/ )。(我在镜像的时候开始用的是淘宝的,但会卡死,可能是不能用了,也可能是网速慢,在后面讲“坑”的时候会说)。在这我用的是Ruby China 社区专注维护的这个源(https://gems.ruby-china.org/)。

##(注意:复制别人的命令时不要将$也复制,终端是一直自动带着的)
*首先,执行以下命令删除原来的ruby源:

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

执行命令后可在终端看见以下信息:

https://rubygems.org/ removed from sources

*然后下一步添加你找到的可用的镜像源(这里我没有用淘宝的源:https://ruby.taobao.org/ ):

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

*验证新源是否替换成功

gem sources -l

终端输出:

 *** CURRENT SOURCES ***
 https://gems.ruby-china.org/

到此ruby 源替已经换成国内的源

(2)、开始安装 CocoaPods

其实就是执行sudo gem install cocoapods命令这么简单,但这一步是最容易出现坑的。

=========可能出现的状况(坑)=========

问题一:While executing gem ... (Errno::EPERM)  Operation not permitted - /usr/bin/fuzzy_match    错误

解决方案 :

执行sudo gem install -n /usr/local/bin cocoapods  语句。然后提示gems installed即可。

问题二:Error installing pods:active support requires Ruby version >= 2.2.2

解决方案 :

查看ruby版本

$ruby -v

终端会输出你的ruby 版本信息

查看目前的所有ruby版本:

$rvm list known

如果提示command not found 请先安装rvm

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

如果已安装会列出所有的ruby版本:

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[.8]
[ruby-]2.2[.4]
[ruby-]2.3[.0]
[ruby-]2.2-head
ruby-head
......

安装2.2.2:

$rvm install 2.2.2

终端运行结果:(如果直接成功请绕过homebrew的卸载安装)

Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
:

回车:

It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
ruby -e "$(curl -fsSL  
"The current contents of /usr/local are .git
Requirements installation failed with status: 1.

出现Requirements installation failed with status: 1.时才执行:

$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

卸载home-brew

Warning: This script will remove:/Library/Caches/Homebrew//usr/local/.git/
Are you sure you want to uninstall Homebrew? [y/N] y
==> Removing Homebrew installation...
==> Removing empty directories...
==> Homebrew uninstalled!
You may want to restore /usr/local's original permissions
  sudo chmod 0755 /usr/local
  sudo chgrp wheel /usr/local

再执行:

$ rvm install 2.2.2

提示:

Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user):

按回车:

==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
Password:

这里需要输入电脑密码:

==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown haha /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 501, done.
remote: Compressing objects: 100% (445/445), done.
remote: Total 501 (delta 29), reused 360 (delta 27), pack-reused 0
Receiving objects: 100% (501/501), 787.83 KiB | 169.00 KiB/s, done.
Resolving deltas: 100% (29/29), done.
From https://github.com/Homebrew/brew
 * [new branch]      master     -> origin/master
HEAD is now at 32f7e73 download_strategy: ensure fixed commit hash length
==> Tapping homebrew/core
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 3714, done.
remote: Compressing objects: 100% (3598/3598), done.
remote: Total 3714 (delta 14), reused 2112 (delta 6), pack-reused 0
Receiving objects: 100% (3714/3714), 2.88 MiB | 240.00 KiB/s, done.
Resolving deltas: 100% (14/14), done.
Checking connectivity... done.
Checking out files: 100% (3717/3717), done.
Tapped 3591 formulae (3,740 files, 9.0M)
==> Installation successful!
==> Next steps
Run `brew help` to get started
Further documentation: https://git.io/brew-docs
==> Homebrew has enabled anonymous aggregate user behaviour analytics
Read the analytics documentation (and how to opt-out) here:
https://git.io/brew-analytics
Installing requirements for osx.
Updating system.....
Installing required packages: autoconf, automake, libtool, pkg-config, libyaml, readline, libksba, openssl........
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
ruby-2.2.2 - #configure
ruby-2.2.2 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
100 6854k  100 6854k    0     0  61342      0  0:01:54  0:01:54 --:--:--  132k
ruby-2.2.2 - #validate archive
ruby-2.2.2 - #extract
ruby-2.2.2 - #validate binary
ruby-2.2.2 - #setup
ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2 - #importing gemset /Users/haha/.rvm/gemsets/global.gems..............................
ruby-2.2.2 - #generating global wrappers........
ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2
ruby-2.2.2 - #importing gemsetfile /Users/haha/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.2 - #generating default wrappers........
Updating certificates in '/etc/openssl/cert.pem'.
mkdir: /etc/openssl: Permission denied
mkdir -p "/etc/openssl" failed, retrying with sudo
haha password required for 'mkdir -p /etc/openssl': 
and sudo mkdir worked

至此ruby2.2.2就安装好了,也可以直接安装更高的版本,只需将版本号修改一下

然后我们继续sudo gem install cocoapods

问题三、Setting up CocoaPods master repo 卡着不动

出现Setting up CocoaPods master repo,说明Cocoapods正在将它的信息下载到 ~/.cocoapods里;(这一步是很费时间的,等输出Setup completed 安装完成啦)
在此过程中可以右击终端选 --->新建窗口;在新建的终端窗口输入:

cd ~/.cocoapods

进入cocoa pods文件,然后在终端输入:

du -sh *

即可查看下载的文件大小。也就可以知道是网速不好,还是源不可用了。

解决方案 :

1、第一次我用淘宝的https://ruby.taobao.org/ 替换自带的软件源,du -sh *查看下载的文件大小,一直不变(不知道是网速的原因,还是淘宝这个源不可用)。然后我就换到这个源https://gems.ruby-china.org/ 。重新搞了一遍。它的下载速度还是蛮快的,文件大约是800多兆。

2、也可以使用cocoapods的镜像索引.
所有项目的Podspec文件都托管在https://github.com/CocoaPods/Specs ,第一次执行pod setup时,CocoaPods会将这些podspec索引文件更新到本地的~/.cocoapods目录下,这个索引文件比较大,所以第一次更新时非常慢.友好人士在国内的服务器建立了Cocoapods索引库的镜像,所以执行索引跟新操作时候会快很多.具体操作方法如下:
   $ pod repo remove master
   $ git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master
   $ pod repo update
这是使用gitcafe上的镜像,将以上代码中的 https://gitcafe.com/akuandev/Specs.git 替换成https://git.oschina.net/akuandev/Specs.git 即可使用oschina上的镜像。

问题四、出现如下

[!] An error occurred while performing `git pull` on repo `master`.
[!] /usr/bin/git pull --ff-only

原因: Cocoapods的分支不支持当前最新的Xcode版本

解决办法: 删除master分支 重新建立新的分支

sudo rm -fr ~/.cocoapods/repos/master

然后再: pod setup

第五个问题是在别人的博客中看到的

问题五:解决升级EI Capiton CocoaPods "pod: command not found"

升级OS X EI Capiton之后,发现CocoaPods的pod无效了,运行pod后显示:"pod: command not found"的错误。

解决步骤:

1.为了安全起见,执行命令"sudo gem uninstall cocoapods",卸载原有的CocoaPod
2.执行命令"sudo gem install -n /usr/local/bin cocoapods"来重新安装cocoapod
3.如果没有权限执行pod,执行命令"sudo chmod +rx /usr/local/bin/",赋予/usr/local/bin给予执行与读取权限

原文链接:http://www.jianshu.com/p/6ff1903c3f11

三、CocoaPods的使用

(1)查找第三方库

 pod search AFNetworking

(2)、创建Podfile文件
在终端使用cd +路径切换到项目所在文件下,然后输入:

touch Podfile

就可以在项目目录里看到Podfile文件。也可以使用

pod init

来创建,
打开Podfile文件:

open Podfile

然后 pod install就可以了

(3)、 删除已经配置的类库和移除CocoaPods
可以去查看http://www.jianshu.com/p/552f21a989ba?utm_source=tuicool&utm_medium=referral 这篇文章。

pod install与pod update区别:

1.使用pod install来安装新的库,即使你的工程里面已经有了Podfile,并且已经执行过pod install命令了;所以即使你是添加或移除库,都应该使用pod install。
2.使用pod update [PODNAME] 只有在你需要更新库到更新的版本时候用。

小技巧:

最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动原因在于当执行以上两个命令的时候会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。加参数的命令如下:

pod install --verbose --no-repo-updatepod update --verbose --no-repo-update

参考资料:http://blog.csdn.net/u012960049/article/details/52275272
http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/

【转】CocoaPods的安装以及遇到的坑的更多相关文章

  1. 【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)

    本文主要说明CocoaPods的安装步骤.使用说明和常见的报错即解决方法. 1. CocoaPods 1>  CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具. ...

  2. iOS 最新版 CocoaPods 的安装使用

    当在开发iOS应用时,会经常使用到很多的第三方开源类库,一般的方法是直接从GitHub下载,然后拖到项目中使用,如果该开源类库不依赖其他的类库,就可以直接使用:如果该开源类库还依赖一些其他的类库,则需 ...

  3. CocoaPods的安装、使用、以及遇到的问题

    CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...

  4. iOS 最新版 CocoaPods 的安装流程

    iOS 最新版 CocoaPods 的安装流程1.移除现有Ruby默认源$gem sources --remove https://rubygems.org/2.使用新的源$gem sources - ...

  5. 最新版 CocoaPods 的安装流程

                                       iOS 最新版 CocoaPods 的安装流程     1.移除现有Ruby默认源 $gem sources --remove h ...

  6. Cocoapods的安装

    1.先升级Gem sudo gem update --system 2.切换cocoapods的数据源 [先删除,再添加,查看] gem sources --remove https://rubyge ...

  7. Cocoapods的安装,卸载和使用

    [一]Cocoapods的安装 (1)Cocoapods的官方网站为: https://cocoapods.org/ .如果你的电脑已经安装了Ruby开发环境,那么在终端(Terminal)中使用以下 ...

  8. OS10.11系统下 安装cocoapods 以及 安装cocoapods-xcode-plugin-master插件来加载三方框架

    http://www.cnblogs.com/cheng923181/p/4883476.html OS10.11系统下 安装cocoapods 以及 安装cocoapods-xcode-plugin ...

  9. CocoaPods的安装(图文并茂)OS X 10.11 系统

    这里是前言,可以跳过前言部分直接跳到后面"这里正式安装"开始看步骤: 系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 先给一个挺不错的关于 ...

随机推荐

  1. Linux下拆分大文件

    linux split 命令 功能说明:切割文件. 语 法:split [--help][--version][-<行数>][-b <字节>][-C <字节>][- ...

  2. oracle lsnrctl status|start|stop

    [oracle@redhat4 ~]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 06-OCT-2015 ...

  3. C++ STL之set的基本操作

    set是集合,虽然也存在键值和实值,不过两者根本就是同一个值,键值的设置完全就是为了满足红黑树的底层结构,set操作与map很像不过也有些不同. 1. set迭代器与map的不同: (1)set使用接 ...

  4. Linux 下查看文件字符编码和转换编码

    Linux 下查看文件字符编码和转换编码 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linu ...

  5. (六)Ireport制作一个规范的报表,处理数据格式

    转载:http://frankco.iteye.com/blog/1686651 删除注释信息,Report Respector面板中按住Ctrl鼠标选中位于报表每个部分的组件,使用键盘的方向键可以左 ...

  6. hdu 携程全球数据中心建设 (球面距离 + 最小生成树)

    题目 #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib&g ...

  7. 宏buf_pool_struct

    typedef struct buf_pool_struct buf_pool_t; /** @brief The buffer pool structure. NOTE! The definitio ...

  8. 3D开发--CopperCube

    CopperCube的常用接口,以及如何用javascript语言控制场景中的人物动作,或者获取任务的位置等信息

  9. jquery datepicker-强大的日期控件

    在web开发中,总会遇到需要用户输入日期的情况.一般都是提供一个text类型的input供用户输入日期.然而,这种方式,开发人员必须对用户输入的日期进行验证,判断其合法性.除此之外,让用户输入日期也是 ...

  10. webapp调试工具weinre的使用

    在设计师与前端开发人员的努力下,一个WebApp出炉了,可是测试人员说了一堆的问题:某某机型下页面表现不一致,某某系统下页面如何如何,某某 系统浏览器下页面怎么怎么滴.看着满满的测试汇总文档,我们曾经 ...