您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

用 CocoaPods 開發並打包 iOS 靜態庫

編輯:iOS 開發基礎

CocoaPods 作為 OS X 與 iOS 平台的類庫管理工具,已經相當成熟強大。平常我們用 pod 管理第三方開源庫,但也常會遇到另一種情況:自己開發一個以 pod 管理相依關係的靜態庫給別人用,卻又不想公開原始碼(例如各種 SDK),這時就得打包成 .a 檔。本文以一個相依於 ASIHTTPRequest 的靜態庫為例,走一遍從建立到打包的完整流程。

建立靜態庫

有兩種做法。

不基於 pod 的手動建立(已不建議)

流程偏繁瑣,純體力活,大體步驟是:

1. 在 Xcode 中建立一個 Cocoa Touch Static Library;

2. 建立 Podfile;

3. 執行 pod install 完成專案搭建;

4. 若需要 demo,手動建立範例程式,用 pod 加入對私有靜態庫的相依,再跑一次 pod install。

基於 pod 的自動建立

一條 pod lib 指令就能把初始專案搭好。以下以 BZLib 為例說明。

1. 執行 pod lib create BZLib,過程中會問你四個問題。

Would you like to provide a demo application with your library? [ Yes / No ] yes Which testing frameworks will you use? [ Specta / Kiwi / None ] Kiwi Would you like to do view based testing? [ Yes / No ] No What is your class prefix? BZ

第一題問是否附帶 demo 專案,通常選 Yes,其餘依需求決定。執行完畢後,一個以 CocoaPods 管理相依的基本類庫骨架就產生了。

2. 打開 BZLib.podspec,修改類庫設定資訊,結果大致如下。

Pod::Spec.new do |s| s.name = "BZLib" s.version = "0.1.0" s.summary = "A short description of BZLib." s.description = < "brycezhang.cn@gmail.com" } s.source = { :git => "https://github.com//BZLib.git", :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/' s.platform = :ios, '6.0' s.requires_arc = true s.source_files = 'Pod/Classes/**/*.{h,m}' s.resource_bundles = { 'BZLib' => ['Pod/Assets/*.png'] } s.public_header_files = 'Pod/Classes/**/*.h' s.frameworks = 'MobileCoreServices', 'CFNetwork', 'CoreGraphics' s.libraries = 'z.1' s.dependency 'YSASIHTTPRequest', '~> 2.0.1' end

預設情況下,原始碼放在 Pod/Classes,資源檔放在 Pod/Assets,可以改 s.source_files 與 s.resource_bundles 換位置;s.public_header_files 用來指定公開標頭檔的搜尋路徑。
s.frameworks 與 s.libraries 宣告相依的 SDK framework 與類庫,這裡有個關鍵:相依項不能只寫你自己類庫用到的,還必須包含所有第三方庫的相依,這樣打包成 .a 或 .framework 後別的專案才能正常使用。範例中的 s.frameworks 與 s.libraries 其實都是 ASIHTTPRequest 的相依項。
podspec 的完整語法可參考 Podspec Syntax Reference。

3. 進入 Example 目錄執行 pod install,讓 demo 專案安裝相依並更新設定。

localhost:Example bryce$ pod install --no-repo-update Analyzing dependencies Fetching podspec for `BZLib` from `../` Downloading dependencies Installing BZLib 0.1.0 (was 0.1.0) Using Kiwi (2.3.1) Installing Reachability (3.2) Installing YSASIHTTPRequest (2.0.1) Generating Pods project Integrating client project

4. 開始寫程式。基於示範目的,這裡只簡單封裝一個 GET 請求。

新增 BZHttphelper 類別,注意檔案要放在 Pod/Classes 下,與 podspec 的設定一致。

接著再跑一次 pod install,讓 demo 載入新類別。你可能已經注意到:只要新增類別/資源檔或第三方相依,都得重跑 pod install 才會生效。
程式碼本身很簡單,就是一個 GET 請求的包裝方法。

#import "BZHttphelper.h" @implementation BZHttphelper - (void)getWithUrl:(NSString *)url withCompletion:(void (^)(id responseObject))completion failed:(void (^)(NSError *error))failed { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; __weak ASIHTTPRequest *weakrequest = request; [request setCompletionBlock:^{ NSString *responseString = [weakrequest responseString]; completion(responseString); }]; [request setFailedBlock:^{ NSError *error = [weakrequest error]; failed(error); }]; [request start]; } @end

在 demo 專案中呼叫測試。

#import "BZViewController.h" @interface BZViewController () { BZHttphelper *_httpHelper; } @end @implementation BZViewController - (void)viewDidLoad { [super viewDidLoad]; _httpHelper = [BZHttphelper new]; [_httpHelper getWithUrl:@"https://wcf.open.cnblogs.com/blog/u/brycezhang/posts/1/5" withCompletion:^(id responseObject) { NSLog(@"[Completion]:%@", responseObject); } failed:^(NSError *error) { NSLog(@"[Failed]:%@", error); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

成功印出結果,呼叫成功!

2014-11-23 16:52:23.946 BZLib[6329:96133] [Completion]:cnblogs ...

提交本地程式碼庫

1. 修改 s.source,依實際路徑調整。

s.source = { :git => "/Users/name/workspace/BZLib", :tag => '0.1.0' }

2. 提交原始碼並打 tag。

git add . git commit -a -m 'v0.1.0' git tag -a 0.1.0 -m 'v0.1.0'

驗證類庫

靜態庫寫完後,用 pod lib lint 檢查是否符合 pod 規範,加上 --only-errors 可以濾掉一些警告。

pod lib lint BZLib.podspec --only-errors --verbose ... BZLib passed validation.

打包類庫

打包需要 CocoaPods 的外掛 cocoapods-packager。手動編譯打包當然也行,但過程會繁瑣得多。

安裝打包外掛

在終端機執行:

sudo gem install cocoapods-packager

打包
指令很簡單:

pod package BZLib.podspec --library --force

其中 --library 表示打包成 .a,不帶這個參數則會輸出 .framework;--force 是強制覆蓋。最終目錄結構如下:

|____BZLib.podspec |____ios | |____libBZLib.a

要特別一提的是,這個外掛會把相依的第三方庫重新命名,很好地解決了類庫命名衝突的問題。

本文示例程式碼可下載:BZLib

以上透過 pod 及其外掛完成了建立與打包。若對實作細節有興趣,可以直接看原始碼,或參考文末的延伸閱讀。

延伸閱讀

Developing private static library for iOS with CocoaPods

Automatic build of static library for iOS and many architectures

Avoiding dependency collisions in iOS static library managed by CocoaPods

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved