参考博客:
http://www.jianshu.com/p/6e5c0f78200a
http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/
iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。
一、安装
由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:
注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境
1、Ruby环境搭建
1)查看当前ruby版本
终端输入:ruby -v
bogon:~ Chris.$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
2)更新ruby
终端输入:gem sources --remove https://rubygems.org/
终端输入:gem sources -a https://ruby-china.org/
检查使用替换镜像位置是否成功:
终端输入:gem sources -l
bogon:~ Chris.$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
bogon:~ Chris.$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
bogon:~ Chris.$ gem sources -a https://ruby-china.org/
https://ruby-china.org/ added to sources
bogon:~ Chris.$ gem sources -l
*** CURRENT SOURCES ***
https://ruby-china.org/
2、下载安装CocoaPods
终端输入:sudo gem install cocoapods
ps:如果出现如下提示错误,则需升级Ruby版本。
bogon:~ Chris.$ sudo gem install cocoapods
Password:
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: minitest-5.9.0.gem (100%)
Successfully installed minitest-5.9.0
Fetching: concurrent-ruby-1.0.2.gem (100%)
Successfully installed concurrent-ruby-1.0.2
Fetching: activesupport-5.0.0.1.gem (100%)
ERROR: Error installing cocoapods:
activesupport requires Ruby version >= 2.2.2.
解决方案 :
查看ruby版本:ruby -v
查看目前的所有ruby版本: rvm list knownps:如果已安装就会列出所有的ruby版本(跳过接下来步骤1)。如果提示 command not found ,请先安装rvm:
1)安装 RVM baby 版本管理器
终端输入:curl -L get.rvm.io | bash -s stable
等安装完成 执行
source ~/.bashrc
source ~/.bash_profile
测试是否安装正常
rvm -v
bogon:~ Chris.$ source ~/.bashrc
bogon:~ Chris.$ source ~/.bash_profile
bogon:~ Chris.$ rvm -v
rvm 1.27.0 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
2)用RVM升级Ruby查看当前ruby版本
终端输入:ruby -v
终端输入: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[.8]
[ruby-]2.2[.4]
[ruby-]2.3[.0]
[ruby-]2.2-head
ruby-head
# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2
# JRuby
jruby-1.6[.8]
jruby-1.7[.23]
jruby[-9.0.5.0]
jruby-head
# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx[-2.5.8]
rbx-head
# Opal
opal
# Minimalistic ruby implementation - ISO 30170:2012
mruby[-head]
# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]
# GoRuby
goruby
# Topaz
topaz
# MagLev
maglev[-head]
maglev-1.0.0
# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head
# IronRuby
ironruby[-1.1.3]
ironruby-head
安装ruby2.2.2,执行:rvm install 2.2.2
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/apple/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2 - #importing gemset /Users/apple/.rvm/gemsets/global.gems...............-
ruby-2.2.2 - #generating global wrappers........
ruby-2.2.2 - #gemset created /Users/apple/.rvm/gems/ruby-2.2.2
ruby-2.2.2 - #importing gemsetfile /Users/apple/.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
Chris. password required for 'mkdir -p /etc/openssl':
and sudo mkdir worked
如果最终打印如上信息,说明安装成功。
3)继续CocoaPods的安装
终端输入:sudo gem install cocoapods
二、使用
(optional) 终端输入:pod search AFNetworking 获得某第三方库的信息
进入工程所在的目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)
建立Podfile(配置文件)
ChrisdeMacBook-Pro:~ Chris.$ cd /Users/apple/Desktop/podTest
ChrisdeMacBook-Pro:podTest Chris.$ vim Podfile
platform :ios, '8.0'
target "targetName" do
pod 'MBProgressHUD','0.9.2'
end
PS:这里的targetName写工程名字,platform后的“ios”必须为小写
终端输入:pod install 完成安装
PS:若是执行pod install还是pod update都卡在了Analyzing dependencies不动
原因在于当执行以上两个命令的时候会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。加参数的命令如下:
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
或者
pod install --no-repo-update
pod update --no-repo-update
以后使用的时候切记如下两点:
1.从此以后需要使用Cocoapods生成的 .xcworkspace文件来打开工程,而不是使用以前的.xcodeproj文件
2.每次更改了Podfile文件,都需要重新执行一次pod update命令
ps:当执行pod install之后,除了Podfile,还会生成一个名为Podfile.lock的文件,它会锁定当前各依赖库的版本,之后即使多次执行pod install也不会更改版本,只有执行pod update才会改变Podfile.lock。在多人协作的时候,这样可以防止第三方库升级时候造成大家各自的第三方库版本不一致。所以在提交版本的时候不能把它落下,也不要添加到.gitignore中.