安装cocoapods之最详细最正确的安装方法

CocoaPods是什么

当我们开发iOS项目时候,会经常使用到第三方类库,并且会使用很多。
大家的做法基本上都是到GitHub上下载一个一个的类库,然后导入到工程中,并且引入各种的类库,做各种的配置。有时候还要处理ARC与MRC的问题,很麻烦。
还有一种情况,就是当第三方类库出现升级的情况时候,我们还需要删掉旧的,下载新的,再重新导入。
以上问题超级的烦人。。。

CocoaPods就是类库管理工具,刚刚提到所有麻烦的操作,CocoaPods都能解决。
作为一名程序员,掌握CocoaPods已经和你会使用UIButton一样重要了!

 

 

安装CocoaPods

 

安装CocoaPods需要本地Ruby环境,默认情况下,Mac自带Ruby环境,可以通过在终端输入命令ruby -v查看Ruby版本,接下来进行安装,使用如下命令:

 

 
$ sudo gem install CocoaPods

 

 

 

输入以上命令,可能会一直没有反应,或者出现ERROR。注意了,我们在天朝,是有一堵墙的,挡住了,所以你安装失败。不过没关系,我们可以使用另外一个地址来访问CocoaPods。

既然官网地址被墙了,我们就使用淘宝的,淘宝的Ruby镜像可以帮助我们访问CocoaPods,分别在终端输入如下命令,将Ruby镜像替换为淘宝的镜像地址:

 

 $ gem sources —r https://rubygems.org/

 

 

 

#我安装的时候,在网上搜索到很多都是将  -r  写成 -remove,r确实是remove的缩写,我不知道其他人写成remove能否将原来的镜像删掉,但是我的是没有删掉, 所以如果哪个遇见没有删掉的情况,不妨修改一下

 

删掉原来的镜像之后,那么,我们就要安装淘宝的镜像了:

 

$ gem sources -a https://ruby.taobao.org/

现在最新的镜像是

$ gem sources --add http://gems.ruby-china.com/

#有的写的 https 是没有s的,即:http, 我这里做的时候不加 s ,没有成功,不知道是自身原因还是因为都是这个原因,我不否定所有,就是给大家在有错误的时候,多一个出路,这个不行,不妨换个方式试试!

 

完成之后,我们可以使用如下命令来查看Ruby镜像是否已经指向了淘宝:

 

$ gem sources -l

 

 

如果看到如下信息,表示已经成功!!!

 *** CURRENT SOURCES ***

 https://ruby.taobao.org/

最新镜像是

*** CURRENT SOURCES ***

http://gems.ruby-china.com/

下面,重新执行安装命令,等待安装结束就可以了:

 

$ sudo gem install cocoapods;

 

 

#在这里,我看到某些文章写成 CocoaPods,大小写有所不同,但是我的用大写的就不行,我用我的事实说话!

ERROR:  Could not find a valid gem 'CocoaPods' (>= 0) in any repository
ERROR:  Possible alternatives: cocoapods
whatHapend$Mac-mini:~ whatHapend$ sudo gem install cocoaPods
ERROR:  Could not find a valid gem 'cocoaPods' (>= 0) in any repository

这是我用大写的时候报的错

 

等待执行完毕,看到如下提示,恭喜你,安装完成:

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.3.gem (100%)
Successfully installed minitest-5.8.3
Fetching: activesupport-4.2.5.gem (100%)
Successfully installed activesupport-4.2.5
Fetching: nap-1.0.0.gem (100%)
Successfully installed nap-1.0.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.0.gem (100%)
Successfully installed molinillo-0.4.0
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.3
Installing ri documentation for minitest-5.8.3
Parsing documentation for activesupport-4.2.5
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.5
Parsing documentation for nap-1.0.0
Installing ri documentation for nap-1.0.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.0
Installing ri documentation for molinillo-0.4.0
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
 

当然了,也有可能出现安装上cocoapods 提示不一样的,有的会出来现那种带颜色的字,因为没有实例,就没法截图给大家看了,但是我第一次安装的时候,确实出现了不同的提示,可能是因为才做的不同,第一次是朋友帮做的!

 

使用CocoaPods

 

安装已经完成了,下面就来教大家如何使用。
做一个项目,项目中需要使用SDWebImage,看看如何使用CocoaPods进行安装
首先使用如下命令,查看CocoaPods是否支持SDWebImage类库

 

 

下面创建空的项目,然后使用CocoaPods往项目中导入SDWebImage类库:

1>. 创建项目 CocoaPods_Test,保存到桌面

2>. 在终端输入 cd空格,拖入项目文件夹,回车,进入项目所在的根目录

3>.通过命令行创建Podfile文件,该文件的作用是配置第三方类库的信息,就是告诉CocoaPods去管理哪些类库

 

$ vi Podfile

 

 

#这里有的写 vim的

 

vi打开文件后,按i进入编辑模式,输入如下文字:

pod ‘ SDWebImage ’

#这样它会找到最新版本的,你也可以指定寻找,这里就不做多说了

 

输入完成后按ESC退出编辑模式,接着输入:wq,并按回车键进行保存并退出。
上面的操作完成之后,我们可以在项目根目录发现一个没有后缀的Podfile文件,这就是刚刚创建的文件,里面保存了需要使用的第三方类库的信息。(注意:Podfile文件必须和.xcodeproj文件在同一目录下)

 

4>. 接下来,我们就可以使用命令来安装第三方类库了,(确保终端路径在项目根目录)运行如下命令:

 

$ pod install

CocoaPods最后下载的时候:pod install 会出现等待现象。 建议使用:pod install --verbose --no-repo-update  命令,不需要等待,立马下载

 

#注意出来的最后一句话:”Please close any current Xcode sessions and use ‘CocoaPods_Test.xcworkspace’for this project from now on”。这就是告诉我们,从现在开始需要使用这个文件来打开项目,而不在是之前的.xcodeproj

 

 

我们会发现多了一个Pods的依赖工程,打开Pods文件夹后,发现SDWebImage已经在这里面了。

6>. 现在,我们就可以在想要使用SDWebImage的文件中,引入需要的文件,然后就可以正常使用了。

7>. 到现在为止,你已经学会了如何使用CocoaPods来添加第三方类库

,如果你还需要添加第三方的话, 只需要在 vi Podfile 里面,添加 pod ‘ 要添加的第三方 ’就可以啦

 

8>

$ sudo gem uninstall CocoaPods

  不想用的时候,这句代码就可以卸载cocoapods啦!

 

Cocoapods新版本更新后添加第三方有所改变:

 

在 vi pod file 编辑的时候  里面要这样写

platform:ios,'7.0'

target '项目名称' do

pod '添加的文件'

end

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值