iOS開發~CocoaPods使用詳細說明,ioscocoapods

來源:互聯網
上載者:User

iOS開發~CocoaPods使用詳細說明,ioscocoapods

一、概要

iOS開發時,項目中會引用許多第三方庫,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用來方便的統一管理這些第三方庫。

 

二、安裝

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

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

 

1、Ruby環境搭建

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

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

 

[objc] view plaincopyprint?  
  1. ritekiMac-mini:PodTest lucky$ ruby -v  
  2. ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]  
  3. ritekiMac-mini:PodTest lucky$   


b 更新ruby

 

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

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

 

[objc] view plaincopyprint?  
  1. ritekiMac-mini:~ lucky$ gem sources --remove https://rubygems.org/  
  2. https://rubygems.org/ removed from sources  
  3. ritekiMac-mini:~ lucky$ gem sources -a https://ruby.taobao.org/  
  4. https://ruby.taobao.org/ added to sources  
  5. ritekiMac-mini:~ lucky$ gem sources -l  
  6. *** CURRENT SOURCES ***  

 

 

2、下載安裝CocoaPods

終端輸入:sudo gem install cocoapods 

 

[html] view plaincopyprint?  
  1. ritekiMac-mini:~ lucky$ sudo gem install cocoapods  
  2.   
  3. CHANGELOG:  
  4.   
  5. ## 0.32.1  
  6.   
  7. ##### Bug Fixes  
  8.   
  9. * Fixed the Podfile `default_subspec` attribute in nested subspecs.    
  10.   [Fabio Pelosin][irrationalfab]  
  11.  \ [#2050](https://github.com/CocoaPods/CocoaPods/issues/2050)  
  12.   
  13.   
  14. Successfully installed cocoapods-0.32.1  
  15. Installing ri documentation for cocoapods-0.32.1  
  16. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block  
  17. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block  
  18. Done installing documentation for cocoapods after 10 seconds  
  19. 1 gem installed  

 

 

這樣就下載安裝好了CocoaPods

 

 

3、使用CocoaPods

a  建立一個項目,名字PodTest

 

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

 

[objc] view plaincopyprint?  
  1. cd /Users/lucky/Desktop/PodTest   


c  建立Podfile(設定檔)

 

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

 

鍵盤輸入 i,進入編輯模式,輸入

platform :ios, '7.0' 
pod 'MBProgressHUD', '~> 0.8'

 

然後按Esc,並且輸入“ :”號進入vim命令模式,然後在冒號後邊輸入wq

 

注意:鍵盤輸入 :後,才能輸入wq。斷行符號後發現PodTest項目總目錄中多一個Podfile檔案

 

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

 

 

查看項目根目錄:

 

注意:現在開啟項目不是點擊 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檔案,按照之前的步驟,把更多的庫都匯入項目!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.