一、概要
iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。
二、安装
由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:
注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境
1、Ruby环境搭建
当前安装环境为Mac mini 10.10.5。Mac OS本身自带Ruby,但还是更新一下保险,因为我第一次安装在没有更新Ruby的情况下就失败了。
a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工作失败了,所以更新下ruby)
192:~ WangLimin$ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
192:~ WangLimin$ sudo gem install cocoapods
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
Password:
ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::EPIPE: Broken pipe - SSL_connect (https://rubygems.org/latest_specs.4.8.gz)
b 更新ruby
终端输入如下命令(把Ruby镜像指向taobao,避免被墙,你懂得)
gem sources --remove https://rubygems.org/
gem sources -add https://ruby.taobao.org/
gem sources -l (用来检查使用替换镜像位置成功)
192:~ WangLimin$ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
https://ruby.taobao.org/ added to sources
https://rubygems.org/ removed from sources
192:~ WangLimin$ gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org/
2、下载安装CocoaPods
终端输入:sudo gem install cocoapods
然后输入电脑密码,开始下载安装
192:~ WangLimin$ 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.8.4.gem (100%)
Successfully installed minitest-5.8.4
Fetching: activesupport-4.2.6.gem (100%)
Successfully installed activesupport-4.2.6
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: cocoapods-core-0.39.0.gem (100%)
Successfully installed cocoapods-core-0.39.0
Fetching: claide-0.9.1.gem (100%)
Successfully installed claide-0.9.1
Fetching: colored-1.2.gem (100%)
Successfully installed colored-1.2
Fetching: xcodeproj-0.28.2.gem (100%)
Successfully installed xcodeproj-0.28.2
Fetching: cocoapods-downloader-0.9.3.gem (100%)
Successfully installed cocoapods-downloader-0.9.3
Fetching: cocoapods-plugins-0.4.2.gem (100%)
Successfully installed cocoapods-plugins-0.4.2
Fetching: cocoapods-search-0.1.0.gem (100%)
Successfully installed cocoapods-search-0.1.0
Fetching: cocoapods-stats-0.6.2.gem (100%)
Successfully installed cocoapods-stats-0.6.2
Fetching: cocoapods-try-0.5.1.gem (100%)
Successfully installed cocoapods-try-0.5.1
Fetching: netrc-0.7.8.gem (100%)
Successfully installed netrc-0.7.8
Fetching: cocoapods-trunk-0.6.4.gem (100%)
Successfully installed cocoapods-trunk-0.6.4
Fetching: molinillo-0.4.4.gem (100%)
Successfully installed molinillo-0.4.4
Fetching: escape-0.0.4.gem (100%)
Successfully installed escape-0.0.4
Fetching: cocoapods-0.39.0.gem (100%)
Successfully installed cocoapods-0.39.0
Parsing documentation for i18n-0.7.0
Installing ri documentation for i18n-0.7.0
Parsing documentation for thread_safe-0.3.5
Installing ri documentation for thread_safe-0.3.5
Parsing documentation for tzinfo-1.2.2
Installing ri documentation for tzinfo-1.2.2
Parsing documentation for minitest-5.8.4
Installing ri documentation for minitest-5.8.4
Parsing documentation for activesupport-4.2.6
unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
Installing ri documentation for activesupport-4.2.6
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for cocoapods-core-0.39.0
Installing ri documentation for cocoapods-core-0.39.0
Parsing documentation for claide-0.9.1
Installing ri documentation for claide-0.9.1
Parsing documentation for colored-1.2
Installing ri documentation for colored-1.2
Parsing documentation for xcodeproj-0.28.2
Installing ri documentation for xcodeproj-0.28.2
Parsing documentation for cocoapods-downloader-0.9.3
Installing ri documentation for cocoapods-downloader-0.9.3
Parsing documentation for cocoapods-plugins-0.4.2
Installing ri documentation for cocoapods-plugins-0.4.2
Parsing documentation for cocoapods-search-0.1.0
Installing ri documentation for cocoapods-search-0.1.0
Parsing documentation for cocoapods-stats-0.6.2
Installing ri documentation for cocoapods-stats-0.6.2
Parsing documentation for cocoapods-try-0.5.1
Installing ri documentation for cocoapods-try-0.5.1
Parsing documentation for netrc-0.7.8
Installing ri documentation for netrc-0.7.8
Parsing documentation for cocoapods-trunk-0.6.4
Installing ri documentation for cocoapods-trunk-0.6.4
Parsing documentation for molinillo-0.4.4
Installing ri documentation for molinillo-0.4.4
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-0.39.0
Installing ri documentation for cocoapods-0.39.0
21 gems installed
192:~ WangLimin$
这样就下载安装好了CocoaPods
3、使用CocoaPods
a 新建一个项目,名字PodTest
b 终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)
c 建立Podfile(配置文件)
接着上一步,终端输入 vim Podfile
192:PodTest WangLimin$ vi podfile
在Podfile文件中输入需要下载的第三方库信息,然后保存退出
platform :iOS,'9.0'
pod 'MBProgressHUD', '~> 0.8'
回车后发现PodTest项目总目录中多一个Podfile文件
激动人心的时刻到了:确定终端cd到项目总目录,然后输入 pod install,等待一会,大约3分钟。
192:PodTest WangLimin$ pod install
Creating shallow clone of spec repo `master` from `https://github.com/CocoaPods/Specs.git`
CocoaPods 1.0.0.beta.6 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Updating local specs repositories
CocoaPods 1.0.0.beta.6 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Analyzing dependencies
Downloading dependencies
Installing MBProgressHUD (0.9.2)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `PodTest.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1
total pod installed.
192:PodTest WangLimin$
以上表明安装成功查看项目根目录:
注意:现在打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace
打开项目后看到项目结构并且测试一下:
运行结果:
补充:
1、CocoaPods的基本安装及使用都详细的说明了,但还有一些补充,当需要同时导入多个第三方时候怎么办 ?
这就需要修改Podfile了,就是用vim编辑的那个保存在项目根目录中的文件,修改完了Podfile文件,需要重新执行一次pod install命令。
例如:
platform :ios
pod 'JSONKit', '~> 1.4'
pod 'AFNetworking', '~> 2.0'
终端输入命令:pod search UI
疯了了,我怎么查找这么大众的关键字,好多库~~
然后重新编辑Podfile文件,按照之前的步骤,把更多的库都导入项目!
/////////////////////////////////
Cocoapods卸载安装
Cocoapods存在两个版本 一个0.39.0 一个1.0.1
所以把这两个都删了
sudo gem uninstall cocoapods -v0.39.0
sudo gem uninstall cocoapods -v1.0.1
之后重新安装一个版本
sudo gem install cocoapods -v0.39.0
本博客盗窃自阿福的博客,地址:http://blog.csdn.net/lizhongfu2013/article/details/26384029