网上查了很多关于CocoaPods安装与使用的资料,有很多但是多少都不全,于是就将大神们的东西综合了一下,希望能帮到新手,其实我也是新手,哈哈。
一.CocoaPods的安装
1.打开终端
2.终端输入ruby -v,查看自己的ruby版本
walldeAir:~ wall$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]</span>
注:由于下载安装CocoaPods需要Ruby环境,所以需要查看ruby版本,系统默认ruby版本是ruby 2.0.0。本人亲测,现在安装CocoaPods,Ruby version >= 2.2.2,不信可以直接安装CocoaPods,会出现以下提示:
Error installing cocoapods:
activesupport requires Ruby version >= 2.2.2
3.升级ruby版本
第一步:安装rvm(rvm是ruby版本管理器,可以让你拥有多个版本的Ruby)
$ curl -L get.rvm.io | bash -s stable
$ source ~/.profile</span>
第二步:安装ruby
列出ruby可安装的版本信息
$ rvm list known
我安装的是ruby2.3的版本
$ rvm install 2.3
这个安装过程很长,中间要下载和安装很多东西,不用管他,根据他的提示来就好,我估计花了一个多小时,安装完成后会出现下面的提示
ps:中间安装东西要输入密码,就是你电脑的密码,就是开机密码
#extracting ruby-2.3.0 to /Users/wall/.rvm/src/ruby-2.3.0....
ruby-2.3.0 - #configuring......................................................|
ruby-2.3.0 - #post-configuration.
ruby-2.3.0 - #compiling........................................................|
ruby-2.3.0 - #installing.........
ruby-2.3.0 - #making binaries executable..
Installed rubygems 2.5.1 is newer than 2.4.8 provided with installed ruby, skipping installation, use --force to force installation.
ruby-2.3.0 - #gemset created /Users/wall/.rvm/gems/ruby-2.3.0@global
ruby-2.3.0 - #importing gemset /Users/wall/.rvm/gemsets/global.gems............|
ruby-2.3.0 - #generating global wrappers........
ruby-2.3.0 - #gemset created /Users/wall/.rvm/gems/ruby-2.3.0
ruby-2.3.0 - #importing gemsetfile /Users/wall/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.0 - #generating default wrappers........
ruby-2.3.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.3.0 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri</span>
第三步:更换源
查看已有的源
$ gem source -l
显示如下
CURRENT SOURCES
http://rubygems.org/
$ gem sources -r http://rubygems.org/
$ gem sources -a https://ruby.taobao.org
*** CURRENT SOURCES ***
https://ruby.taobao.org/
3.终于可以安装 CocoaPods
终端输入
$ sudo gem install cocoapods
出现下面结果表示安装成功
Done installing documentation for i18n, thread_safe, tzinfo, concurrent-ruby, activesupport, nap, fuzzy_match, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, colored, xcodeproj, escape, fourflusher, cocoapods after 22 seconds
23 gems installed
这里以AFNetWorking为例来介绍CocoaPods的使用。
1.进入项目的根目录
walldeMacBook-Air:~ wall$ cd /Users/wall/Study/iOS\ Sample\ Demo/AFNetworking3.0_CoacoPods
2.创建podfile文件
$ vim podfile
3.编辑 podfile文件
在终端输入i,进入编辑状态,然后输入一下内容
platform :ios, '8.0'
#use_frameworks!个别需要用到它,比如reactiveCocoa
target 'AFNetworking3.0_CoacoPods' do
pod 'AFNetworking', '~> 3.1.0'
end
注:target是指你说创建工程的target
esc退出编辑状态,再输入:,然后输入wq,保存podfile文件
4.执行
$ pod install --verbose --no-repo-update
注:这样可以提高安装的效率,后面的语句是不更新
CocoaPods的spec仓库
Sending stats
- AFNetworking, 3.1.0
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.
这样就安装完成了。
三.CocoaPods安装的常见问题
1.
[!] Unable to satisfy the following requirements:
- `Firebase/Core` required by `Podfile`
None of your spec sources contain a spec satisfying the dependency: `Firebase/Core`.
You have either:
* out-of-date source repos which you can update with `pod repo update`.
* mistyped the name or version.
* not added the source repo that hosts the Podspec to your Podfile.
Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.
出现这种情况是因为你的cocoapods
太久没有更新了,以至于版本落后太多...
$ pod repo update