当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。
如何安装cocoapods?
Mac下自带ruby,使用ruby的gem指令即可下载安装;
打开Mac下的终端,目前因为软件源的rubygems.org因为使用亚马逊的云服务,所以被屏蔽了,需要更新ruby的源,输入如下的命令:
gem sources --remove https://rubygems.org/
gem sources -a http://ruby.taobao.org/ (可能需要等待一会,一般几分钟左右,看网络情况而定)
gem sources -l (检测是否更改成功)
成功则会显示:
*** CURRENT SOURCES ***
http://ruby.taobao.org/
成功之后就可以输入命令安装下载cocoapods了:
sudo gem install cocoapods
输入命令之后会显示输入password:这时输入你自己电脑的密码就可以了(可能命令行会不动,但是直接输入后,回车就好,或者可以改一下密码,就可以正常显示了)。
输入密码之后就会显示:
MrD:~ dongpi$ sudo gem install cocoapods
Password:
Sorry, try again.
Password:
Sorry, try again.
Password:
Fetching: i18n-0.7.0.gem ( 68%)p
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.7.0.gem (100%)
Successfully installed minitest-5.7.0
Fetching: activesupport-4.2.3.gem (100%)
Successfully installed activesupport-4.2.3
Fetching: nap-0.8.0.gem (100%)
Successfully installed nap-0.8.0
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: cocoapods-core-0.38.2.gem (100%)
Successfully installed cocoapods-core-0.38.2
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.26.3.gem (100%)
Successfully installed xcodeproj-0.26.3
Fetching: cocoapods-downloader-0.9.1.gem (100%)
Successfully installed cocoapods-downloader-0.9.1
Fetching: cocoapods-plugins-0.4.2.gem (100%)
Successfully installed cocoapods-plugins-0.4.2
Fetching: cocoapods-stats-0.5.3.gem (100%)
Successfully installed cocoapods-stats-0.5.3
Fetching: cocoapods-try-0.4.5.gem (100%)
Successfully installed cocoapods-try-0.4.5
Fetching: netrc-0.7.8.gem (100%)
(PS:由于过多,只放上一部分供参考)下载完成之后会显示:
MrD:~ dongpi$ pod setup
Setting up CocoaPods master repo
Setup completed
MrD:~ dongpi$ pod
Usage:
$ podCOMMAND
CocoaPods, the Cocoa library package manager.
Commands:
+ cache Manipulate the CocoaPods cache
+ init Generate a Podfile for the current directory.
+ install Install project dependencies to Podfile.lock versions
+ ipc Inter-process communication
+ lib Develop pods
+ list List pods
+ outdated Show outdated project dependencies
+ plugins Show available CocoaPods plugins
+ repo Manage spec-repositories
+ search Searches for pods
+ setup Setup the CocoaPods environment
+ spec Manage pod specs
+ trunk Interact with the CocoaPods API (e.g. publishing new specs)
+ try Try a Pod!
+ update Update outdated project dependencies and create new
Podfile.lock
Options:
--silent Show nothing
--version Show the version of the tool
--verbose Show more debugging information
--no-ansi Show output without ANSI codes
--help Show help banner of specified command
(PS:这时,就可以根据项目的目录进行cocoapods的使用了)
使用命令找到要使用cocoapods的目录,(一般为cd /users/用户名/desktop/文件名/)
找到之后输入 vim Podfile 创建名为Podfile的文件;
在Podfile文件中输入:
platform:ios,’6.1’
pod ‘SDWebImage’,’~>3.6’
输入完成,退出insert模式,按Esc,然后输入:wq保存退出
输入 pod install 就可以进行三方库的下载了;之后就可以在相应的项目目录中显示pods了;
之后如果要修改,添加三方库,可输入:
vim Podfile (打开文件)
pod '三方库的名字' (注意大小写)
pod update (第一次创建文件用install 之后更新一般都使用update)
(PS:查找三方库:pod search 三方库的名字,回车,会显示搜索出来的相关的三方库)
注:如果你修改了三方库内的文件,在pod update之后会覆盖你修改的内容,需要你自己重新进行修改