1. 程式人生 > >iOS開發--CocoaPods的安裝和使用說明

iOS開發--CocoaPods的安裝和使用說明

一、概要

二、安裝

由於網上的教程基本都大同小異,但細節之處還不是很完善,所以藉機會在這裡補充下:

注:要使用CocoaPods,那就要下載安裝它,而下載安裝CocoaPods需要Ruby環境

1、Ruby環境搭建

當前安裝環境為Mac mini 10.10.5。Mac  OS本身自帶Ruby,但還是更新一下保險,因為我第一次安裝在沒有更新Ruby的情況下就失敗了。

a 檢視下當前ruby版本:開啟終端輸入 ruby -v(確實安裝了,不過用這個版本接下來工作失敗了,所以更新下ruby)

192:~ WangLimin$ ruby -v

ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

192:~ WangLimin$ sudo gem install cocoapods

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

Password:

ERROR:  Could not find a valid gem 'cocoapods' (>= 0), here is why:

          Unable to download data from https://rubygems.org/ - Errno::EPIPE: Broken pipe - SSL_connect (https://rubygems.org/latest_specs.4.8.gz)


b 更新ruby

終端輸入如下命令(把Ruby映象指向taobao,避免被牆,你懂得

gem sources --remove https://rubygems.org/ 
gem sources -add https://ruby.taobao.org/ 
gem sources -l  (用來檢查使用替換映象位置成功)


192:~ WangLimin$ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/

https://ruby.taobao.org/ added to sources

https://rubygems.org/ removed from sources

192:~ WangLimin$ gem sources -l

*** CURRENT SOURCES ***

https://ruby.taobao.org/

2、下載安裝CocoaPods

終端輸入:sudo gem install cocoapods 

然後輸入電腦密碼,開始下載安裝

192:~ WangLimin$ sudo gem install cocoapods

Password:

Fetching: i18n-0.7.0.gem (100%)

Successfully installed i18n-0.7.0

Fetching: thread_safe-0.3.5.gem (100%)

Successfully installed thread_safe-0.3.5

Fetching: tzinfo-1.2.2.gem (100%)

Successfully installed tzinfo-1.2.2

Fetching: minitest-5.8.4.gem (100%)

Successfully installed minitest-5.8.4

Fetching: activesupport-4.2.6.gem (100%)

Successfully installed activesupport-4.2.6

Fetching: nap-1.1.0.gem (100%)

Successfully installed nap-1.1.0

Fetching: fuzzy_match-2.0.4.gem (100%)

Successfully installed fuzzy_match-2.0.4

Fetching: cocoapods-core-0.39.0.gem (100%)

Successfully installed cocoapods-core-0.39.0

Fetching: claide-0.9.1.gem (100%)

Successfully installed claide-0.9.1

Fetching: colored-1.2.gem (100%)

Successfully installed colored-1.2

Fetching: xcodeproj-0.28.2.gem (100%)

Successfully installed xcodeproj-0.28.2

Fetching: cocoapods-downloader-0.9.3.gem (100%)

Successfully installed cocoapods-downloader-0.9.3

Fetching: cocoapods-plugins-0.4.2.gem (100%)

Successfully installed cocoapods-plugins-0.4.2

Fetching: cocoapods-search-0.1.0.gem (100%)

Successfully installed cocoapods-search-0.1.0

Fetching: cocoapods-stats-0.6.2.gem (100%)

Successfully installed cocoapods-stats-0.6.2

Fetching: cocoapods-try-0.5.1.gem (100%)

Successfully installed cocoapods-try-0.5.1

Fetching: netrc-0.7.8.gem (100%)

Successfully installed netrc-0.7.8

Fetching: cocoapods-trunk-0.6.4.gem (100%)

Successfully installed cocoapods-trunk-0.6.4

Fetching: molinillo-0.4.4.gem (100%)

Successfully installed molinillo-0.4.4

Fetching: escape-0.0.4.gem (100%)

Successfully installed escape-0.0.4

Fetching: cocoapods-0.39.0.gem (100%)

Successfully installed cocoapods-0.39.0

Parsing documentation for i18n-0.7.0

Installing ri documentation for i18n-0.7.0

Parsing documentation for thread_safe-0.3.5

Installing ri documentation for thread_safe-0.3.5

Parsing documentation for tzinfo-1.2.2

Installing ri documentation for tzinfo-1.2.2

Parsing documentation for minitest-5.8.4

Installing ri documentation for minitest-5.8.4

Parsing documentation for activesupport-4.2.6

unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping

Installing ri documentation for activesupport-4.2.6

Parsing documentation for nap-1.1.0

Installing ri documentation for nap-1.1.0

Parsing documentation for fuzzy_match-2.0.4

Installing ri documentation for fuzzy_match-2.0.4

Parsing documentation for cocoapods-core-0.39.0

Installing ri documentation for cocoapods-core-0.39.0

Parsing documentation for claide-0.9.1

Installing ri documentation for claide-0.9.1

Parsing documentation for colored-1.2

Installing ri documentation for colored-1.2

Parsing documentation for xcodeproj-0.28.2

Installing ri documentation for xcodeproj-0.28.2

Parsing documentation for cocoapods-downloader-0.9.3

Installing ri documentation for cocoapods-downloader-0.9.3

Parsing documentation for cocoapods-plugins-0.4.2

Installing ri documentation for cocoapods-plugins-0.4.2

Parsing documentation for cocoapods-search-0.1.0

Installing ri documentation for cocoapods-search-0.1.0

Parsing documentation for cocoapods-stats-0.6.2

Installing ri documentation for cocoapods-stats-0.6.2

Parsing documentation for cocoapods-try-0.5.1

Installing ri documentation for cocoapods-try-0.5.1

Parsing documentation for netrc-0.7.8

Installing ri documentation for netrc-0.7.8

Parsing documentation for cocoapods-trunk-0.6.4

Installing ri documentation for cocoapods-trunk-0.6.4

Parsing documentation for molinillo-0.4.4

Installing ri documentation for molinillo-0.4.4

Parsing documentation for escape-0.0.4

Installing ri documentation for escape-0.0.4

Parsing documentation for cocoapods-0.39.0

Installing ri documentation for cocoapods-0.39.0

21 gems installed

192:~ WangLimin$


這樣就下載安裝好了CocoaPods

3、使用CocoaPods

a  新建一個專案,名字PodTest


b  終端中,cd到專案總目錄(注意:包含PodTest資料夾、PodTest.xcodeproj、PodTestTest的那個總目錄)

c  建立Podfile(配置檔案)

接著上一步,終端輸入 vim Podfile

192:PodTest WangLimin$ vi podfile

在Podfile檔案中輸入需要下載的第三方庫資訊,然後儲存退出

platform :iOS,'9.0'

pod 'MBProgressHUD', '~> 0.8'

回車後發現PodTest專案總目錄中多一個Podfile檔案


激動人心的時刻到了:確定終端cd到專案總目錄,然後輸入 pod install,等待一會,大約3分鐘。

192:PodTest WangLimin$ pod install

Creating shallow clone of spec repo `master` from `https://github.com/CocoaPods/Specs.git`

CocoaPods 1.0.0.beta.6 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.6 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

Pod installation complete! There is 1 dependency from the Podfile and 1

total pod installed.

192:PodTest WangLimin$ 

以上表明安裝成功

檢視專案根目錄:


注意:現在開啟專案不是點選 PodTest.xodeproj了,而是點選 PodTest.xcworkspace

開啟專案後看到專案結構並且測試一下:


執行結果:


補充:

1、CocoaPods的基本安裝及使用都詳細的說明了,但還有一些補充,當需要同時匯入多個第三方時候怎麼辦 ?

這就需要修改Podfile了,就是用vim編輯的那個儲存在專案根目錄中的檔案,修改完了Podfile檔案,需要重新執行一次pod install命令。

例如:

platform :ios

pod 'JSONKit',       '~> 1.4'

pod 'AFNetworking',  '~> 2.0'

2、CocoaPods可以查詢你想要的第三方庫

終端輸入命令:pod search UI

瘋了了,我怎麼查詢這麼大眾的關鍵字,好多庫~~


然後重新編輯Podfile檔案,按照之前的步驟,把更多的庫都匯入專案!

/////////////////////////////////
Cocoapods解除安裝安裝

Cocoapods存在兩個版本 一個0.39.0 一個1.0.1 
所以把這兩個都刪了
sudo gem uninstall cocoapods -v0.39.0
sudo gem uninstall cocoapods -v1.0.1

之後重新安裝一個版本
sudo gem install cocoapods -v0.39.0

本部落格盜竊自阿福的部落格,地址:http://blog.csdn.net/lizhongfu2013/article/details/26384029