如何使用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等術語