一.CocoaPods是什么?
CocoaPods是一個用Ruby寫的、負責管理iOS項目中第三方開源庫的工具,CocoaPods能讓我們集中的、統一管理第三方開源庫,為我們節省設置和更新第三方開源庫的時間。
二.CocoaPods的安裝
因為Mac電腦自帶Ruby環境,我們就只需打開終端開始動手。
(1)、將Ruby 的軟件源替換成國內的
ruby 的軟件源(https://rubygems.org)使用的是亞馬遜的云服務,所以自帶的需要翻墻,但我們可以使用如下代碼將官方的 ruby 源替換成國內淘寶的源(https://ruby.taobao.org/)或者是由 Ruby China 社區專注維護的這個源(https://gems.ruby-china.org/)。(我在鏡像的時候開始用的是淘寶的,但會卡死,可能是不能用了,也可能是網速慢,在后面講“坑”的時候會說)。在這我用的是Ruby China 社區專注維護的這個源(https://gems.ruby-china.org/)。
##(注意:復制別人的命令時不要將$也復制,終端是一直自動帶著的)
*首先,執行以下命令刪除原來的ruby源:
gem?sources?--remove?https://rubygems.org/
執行命令后可在終端看見以下信息:
https://rubygems.org/?removed?from?sources
*然后下一步添加你找到的可用的鏡像源(這里我沒有用淘寶的源:https://ruby.taobao.org/):
gem?sources?-a?https://gems.ruby-china.org/
*驗證新源是否替換成功
gem?sources?-l
終端輸出:
***?CURRENT?SOURCES?***
https://gems.ruby-china.org/
到此ruby 源替已經換成國內的源
(2)、開始安裝 CocoaPods
其實就是執行sudo gem install cocoapods命令這么簡單,但這一步是最容易出現坑的。
=========可能出現的狀況(坑)=========
問題一:While executing gem ... (Errno::EPERM) ?Operation not permitted - /usr/bin/fuzzy_match ? ?錯誤
解決方案 :
執行sudo?gem?install?-n?/usr/local/bin?cocoapods??語句。然后提示gems?installed即可。
問題二:Error installing pods:active support requires Ruby version >= 2.2.2
解決方案 :
查看ruby版本
$ruby?-v
終端會輸出你的ruby 版本信息
查看目前的所有ruby版本:
$rvm?list?known
如果提示command not found 請先安裝rvm
$curl?-L?get.rvm.io?|?bash?-s?stable
如果已安裝會列出所有的ruby版本:
MRI?Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head]?#?security?released?on?head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.8]
[ruby-]2.2[.4]
[ruby-]2.3[.0]
[ruby-]2.2-head
ruby-head
......
安裝2.2.2:
$rvm?install?2.2.2
終端運行結果:(如果直接成功請繞過homebrew的卸載安裝)
Searching?for?binary?rubies,?this?might?take?some?time.
Found?remote?file?https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking?requirements?for?osx.
About?to?install?Homebrew,?press?`Enter`?for?default?installation?in?`/usr/local`,
type?new?path?if?you?wish?custom?Homebrew?installation?(the?path?needs?to?be?writable?for?user)
:
回車:
It?appears?Homebrew?is?already?installed.?If?your?intent?is?to?reinstall?you
should?do?the?following?before?running?this?installer?again:
ruby?-e?"$(curl?-fsSL
"The?current?contents?of?/usr/local?are?.git
Requirements?installation?failed?with?status:?1.
出現Requirements installation failed with status: 1.時才執行:
$ruby?-e?"$(curl?-fsSL?https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
卸載home-brew
Warning:?This?script?will?remove:/Library/Caches/Homebrew//usr/local/.git/
Are?you?sure?you?want?to?uninstall?Homebrew??[y/N]?y
==>?Removing?Homebrew?installation...
==>?Removing?empty?directories...
==>?Homebrew?uninstalled!
You?may?want?to?restore?/usr/local's?original?permissions
sudo?chmod?0755?/usr/local
sudo?chgrp?wheel?/usr/local
再執行:
$?rvm?install?2.2.2
提示:
Searching?for?binary?rubies,?this?might?take?some?time.
Found?remote?file?https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking?requirements?for?osx.
About?to?install?Homebrew,?press?`Enter`?for?default?installation?in?`/usr/local`,
type?new?path?if?you?wish?custom?Homebrew?installation?(the?path?needs?to?be?writable?for?user):
按回車:
==>?This?script?will?install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
Press?RETURN?to?continue?or?any?other?key?to?abort
==>?/usr/bin/sudo?/bin/mkdir?/Library/Caches/Homebrew
Password:
這里需要輸入電腦密碼:
==>?/usr/bin/sudo?/bin/chmod?g+rwx?/Library/Caches/Homebrew
==>?/usr/bin/sudo?/usr/sbin/chown?haha?/Library/Caches/Homebrew
==>?Downloading?and?installing?Homebrew...
remote:?Counting?objects:?501,?done.
remote:?Compressing?objects:?100%?(445/445),?done.
remote:?Total?501?(delta?29),?reused?360?(delta?27),?pack-reused?0
Receiving?objects:?100%?(501/501),?787.83?KiB?|?169.00?KiB/s,?done.
Resolving?deltas:?100%?(29/29),?done.
From?https://github.com/Homebrew/brew
*?[new?branch]??????master?????->?origin/master
HEAD?is?now?at?32f7e73?download_strategy:?ensure?fixed?commit?hash?length
==>?Tapping?homebrew/core
Cloning?into?'/usr/local/Library/Taps/homebrew/homebrew-core'...
remote:?Counting?objects:?3714,?done.
remote:?Compressing?objects:?100%?(3598/3598),?done.
remote:?Total?3714?(delta?14),?reused?2112?(delta?6),?pack-reused?0
Receiving?objects:?100%?(3714/3714),?2.88?MiB?|?240.00?KiB/s,?done.
Resolving?deltas:?100%?(14/14),?done.
Checking?connectivity...?done.
Checking?out?files:?100%?(3717/3717),?done.
Tapped?3591?formulae?(3,740?files,?9.0M)
==>?Installation?successful!
==>?Next?steps
Run?`brew?help`?to?get?started
Further?documentation:?https://git.io/brew-docs
==>?Homebrew?has?enabled?anonymous?aggregate?user?behaviour?analytics
Read?the?analytics?documentation?(and?how?to?opt-out)?here:
https://git.io/brew-analytics
Installing?requirements?for?osx.
Updating?system.....
Installing?required?packages:?autoconf,?automake,?libtool,?pkg-config,?libyaml,?readline,?libksba,?openssl........
Certificates?in?'/usr/local/etc/openssl/cert.pem'?are?already?up?to?date.
Requirements?installation?successful.
ruby-2.2.2?-?#configure
ruby-2.2.2?-?#download
%?Total????%?Received?%?Xferd??Average?Speed???Time????Time?????Time??Current
Dload??Upload???Total???Spent????Left??Speed
100?6854k??100?6854k????0?????0??61342??????0??0:01:54??0:01:54?--:--:--??132k
ruby-2.2.2?-?#validate?archive
ruby-2.2.2?-?#extract
ruby-2.2.2?-?#validate?binary
ruby-2.2.2?-?#setup
ruby-2.2.2?-?#gemset?created?/Users/haha/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2?-?#importing?gemset?/Users/haha/.rvm/gemsets/global.gems..............................
ruby-2.2.2?-?#generating?global?wrappers........
ruby-2.2.2?-?#gemset?created?/Users/haha/.rvm/gems/ruby-2.2.2
ruby-2.2.2?-?#importing?gemsetfile?/Users/haha/.rvm/gemsets/default.gems?evaluated?to?empty?gem?list
ruby-2.2.2?-?#generating?default?wrappers........
Updating?certificates?in?'/etc/openssl/cert.pem'.
mkdir:?/etc/openssl:?Permission?denied
mkdir?-p?"/etc/openssl"?failed,?retrying?with?sudo
haha?password?required?for?'mkdir?-p?/etc/openssl':
and?sudo?mkdir?worked
至此ruby2.2.2就安裝好了,也可以直接安裝更高的版本,只需將版本號修改一下
然后我們繼續sudo gem install cocoapods
問題三、Setting up CocoaPods master repo 卡著不動
出現Setting up CocoaPods master repo,說明Cocoapods正在將它的信息下載到 ~/.cocoapods里;(這一步是很費時間的,等輸出Setup completed 安裝完成啦)
在此過程中可以右擊終端選 --->新建窗口;在新建的終端窗口輸入:
cd?~/.cocoapods
進入cocoa pods文件,然后在終端輸入:
du?-sh?*
即可查看下載的文件大小。也就可以知道是網速不好,還是源不可用了。
解決方案 :
1、第一次我用淘寶的https://ruby.taobao.org/替換自帶的軟件源,du -sh *查看下載的文件大小,一直不變(不知道是網速的原因,還是淘寶這個源不可用)。然后我就換到這個源https://gems.ruby-china.org/。重新搞了一遍。它的下載速度還是蠻快的,文件大約是800多兆。
2、也可以使用cocoapods的鏡像索引.
所有項目的Podspec文件都托管在https://github.com/CocoaPods/Specs,第一次執行pod setup時,CocoaPods會將這些podspec索引文件更新到本地的~/.cocoapods目錄下,這個索引文件比較大,所以第一次更新時非常慢.友好人士在國內的服務器建立了Cocoapods索引庫的鏡像,所以執行索引跟新操作時候會快很多.具體操作方法如下:
$ pod repo remove master
$ git clonehttps://github.com/CocoaPods/Specs.git~/.cocoapods/repos/master
$ pod repo update
這是使用gitcafe上的鏡像,將以上代碼中的https://gitcafe.com/akuandev/Specs.git替換成https://git.oschina.net/akuandev/Specs.git即可使用oschina上的鏡像。
問題四、出現如下
[!]?An?error?occurred?while?performing?`git?pull`?on?repo?`master`.
[!]?/usr/bin/git?pull?--ff-only
原因: Cocoapods的分支不支持當前最新的Xcode版本
解決辦法: 刪除master分支 重新建立新的分支
sudo?rm?-fr?~/.cocoapods/repos/master
然后再: pod setup
第五個問題是在別人的博客中看到的
問題五:解決升級EI Capiton CocoaPods "pod: command not found"
升級OS X EI Capiton之后,發現CocoaPods的pod無效了,運行pod后顯示:"pod: command not found"的錯誤。
解決步驟:
1.為了安全起見,執行命令"sudo?gem?uninstall?cocoapods",卸載原有的CocoaPod
2.執行命令"sudo?gem?install?-n?/usr/local/bin?cocoapods"來重新安裝cocoapod
3.如果沒有權限執行pod,執行命令"sudo?chmod?+rx?/usr/local/bin/",賦予/usr/local/bin給予執行與讀取權限
原文鏈接:http://www.lxweimin.com/p/6ff1903c3f11
三、CocoaPods的使用
(1)查找第三方庫
pod?search?AFNetworking
(2)、創建Podfile文件
在終端使用cd +路徑切換到項目所在文件下,然后輸入:
touch?Podfile
就可以在項目目錄里看到Podfile文件。也可以使用
pod?init
來創建,
打開Podfile文件:
open?Podfile
然后 pod install就可以了
(3)、 刪除已經配置的類庫和移除CocoaPods
可以去查看http://www.lxweimin.com/p/552f21a989ba?utm_source=tuicool&utm_medium=referral這篇文章。
pod install與pod update區別:
1.使用pod install來安裝新的庫,即使你的工程里面已經有了Podfile,并且已經執行過pod install命令了;所以即使你是添加或移除庫,都應該使用pod install。
2.使用pod update [PODNAME] 只有在你需要更新庫到更新的版本時候用。
小技巧:
最近使用CocoaPods來添加第三方類庫,無論是執行pod install還是pod update都卡在了Analyzing dependencies不動原因在于當執行以上兩個命令的時候會升級CocoaPods的spec倉庫,加一個參數可以省略這一步,然后速度就會提升不少。加參數的命令如下:
pod?install?--verbose?--no-repo-updatepod?update?--verbose?--no-repo-update
參考資料:http://blog.csdn.net/u012960049/article/details/52275272
http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/
文章轉自此生逍遙的簡書
判斷的CocoaPods是否可以使用,可以用CocoaPods的搜索功能驗證一下。在終端中輸入:
pod search AFNetworking;
執行后可能會一直停在
Setting up CocoaPods master repo然后(很久)會出現如下錯誤:
解決辦法是:
這說明CocoaPods還不能正常使用,需要更新pod,下載它的一些依賴包;在終端中輸入:
$ pod setup
過一段時間之后,你會在終端中看到跟上面同樣的紅色的錯誤信息。
這說明某些環境原因導致pod更新不了,可能原因有,1)gem版本太低;2)github無法鏈接;3).cocoapods目錄下的配置信息錯誤。我們可以一個一個來排除,
首先更新gem到最新版本,在終端中輸入:
$ sudo gem update --system
然后檢查是否可以ping通github,在終端中輸入:
$ ping github.com
然后查看pob repo list:
$ pod repo list
說明Cocoapods在將它的信息下載到 ~/.cocoapods里;
cd??到該目錄里,用du -sh *命令來查看文件大小,結果顯示0 repos,說明沒有安裝成功;
在終端輸入:
cd ~/.cocoapods
進入cocoapods文件后在終端輸入:du -sh *
重新執行pod setup,過一段時間后提示setup completed,在終端中輸入 pod list,展示出安裝列表;
這樣總算安裝好了。接下來再一次輸入:
pod search AFNetworking
輸入過后它可能會報:
[!] Unable to find a pod with name, author, summary, or descriptionmatching `AFNetworking`
解決方案是:
輸入:
rm ~/Library/Caches/CocoaPods/search_index.json
后在一次輸入:pod search AFNetworking
就可以咯。
三,cocoapods的使用
首先來到你的項目文件:
cd 項目文件的位置
然后,添加一個Podfile文件:vim Podfile
然后按i進入插入模式,進行編輯,在文件中輸入以下內容:
注意百度上很多舊版本輸入的類容:
platform :ios, '8.1'
pod 'AFNetworking', '~> 2.0'
它會報:[!] The dependency `AFNetworking (~> 3.0)` is not used in any concrete target.
現在版本升級官方給的文檔是:
platform :ios, '8.0'
target '你的項目名稱' do
pod 'AFNetworking', '~> 3.0'
end
注意:'你的項目名稱'是你項目文件中'xx.xcodeproj'文件,'xx.xcodeproj'去掉后綴名所得的‘xx’==(就是)‘你的項目名稱’
輸入完成后按ESC退出編輯模式,最后輸入:wq保存并退出文件。此時可以發現在項目目錄下多了一個Podfile的文件,請注意這個文件必須與.xcodeproj在同一目錄下。
最后:
pod install
沒報錯就可以了。