使用CocoaPods來做iOS程序的包依賴管理 libpods.a


前言

每種語言發展到一個階段,就會出現相應的依賴管理工具, 或者是中央代碼倉庫。比如

  • Java: maven,Ivy
  • Ruby: gems
  • Python: pip, easy_install
  • Nodejs: npm

隨着iOS開發者的增多,業界也出現了爲iOS程序提供依賴管理的工具,這個工具叫:CocoaPods

CocoaPods簡介

CocoaPods是一個負責管理iOS項目中第三方開源代碼的工具。CocoaPods項目的源碼在Github上管理。該項目開始於2011年8月12日,經過一年多的發展,現在已經超過1000次提交,並且持續保持活躍更新。開發iOS項目不可避免地要使用第三方開源庫,CocoaPods的出現使得我們可以節省設置和更新第三方開源庫的時間。

拿我之前開發的粉筆網iPhone客戶端爲例,其使用了14個第三方開源庫。在沒有使用CocoaPods以前,我需要:

  1. 把這些第三方開源庫的相關文件複製到項目中,或者設置成git的submodule,然後這些開源庫通常需要依賴系統的一些framework,我需要手工地將這些framework一一增加到項目依賴中,比如ASI網絡庫就需要增加以下framework: CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics and zlib。
  2. 對於RegexKitLite這個正則表達式庫,我還需要設置-licucore的編譯參數
  3. 手工管理這些依賴包的更新。

這些體力活雖然簡單,但毫無技術含量並且浪費時間。在使用CocoaPods之後,我只需要將用到的第三方開源庫放到一個名爲Podfile的文件中,然後執行pod install。CocoaPods就會自動將這些第三方開源庫的源碼下載下來,並且爲我的工程設置好相應的系統依賴和編譯參數。

CocoaPods的安裝和使用介紹

安裝

安裝方式異常簡單, Mac下都自帶ruby,使用ruby的gem命令即可下載安裝:

<span class="line-number">1</span>
<span class="line-number">2</span>
$ gem install cocoapods
$ pod setup

使用

使用時需要新建一個名爲Podfile的文件,以如下格式,將依賴的庫名字依次列在文件中即可

<span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
platform :ios
pod 'JSONKit',       '~> 1.4'
pod 'Reachability',  '~> 3.0.0'
pod 'ASIHTTPRequest'
pod 'RegexKitLite'

然後你將編輯好的Podfile文件放到你的項目根目錄中,執行如下命令即可:

<span class="line-number">1</span>
<span class="line-number">2</span>
cd "your project home"
pod install

現在,你的所有第三方庫都已經下載完成並且設置好了編譯參數和依賴,你只需要記住如下2點即可:

  1. 使用CocoaPods生成的 .xcworkspace 文件來打開工程,而不是以前的 .xcodeproj 文件。
  2. 每次更改了Podfile文件,你需要重新執行一次pod install命令。

查找第三方庫

你如果不知道cocoaPods管理的庫中,是否有你想要的庫,那麼你可以通過pod search命令進行查找,以下是我用pod search json查找到的所有可用的庫:

<span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
$ pod search json


-> AnyJSON (0.0.1)
   Encode / Decode JSON by any means possible.
   - Homepage: https://github.com/mattt/AnyJSON
   - Source:   https://github.com/mattt/AnyJSON.git
   - Versions: 0.0.1 [master repo]


-> JSONKit (1.5pre)
   A Very High Performance Objective-C JSON Library.
   - Homepage: https://github.com/johnezang/JSONKit
   - Source:   git://github.com/johnezang/JSONKit.git
   - Versions: 1.5pre, 1.4 [master repo]


-> MTJSONDictionary (0.0.4)
   An NSDictionary category for when you're working with it converting to/from JSON. DEPRECATED, use MTJSONUtils
   instead.
   - Homepage: https://github.com/mysterioustrousers/MTJSONDictionary.git
   - Source:   https://github.com/mysterioustrousers/MTJSONDictionary.git
   - Versions: 0.0.4, 0.0.3, 0.0.2 [master repo]


-> MTJSONUtils (0.1.0)
   An NSObject category for working with JSON.
   - Homepage: https://github.com/mysterioustrousers/MTJSONUtils.git
   - Source:   https://github.com/mysterioustrousers/MTJSONUtils.git
   - Versions: 0.1.0, 0.0.1 [master repo]


-> SBJson (3.1.1)
   This library implements strict JSON parsing and generation in Objective-C.
   - Homepage: http://stig.github.com/json-framework/
   - Source:   https://github.com/stig/json-framework.git
   - Versions: 3.1.1, 3.1, 3.0.4, 2.2.3 [master repo]


-> TouchJSON (1.0)
   TouchJSON is an Objective-C based parser and generator for JSON encoded data.
   - Homepage: https://github.com/TouchCode/TouchJSON
   - Source:   https://github.com/TouchCode/TouchJSON.git
   - Versions: 1.0 [master repo]

生成第三方庫的幫助文檔

如果你想讓CococaPods幫你生成第三方庫的幫助文檔,並集成到XCode中,那麼用brew安裝appledoc即可:

<span class="line-number">1</span>
brew install appledoc

關於appledoc,我在今年初的另一篇博客《使用Objective-C的文檔生成工具:appledoc》中有專門介紹。它最大的優點是可以將幫助文檔集成到XCode中,這樣你在敲代碼的時候,按住opt鍵單擊類名或方法名,就可以顯示出相應的幫助文檔。

原理

大概研究了一下CocoaPods的原理,它是將所有的依賴庫都放到另一個名爲Pods項目中,然後讓主項目依賴Pods項目,這樣,源碼管理工作都從主項目移到了Pods項目中。發現的一些技術細節有:

  1. Pods項目最終會編譯成一個名爲libPods.a的文件,主項目只需要依賴這個.a文件即可。
  2. 對於資源文件,CocoaPods提供了一個名爲Pods-resources.sh的bash腳本,該腳本在每次項目編譯的時候都會執行,將第三方庫的各種資源文件複製到目標目錄中。
  3. CocoaPods通過一個名爲Pods.xcconfig的文件來在編譯時設置所有的依賴和參數。
發佈了17 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章