如何使用CocoaPods製作私有庫

語言: CN / TW / HK

theme: geek-black

「這是我參與2022首次更文挑戰的第2天,活動詳情檢視:2022首次更文挑戰」。

前言

在前面的章節中有介紹過CocoaPods的使用:

CocoaPods公有庫以及私有庫的區別

製作Framework以及上傳至CocoaPods庫

此篇文章會著重介紹下製作過程以及記錄過程遇到的一些問題和解決辦法。

製作過程

首先,我們要先了解整個製作過程,這樣我們來快速的瞭解下製作的整體流程,以便於我們在後面的製作中有一個清晰的步驟。

  1. 建立私有的源倉庫 (如果已經存在源倉庫,忽略此步)
  2. 建立私有的Pod倉庫
  3. 新增私有庫原始檔 (新增程式碼、資源、包等等)
  4. 修改.podSpec檔案
  5. 驗證.podSpec檔案
  6. 提交程式碼至遠端倉庫
  7. 推送.podSpec檔案

建立私有的源倉庫

1.在遠端倉庫中,建立一個源索引庫PodSpecs。 (遠端倉庫可以選擇githubgitlab碼雲

2.將遠端索引庫新增到本地源中(使用終端輸入以及命令) /** pod repo add [repoName] [source] */ pod repo add component http://github.com/component/specs.git

建立私有的Pod倉庫

1.在遠端倉庫中,建立一個原始碼pod庫。 注意⚠️:這裡先不著急關聯到本地; 2. 選擇一個目錄下,建立本地Pod庫工程。 /** pod lib create Pod庫名 */ pod lib create RTCComponent 執行完成後,命令列會有一系例的問題,按需填寫即可; ``` To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: - http://guides.cocoapods.org/making/using-pod-lib-create.html ( hold cmd and double click links to open in a browser. )

What platform do you want to use?? [ iOS / macOS ]

iOS

What language do you want to use?? [ Swift / ObjC ]

Swift

Would you like to include a demo application with your library? [ Yes / No ]

yes

Which testing frameworks will you use? [ Quick / None ]

none

Would you like to do view based testing? [ Yes / No ]

no `` 命令執行完後會幫我們建立一個Workspace,裡面包含了兩個Project,RTCComponent是我們Pod庫的執行Demo工程;Pods`是我們開發庫的程式碼工程。

專案層級

新增私有庫原始檔

我們可以在Pods/Development Pods/RTCComponent目錄下匯入我們的程式碼檔案或者framework。在這一步就是將我們程式碼以及資原始檔都放置到專案中,匯入的檔案以及資源目錄後面需要在.podSpec檔案中配置, 這裡就不過多說明。

修改.podSpec檔案

``` Pod::Spec.new do |s| s.name = 'RTCComponent' s.version = '0.1.0' s.summary = '元件' s.description = 'xxxxxxxxxxxxxxxxx' s.homepage = 'http://xxxx/component' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' }. s.author = { 'xxxxxxxx' => '[email protected]' } s.source = { :git => 'http://xxxx/component/RTCComponent.git', :tag => s.version.to_s } # s.social_media_url = 'http://twitter.com/' s.ios.deployment_target = '9.0'

# 這裡是原始檔的路徑 s.source_files = 'RTCComponent/Classes/*/'

# 這裡是資原始檔的路徑 # s.resource_bundles = { # 'RTCComponent' => ['RTCComponent/Assets/.png'] # } # 這裡是標頭檔案的路徑 # s.public_header_files = 'Pod/Classes//.h'

# 如果匯入了framework s.frameworks = 'OpenAL', 'Accelerate'

# 如果依賴了library(記得把lib字首,以及.tbd去掉) s.libraries = 'sqlite3', 'resolv', 'c++', 'z'

# 如果依賴了三方pod庫 s.dependency 'HandyJSON', '~> 5.0.2' s.dependency 'TXLiteAVSDK_Professional', '~> 8.9.10382' s.dependency 'SwiftyBeaver', '~> 1.9.3' # Log s.dependency 'SwifterSwift/SwiftStdlib'

# 如果需要修改pod中的target設定,寫在這裡 s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64', 'ENABLE_BITCODE' => 'NO' } # s.user_target_xcconfig = { 'ENABLE_BITCODE' => 'NO' } end ``` 這裡如果需要設定請參照 CocoaPods官網文件

驗證.podSpec檔案

cd到該目錄下;執行以下操作: ``` /* 網路校驗 / pod spec lint

/ 本地校驗 */ pod lib lint `` 常用附加處理: - 檢視詳細資訊,請在命令後加入--verbose- 忽略警告,請在命令後加入--allow-warnings- 使用本地庫,請在命令後加入--use-libraries- 檢查所有問題,請在命令後加入--no-clean- 依賴了私有庫,需要新增源,請在命令後加入--sources=(**注意:如果依賴了公有庫,還需要新增公有庫源:http://github.com/CocoaPods/Specs--sources=私有庫名,http://github.com/CocoaPods/Specs`

提交程式碼至遠端倉庫

將本地pod庫工程提交推送到遠端倉庫,打上Tag釋出。 1.提交本地倉庫,並推送 /** cd Pod工程目錄 */ cd PodProject /** git commit -m "提交資訊" */ git commit -m "commit" /** git branch -M 分支 */ git branch -M main /** git remote add origin git倉庫地址 */ git remote add origin http://github.com/XXXX.git /** git push -u origin 分支 */ git push -u origin main

2.打上Tag釋出 /** 新建tag git tag [tagName] */ git tag "0.0.1" /** 推送單個tag至遠端 git push origin [tagName] */ git push origin "0.0.1" /** 推送本地所有tag git push origin --tags */ git push origin --tags

推送.podSpec檔案

檢查下本地repo,如果有,繼續下一步,反之,則重新在新增一次(pod repo add [name] [源倉庫git地址]); pod repo list 本地倉庫檢查無誤後,開始將建立的Pod庫中的.podspec檔案推送至指定源倉庫; 首先,還是cd.podspec檔案的目錄下,執行以下操作: /** 推送命令: pod repo push [repoName] [name].podspec */ pod repo push component RTCComponent.podspec

到這裡所有流程已經完成;整個過程中的驗證過程可能是最容易出問題的,如果驗證通過,那麼釋出就不會有什麼問題,所以如果驗證沒有通過的話,釋出也是不會成功的;

文章的最後,我會把製作過程中所遇到的問題一一彙總。(後續如果有遇到的其他問題,也會在這裡更新)

問題彙總(Error)

  • 問題1: 校驗失敗 ** BUILD FAILED ** The following build commands failed: CompileSwift normal x86_64 CompileSwiftSources normal i386 com.apple.xcode.tools.swift.compiler CompileSwift normal i386 CompileSwift normal arm64 (4 failures) Testing with `xcodebuild`. 解決辦法:在.podspec檔案中加入;參考iOS 指令集架構 s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64' }
  • 問題2: 元件中依賴的第三方庫中有framework或者.a檔案, pod install 報錯 target has transitive dependencies that include statically linked binaries: 解決辦法: 在podfile檔案中加入以下程式碼; pre_install do |installer| Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {} end
  • 問題3: Xcode setting ENABLE_BITCODE You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) 解決辦法:將target下 ENABLE_BITCODE 設定為 NO

參考文件:

CocoaPods官方製作文件

用 CocoaPods 私有庫提高團隊的整體效率

GitHub 將使用 main替換掉 master等術語