一、概要
iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。
二、安装
由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:
注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境
1、Ruby环境搭建
当前安装环境为Mac mini 10.8.5。Mac OS本身自带Ruby,但还是更新一下保险,因为我第一次安装在没有更新Ruby的情况下就失败了。
a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工作失败了,所以更新下ruby)
a1@Unique:~$ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
b 更新ruby
终端输入如下命令(把Ruby镜像指向taobao,避免被墙,你懂得)
gem sources –remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l (用来检查使用替换镜像位置成功)
a1@Unique:~$gem sources --remove https://rubygems.org/
source https://rubygems.org/ not present in cache
a1@Unique:~$gem sources -a https://ruby.taobao.org/
https://ruby.taobao.org/ added to sources
a1@Unique:~$gem sources -l
*** CURRENT SOURCES ***
http://ruby.taobao.org/
https://ruby.taobao.org/
注意只有显示
* CURRENT SOURCES *
http://ruby.taobao.org/
https://ruby.taobao.org/
才成功了
2、下载安装CocoaPods
终端输入:sudo gem install cocoapods
a1@Unique:~$sudo gem install cocoapods
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: cocoapods-core-0.39.0.gem (100%)
Successfully installed cocoapods-core-0.39.0
Fetching: xcodeproj-0.28.2.gem (100%)
.
.
.
8 gems installed
这样就下载安装好了CocoaPods
3、使用CocoaPods
a 新建一个项目,名字PodTest
b 终端中,cd到项目总目录
cd /Users/lucky/Desktop/PodTest
c 建立Podfile(配置文件)
接着上一步,终端输入 vim Podfile
键盘输入 i,进入编辑模式,输入
platform :ios, ‘8.0’ // 最低支持版本号
use_frameworks!
target ‘MyApp’ do // 自己app的名字
pod ‘AFNetworking’, ‘~> 2.6’
pod ‘ORStackView’, ‘~> 3.0’
pod ‘SwiftyJSON’, ‘~> 2.3’
end
然后按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq
注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件
激动人心的时刻到了:确定终端cd到项目总目录,然后输入 pod install,等待一会,大约3分钟。
Creating shallow clone of spec repo `master` from `https://github.com/CocoaPods/Specs.git`
CocoaPods 1.0.0.beta.8 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Updating local specs repositories
CocoaPods 1.0.0.beta.8 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Analyzing dependencies
Downloading dependencies
Installing MBProgressHUD (0.9.2)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `podTest.xcworkspace` for this project from now on.
Sending stats
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
注意:现在打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace
CocoaPods的基本安装及使用都在这里了
补充:
使用search命令搜索类库名:
$pod search AFNetworking
如果CocoaPods支持,将会输出搜索到的所有类库版本和信息,以及在Podfile中配置的写法,终端返回信息如下:
-> AFNetworking (2.3.1)
A delightful iOS and OS X networking framework.
pod ‘AFNetworking‘, ‘~> 2.3.1’(这个语句是需要在编辑Podfile的时候输入的命令,即是如何安装下载相关类库的指令)
删除已经安装的第三库
只需要打开vim Podfile编辑器
把那一句pod ‘AFNetworking’,’~>3.1.0’删掉后 按Esc :wq保存 在pod install –verbose –no-repo-update 更新一遍就好了
本博客盗窃自阿福的博客,地址:http://blog.youkuaiyun.com/lizhongfu2013/article/details/26384029