如何安装和使用cocoapods来集成第三方库(实战篇)配置初始项目2.cocoapods的使用

2018-06-19 13:05:07 浏览数 (1)

配置初始项目

1.cocoapods的安装

cocoapods的安装是通过ruby,幸运的是Mac电脑都是默认安装ruby的,所以安装ruby的过程就省去了,唯一的前提就是安装Xcode的CommandLineTools。

commandLineTools的安装也很简单,只要在终端输入以下命令:

$ xcode-select --install

如果确实没有安装commandLineTools会提示你要安装它,点安装就可以开始下载,然后等待下载完成后安装即可

但是由于中国的互联网是"墙"的原因,所以呢,你要改变gem的默认下载源:

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

看到以下结果

http://ruby.taobao.org added to sources

将淘宝的ruby源加入进来

删除原来的下载源:

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

看到以下结果就说明已经成功

https://rubygems.org/ removed from sources

然后呢,就可以愉快地安装上cocoapods了!

$ sudo gem install cocoapods

Password:(你的管理员密码,这里不会显示出来) 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: xcodeproj-0.26.3.gem (100%) Successfully installed xcodeproj-0.26.3 Fetching: cocoapods-downloader-0.9.3.gem (100%) Successfully installed cocoapods-downloader-0.9.3 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: cocoapods-trunk-0.6.4.gem (100%) Successfully installed cocoapods-trunk-0.6.4 Fetching: molinillo-0.3.1.gem (100%) Successfully installed molinillo-0.3.1 Fetching: cocoapods-0.38.2.gem (100%) Successfully installed cocoapods-0.38.2 Parsing documentation for cocoapods-core-0.38.2 Installing ri documentation for cocoapods-core-0.38.2 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 cocoapods-downloader-0.9.3 Installing ri documentation for cocoapods-downloader-0.9.3 Parsing documentation for cocoapods-stats-0.5.3 Installing ri documentation for cocoapods-stats-0.5.3 Parsing documentation for cocoapods-try-0.4.5 Installing ri documentation for cocoapods-try-0.4.5 Parsing documentation for cocoapods-trunk-0.6.4 Installing ri documentation for cocoapods-trunk-0.6.4 Parsing documentation for molinillo-0.3.1 Installing ri documentation for molinillo-0.3.1 Parsing documentation for cocoapods-0.38.2 Installing ri documentation for cocoapods-0.38.2 9 gems installed

OK,cocoapods顺利安装完毕!

2.cocoapods的使用

1.  新建一个项目,名字PodTest

2.终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)

$ cd /Users/lucky/Desktop/PodTest  

3. 建立Podfile(配置文件)

接着上一步,终端输入。

$ vim Podfile

键盘输入 i,进入编辑模式,输入

$  platform :ios, '7.0' $ pod 'MBProgressHUD', '~> 0.8'

然后按Esc,并且输入  “  :”  号进入vim命令模式,然后在冒号后边输入wq(注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件)

激动人心的时刻到了:确定终端cd到项目总目录,然后输入 

$ pod install

,等待一会,大约3分钟。

查看项目根目录:

(注意:现在打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace)

0 人点赞