在原始没有三方管理的时代,使用大牛写好的三方框架,我们都是采用直接将人家写好的框架直接拖进来使用,我们会发现,如果该三方的版本进行了升级,那岂不是又要去github下载下来,然后再去工程目录里面去替换,不仅增加了工作量,而且还使得项目文件目录非常不雅观,于是cocoapods应运而生,有了cocoadpods这个专门管理三方的工具,我们会发现管理三方变得如此简单。
首先是cocoapods的安装:
Last login: Tue May 14 20:55:59 on ttys000
xiongdeMacBook-Pro:~ xiongchen$ pod setup
Setting up CocoaPods master repo
$ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Enumerating objects: 290, done.
remote: Counting objects: 100% (290/290), done.
remote: Compressing objects: 100% (223/223), done.
remote: Total 3141156 (delta 112), reused 155 (delta 58), pack-reused 3140866
Receiving objects: 100% (3141156/3141156), 648.56 MiB | 3.86 MiB/s, done.
Resolving deltas: 100% (1872151/1872151), done.
Checking out files: 100% (328598/328598), done.
CocoaPods 1.7.0.rc.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.7.0.rc.1
Setup completed
xiongdeMacBook-Pro:~ xiongchen$ pod --version
1.6.1
xiongdeMacBook-Pro:~ xiongchen$ gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
xiongdeMacBook-Pro:~ xiongchen$
看上面的代码:
首先我们检查镜像源:
gem sources -l
如果没有source的时候,我们需要添加一个源:
gem sources -a https://ruby.taobao.org
这里使用的不是淘宝的镜像源,开发者也可以使用淘宝的源;
然后开始安装cocoapods:
使用命令:
sudo gem install cocoapods
然后初始化三方库索引:
pod setup
这里的下载有点慢,请完成至100%,完成之后可以检查cocoadpods的版本号:
pod --version
如果有版本号的话,恭喜你,cocoapods已经安装成功了!
可以在命令行里面尝试搜索下某个三方:
pod search 'SDWebImage'
如果出现一些关于SDWebImage的三方库的话,cocoapods就可以开始正常使用了!
接下来就是cocoapods的基本使用,大致命令如下:
首先cd到工程根目录下:
然后新建一个podfile,这个podfile主要用于编辑三方需要的一些三方库名称和版本信息:
Last login: Wed May 15 08:58:32 on ttys000
xiongdeMacBook-Pro:~ xiongchen$ cd /Users/xiongchen/Desktop/DriveJouyney
xiongdeMacBook-Pro:DriveJouyney xiongchen$ vim podfile
platform :ios,'8.0'
target 'DriveJouyney' do
pod 'AFNetworking'
pod 'MJRefresh','~>1.4.7'
end
退出完成之后进行安装 pod install命令:
Last login: Wed May 15 08:58:32 on ttys000
xiongdeMacBook-Pro:~ xiongchen$ cd /Users/xiongchen/Desktop/DriveJouyney
xiongdeMacBook-Pro:DriveJouyney xiongchen$ vim podfile
xiongdeMacBook-Pro:DriveJouyney xiongchen$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.1)
Installing MJRefresh (1.4.7)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `DriveJouyney.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
xiongdeMacBook-Pro:DriveJouyney xiongchen$
再去工程目录下,你会发现有了一个.workspace后缀的文件,以后都直接打开这个文件就可以,如果下次要移除或者增加三方库,直接 编辑podfile文件,然后cd到该工程目录下,敲pod install命令,等待安装成功即可。