当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之小编的意思就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。
CocoaPods的安装:
安装教程:http://code4app.com/article/cocoapods-install-usage
在Mac电脑上安装Ruby运行环境:http://www.cnblogs.com/daguo/p/4097263.html
1. 在Mac电脑上安装Ruby运行环境:
在终端输入命令:curl -L https://get.rvm.io|bash -s stable
执行成功之后会有如下提示(部分):
Installation of RVM in /Users/fengyan/.rvm/ is almost complete:
* To start using RVM you need torun `source /Users/fengyan/.rvm/scripts/rvm`
in all your open shell windows,in rare cases you need to reopen all shell windows.
# fengyan,
#
# Thank you for using RVM!
# We sincerely hope that RVM helpsto make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.
In case of problems: http://rvm.io/helpand https://twitter.com/rvm_io
然后执行操作:source ~/.rvm/scripts/rvm
rvm –v
执行后出现的提示如下所示:
rvm 1.26.11 (latest) by Wayne E. Seguin<wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com>[https://rvm.io/]
用 RVM 安装 Ruby 环境:
rvm install 2.0.0
这个过程的下载和安装过程比较漫长。安装完成后的提示如下所示:
ruby-2.0.0-p643 - #post-configuration.
ruby-2.0.0-p643 - #compiling...................................................-
ruby-2.0.0-p643 - #installing..............
ruby-2.0.0-p643 - #making binaries executable..
ruby-2.0.0-p643 - #downloading rubygems-2.4.8
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 437k 100 437k 0 0 168k 0 0:00:02 0:00:02 --:--:-- 168k
No checksum for downloaded archive, recording checksum in userconfiguration.
ruby-2.0.0-p643 - #extracting rubygems-2.4.8....
ruby-2.0.0-p643 - #removing old rubygems.........
ruby-2.0.0-p643 - #installing rubygems-2.4.8......................
ruby-2.0.0-p643 - #gemset created/Users/fengyan/.rvm/gems/ruby-2.0.0-p643@global
ruby-2.0.0-p643 - #importing gemset/Users/fengyan/.rvm/gemsets/global.gems....-
ruby-2.0.0-p643 - #generating global wrappers........
ruby-2.0.0-p643 - #gemset created /Users/fengyan/.rvm/gems/ruby-2.0.0-p643
ruby-2.0.0-p643 - #importing gemsetfile/Users/fengyan/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.0.0-p643 - #generating default wrappers........
ruby-2.0.0-p643 - #adjusting #shebangs for (gem irb erb ri rdoc testrbrake).
Install of ruby-2.0.0-p643 - #complete
Ruby was built without documentation, tobuild it run: rvm docs generate-ri
安装完成后执行操作:ruby –v
执行成功之后的提示如下:ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-darwin14.4.0]
之后执行gem –v操作。提示如下:2.4.8
以上操作执行完毕之后,可以进行CocoaPods的安装操作。
执行sudo gem install cocoapods操作。如果在执行该过程时出现如下错误:
ERROR: While executing gem ...(Gem::RemoteFetcher::FetchError)
Errno::EPIPE: Broken pipe - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/cocoapods-0.38.2.gemspec.rz)
原因可能是因为Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,网上的一种解决方案是将远替换成淘宝的。替换方式如下所示:
gem source --remove https://rubygems.org/
等待有反应时再执行:
gem source -a http://ruby.taobao.org/
以上操作执行完毕之后,使用命令gem sources -l想验证是否替换成功了。并确认镜像是淘宝的。提示如下:
*** CURRENT SOURCES ***
以上替换操作执行完毕之后,再次执行操作,sudo gem install cocoapods。在执行该操作的过程中需要输入密码。部分提示截取下所示:
Parsing documentation for netrc-0.7.8
Installing ri documentation for netrc-0.7.8
Parsing documentation for cocoapods-trunk-0.6.1
Installing ri documentation for cocoapods-trunk-0.6.1
Parsing documentation for cocoapods-try-0.4.5
Installing ri documentation for cocoapods-try-0.4.5
Parsing documentation for cocoapods-stats-0.5.3
Installing ri documentation for cocoapods-stats-0.5.3
Parsing documentation for cocoapods-plugins-0.4.2
Installing ri documentation for cocoapods-plugins-0.4.2
Parsing documentation for cocoapods-downloader-0.9.1
Installing ri documentation for cocoapods-downloader-0.9.1
Parsing documentation for claide-0.9.1
Installing ri documentation for claide-0.9.1
Parsing documentation for xcodeproj-0.26.3
Installing ri documentation for xcodeproj-0.26.3
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for cocoapods-core-0.38.2
Installing ri documentation for cocoapods-core-0.38.2
Parsing documentation for cocoapods-0.38.2
Installing ri documentation for cocoapods-0.38.2
20 gems installed
使用命令pod searchAFNetworking可以查看关于AFNetworking的所有信息。
至此,CocoaPods的安装已经完成。
CocoaPods的使用(以AFNetworking为例说明):
1. 在已创建的工程的文件夹中新创建一个Podfile的文件(注:文件名必须为Podfile)
2. 打开终端,进入到刚刚创建的文件夹使用vim Podfile命令对Podfile文件进行编辑,内容如下:
platform :ios, '7.0'
pod "AFNetworking","~> 2.0"
3.保存并退出。
4.使用命令podinstall安装即可。
安装完成之后,在终端的输出如下:
Mac-mini:ASI的基本使用 fengyan$ pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.5.4)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `ASI的基本使用.xcworkspace` for this project from now on.
Sending stats
以上操作完成之后,打开ASI的基本使用.xcworkspace文件。可以看到要导入的第三方框架已经导入,在工程文件中只需要使用<>的导入方式即可使用使用CocoaPods导入的第三方框架。
要注意两点:
1、Podfile文件要放在XXX.xcodeproj同一目录下
2、xcodeproj'xxx.xcodeproj'
3、Podfile文件的创建是在终端使用命令touch Podfile创建