首先打開終端:(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