Cocopods的安裝
CocoaPods應該是iOS最常用最有名的類庫管理
當我們開發iOS應用時,會經常使用到很多第三方開源類庫,比如AFNetWorking等等,可能某個類庫又用到其他的庫,手動一個個去下載所需類庫十分麻煩。如果我們項目中用到的類庫有更新,還要重新下載新版本,重新加入到項目中,十分麻煩。所以,我們需要 CocoaPods幫我們管理。
如何下載和安裝CocoaPods?
在安裝CocoaPods之前,首先要在本地安裝好Ruby環境。至于如何在Mac中安裝好Ruby環境,請找度娘和谷哥。
假如你在本地已經安裝好Ruby環境,那么下載和安裝CocoaPods將十分簡單,只需要一行命令。在Terminator中輸入以下命令:
sudo gem install cocoapods
但是,在終端中敲入這個命令之后,可能半天沒有任何反應。
這時我們可以用淘寶的Ruby鏡像來訪問cocoapods。按照下面的順序在終端中敲入依次敲入命令:
$ gem sources --remove https://rubygems.org/
//等有反應之后再敲入以下命令
$ gem sources -a https://ruby.taobao.org/
為了驗證你的Ruby鏡像是并且僅是taobao,可以用以下命令查看:
$ gem sources -l
只有在終端中出現下面文字才表明你上面的命令是成功的:
*** CURRENT SOURCES ***
https://ruby.taobao.org/
這時候,我們再次在終端中運行:
$sudo gem install cocoapods
$pod setup
等一會,CocoaPods就可以在你本地下載并且安裝好了
安裝時候遇到的問題:
Setting up CocoaPodsmasterrepo[!] /usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster--depth=1Cloning into 'master'...error: RPC failed;result=18, HTTP code =200fatal: The remote end hung up unexpectedlyfatal: early EOFfatal: index-pack failed
解決辦法:
$ git config --global http.postBuffer 24288000
$ git config --list
會輸出:http.postbuffer=24288000
再次執行:
$ pod setup
Cocopods使用
我們想用CocoaPods,在項目中導入ASIHTTPRequest類庫
首先,我們要確定ASIHTTPRequest是否支持CocoaPods,可以用CocoaPods的搜索功能驗證一下。在終端中輸入:
$pod search ASIHTTPRequest
輸入之后,你會在終端中看到關于ASIHTTPRequest類庫的一些信息。
比如:
-> ASIHTTPRequest (1.8.2)
現在知道ASIHTTPRequest支持Cocopods了,我們就要道你的項目中創建一個文件,這個文件就叫做"Podfile",一定得是這個文件名,而且沒有后綴。在里面添加pod 'ASIHTTPRequest', '~> 1.8.2',就是告訴CocoaPods,我們要下載ASIHTTPRequest ,1.8.2版本
我們先創建這個PodFile文件。在終端中進入(cd命令)你項目所在目錄,然后在當前目錄下,利用vim創建Podfile,運行:
$vim Podfile
然后在Podfile文件中輸入以下文字:
pod "ASIHTTPRequest","~> 1.8.2"
然后保存退出。vim環境下,保存退出命令是:
:wq!
我們會發現項目中,多了一個名字為Podfile的文件,而且文件內容就是輸入的內容。注意,Podfile文件應該和你的工程文件.xcodeproj在同一個目錄下。
下面我們就可以利用CocoPods下載ASIHTTPRequest類庫了。還是在終端中的當前項目下,運行命令:
$ pod install
運行完命令之后,會看見有這句話[!] From now on use ********.xcworkspace這是說:以后打開項目就用********.xcworkspace 打開,而不是之前的********.xcodeproj文件。
現在,我們就可以開始使用ASIHTTPRequest啦。可以測試一下,在項目中輸入:
import 或者#import "AFNetworking.h"試試
如果以后有更新 或者 添加別的類庫,用update命令即可:
$pod update
CocoaPods 報錯
Xcode 升級到 6.0 后,更新 CocoaPods 或者以前手動添加過第三方SDK改用Cocopods,可能會出現以下的錯誤
錯誤1:
[!] The
Paopao [Debug]
target overrides thePODS_ROOT
build setting defined inPods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation- Use the
$(inherited)flag,or- Remove the build settings from the target. [!] The
Paopao [Debug]target overrides the
OTHER_LDFLAGSbuild setting defined in
Pods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation- Use the$(inherited)
flag,or- Remove the build settings from the target. [!] ThePaopao [Release]
target overrides thePODS_ROOT
build setting defined inPods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation- Use the
$(inherited)flag,or- Remove the build settings from the target. [!] The
Paopao [Release]target overrides the
OTHER_LDFLAGSbuild setting defined in
Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation- Use the$(inherited)
flag,or- Remove the build settings from the target.
產生此警告的原因是項目 Target 中的一些設置,CocoaPods 也做了默認的設置,如果兩個設置結果不一致,就會造成問題。
我想要使用 CocoaPods 中的設置,分別在我的項目中定義PODS_ROOT和Other Linker Flags的地方,把他們的值用$(inherited)替換掉,進入終端,執行
pod update
警告沒了
我個人更喜歡一種簡單粗暴的方法:
點擊項目文件 project.xcodeproj,右鍵顯示包內容,用文本編輯器打開project.pbxproj,刪除OTHER_LDFLAGS的地方,保存,pod update,警告沒了。。。。
錯誤2:
錯誤
這個解決也比較簡單,
在Build Setting 中的Other Linker Flags選項中加入$(OTHER_LDFLAGS)如圖:
解決錯誤
編譯通過.........
OS X EI Caption系統下遇到的問題
升級OS X EI Capiton之后,發現CocoaPods的pod命令無效了,提示一下錯誤
pod: command not found
解決步驟:
1.卸載
sudo gem uninstall cocoapods
2.重新安裝
sudo gem install -n /usr/local/bin cocoapods
3.如果沒有權限執行pod,執行命令一下命令賦予/usr/local/bin給予執行與讀取權限
sudo chmod +rx /usr/local/bin/