首先打开终端:(Launchpad-其余-终端)在终端中输入以下指令:ios
1.移除现有Ruby默认源
$gem sources --remove https://rubygems.org/
2.使用新的源
$gem sources -a https://ruby.taobao.org/
3.验证新源是否替换成功
$gem sources -lgit
4.更新gem
$sudo gem update --system
5.安装CocoaPods
(1) $sudo gem install cocoapods
备注:若是苹果系统升级OS X EL Capitan版本后改成 $sudo gem install -n /usr/local/bin
(2) $pod setup
提示:若是终端一直停留在 Setting up CocoaPods master repo 这个状态,是由于要进行下载安装,并且目录比较大,须要耐心等待一下。
6. 新建工程,并在终端用cd指令到文件夹内(具体操做:终端输入cd+空格+工程文件夹直接拖拽到终端+回车)
$pod search 第三方名字 例如:pod search 'AFNetworking' (退出搜索按 :wq )
7.新建文件 vim “Podfile”,
vim Podfile
写入如下内容并保存 提示:(终端vim文件 按 i 可编辑 ,esc 退出编辑(若是esc没反应,control+c也能够退出编辑状态),退出编辑模式之后按 :wq 回车 可保存并退出podfile)github
platform:ios,'7.0'
pod 'AFNetworking','~>3.1.0'json
//若是须要添加多个库,只须要依次添加便可vim
platform:ios,'7.0'
pod 'AFNetworking','~>3.1.0'
pod 'MJRefresh','~>3.1.9'api
备注:platform那一行,ios三个字母都要小写,并且与前面的冒号之间不能有间隔,后面的版本号也能够不写,可是有些开源库对版本是有要求的,好比要在6.0以上才能运行,遇到这样的开源库就须要写上版本号
8.安装podfile文件里面所添加的库
pod install
9.退出终端xcode
使用方法:ruby
1.今后之后须要使用Cocoapods生成的 .xcworkspace文件来打开工程,而不是使用之前的.xcodeproj文件
2.每次更改了Podfile文件,都须要从新执行一次pod update命令session
备注:当执行pod install以后,除了Podfile,还会生成一个名为Podfile.lock的文件,它会锁定当前各依赖库的版本,以后即便屡次执行pod install也不会更改版本,只有执行pod update才会改变Podfile.lock.在多人协做的时候,这样能够防止第三方库升级时候形成你们各自的第三方库版本不一致。因此在提交版本的时候不能把它落下,也不要添加到.gitignore中.app
1).CocoaPods报错: The dependency 'xxx' is not used in any concrete target
出现这个错误的缘由是由于 cocoaPods升级后,Podfile文件的内容格式要求发生了变化,必须指出所用第三方库的target。解决方法以下:
1.检查CocoaPods的版本(目前最新版本是1.0.1,如果最新版本,直接进入步骤3)
$pod --version
2.升级或安装最新版本
升级命令:$sudo gem update -n /usr/local/bin --system
安装命令:$sudo gem install -n /usr/local/bin cocoapods
3.修改Podfile的内容(必定要cd在当前须要修改的工程下,再进入podfile文件)
$vim Podfile
进入podfile文件之后,下面紫色部分就是须要添加的部分,意思就是在Podfile文件中指出使用第三方库的target,不明白的就对照上面的第7步看看区别吧!
platform:ios, '7.0'
target:工程名字 do
pod 'AFNetworking', '~> 3.1.0'
end
2).[!] Oh no, an error occurred.
It appears to have originated from your Podfile at line 2.
Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%2FUsers%2Fxiao6%2FMusic%2FGI06%E5%AE%9E%E8%AE%AD%E8%8A%B8%E8%8C%97%E8%8C%B6%E5%8F%B6%2FYMTea%2FPodfile%3A2%3A+syntax+error%2C+unexpected+%27%3A%27%2C+expecting+end-of-input%0Aplatform+%3A+ios%2C+%277.0%27%0A++++++++++%5E&type=Issues
If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new
Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md
Don't forget to anonymize any private data!
出现这个错误的缘由是由于Podfile文件里面的platform那一行,冒号和ios之间多了一个空格
3).E325: ATTENTION
Found a swap file by the name ".Podfile.swp"
owned by: beisheng02 dated: Tue Aug 9 09:41:12 2016
file name: ~beisheng02/Desktop/ZJL_Buyers/Podfile
modified: YES
user name: beisheng02 host name: zhangjielin.local
process ID: 775
While opening file "Podfile"
(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r Podfile"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".Podfile.swp"
to avoid this message.
Swap file ".Podfile.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
我出现这个错误的缘由是由于以前建立过vim podfile文件,而后没有保存就退出终端了,下次再次打开终端,添加vim podfile文件的时候因为Podfile.swp文件已经存在,因此就报错了。多按几下回车键,找到上次没保存的vim文件,:wq保存并退出就能够了。
4).[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
出现这个问题的缘由是由于我直接用的文本编辑去编辑Podfile,须要使用Xcode编辑,或者使用终端敲命令去编辑。
5).[!] Invalid `Podfile` file: syntax error, unexpected tSTRING_BEG, expecting end-of-input.
在写podfile文件的时候,platform:ios, '7.0'少写了一个引号形成的,注意检查podfile文件的格式
6).pod search 搜索类库失败的解决办法
pod setup
pod install
时,系统会默认操做pod setup
,然而因为中国强大的墙可能会pod setup不成功。这时就须要手动执行pod setup
指令,以下:pod setup
Setting up CocoaPods master repo
,稍等几十秒,最底下会输出Setup completed
。说明执行pod setup
成功。pod search AFNetworking
Unable to find a pod with name, author, summary, or descriptionmatching 'AFNetworking'
这时就须要继续下面的步骤了。pod setup
成功后,依然不能pod search
,是由于以前你执行pod search
生成了search_index.json,此时须要删掉。pod search
。pod search
pod search afnetworking
(不区分大小写)Creating search index for spec repo 'master'.. Done!
,稍等片刻······就会出现全部带有afnetworking字段的类库7).[!] CocoaPods was not able to update the `master` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`
终端运行 pod repo update --verbose