目錄步驟:
基礎
- (一 )準備工作;
- (二) 檢查庫名是否被占用;
- (三) 在遠程端創(chuàng)建一個私有(Private)的索引庫(版本庫,Spec repo);
- (四)添加(clone)遠程端私有索引庫到本地;
- (五) 創(chuàng)建本地私有代碼庫工程;
- (六) 添加自己封裝的源代碼、資源文件到工程指定目錄下;
- (七)配置工程中的.podspec文件;
- (八)更新demo工程,并編譯運行demo;
- (九)通過Pod命令驗證本地.podspec文件;
- (十)通過Git命令,提交本地工程到遠程端的私有代碼庫,并且添加tag;
- (十一)通過Pod命令驗證遠程端私有代碼庫的.podspec文件;
- (十二)保存.podsepc文件并提交到遠程端私有索引庫;
- (十三)檢驗1:通過pod search私有庫名命令,搜索自己的私有庫;
- (十四)檢驗2:新建測試項目,pod '私有庫',再次檢查是否可用,及注意事項;
中級
- (十五)現(xiàn)有私有庫的更新管理維護;
- (十六)本地索引庫管理的其他命令;
高級
- (十七)添加依賴
- (十八)發(fā)布到CocoaPods
分割線——————————————————————————分割線——————————————————————————分割線
(一)準備工作:
托管倉庫:GitHub、碼云、GitLab(本項目以GitLab為例)
管理工具:Sourcetree、終端命令:iTerm 2(本項目以iTerm 2(也是一種終端工具,比Mac的終端好用)為例)
編譯工具:Xcode 12.5.1
(二) 檢查庫名是否被占用:
如果我們創(chuàng)建的庫名在Cocoapods中已存在,在驗證的時候沒提示我們庫名被占用,到最后一步提交的時候提交不到GitHub上,說我的庫名已被占用,所以只能重新來一遍了。另一方面,如果你的私有庫和公共庫有重名的,當你在項目的Podfile文件pod你的私有庫時,當pod找不到你的私有庫就會去公共庫搜索有沒有和這個庫名一樣的庫,如果有就會下載這個同名的公共庫了,這樣pod的就不是自己想引入的私有庫了,所以需要在創(chuàng)建私有代碼/索引庫前檢查一下自己創(chuàng)建的私有庫的庫名是否被占用。這里以SYPrivateRepoTest為私有索引庫名為例:
$ pod search SYPrivateRepoTest
[!] Unable to find a pod with name, author, summary, or description matching `SYPrivateRepoTest`
如上,沒有找到相關的庫,說明該庫沒有被占用,我們可以使用這個庫名。還是不放心的童鞋也可以再驗證一下,比如瀏覽器搜索:

注意:
要記住自己創(chuàng)建的私有庫名一定不要跟Github上的第三方庫重名,否則會搜不到。即便能搜索得到,pod進來的庫也不一定是你自己的私有庫,有可能是Github上重名的公共第三方庫,而不是自己的私有庫。因為pod集成時會先從Github上的公共第三方庫查找,然后才是私有庫中查找。
至此,驗證就算完成,我們就可以放心的使用SYPrivateRepoTest作為我們的私有庫了。
(三) 在遠程端創(chuàng)建一個私有的索引庫(版本庫: Spec repo)
我這里是在GitLab上創(chuàng)建的私有索引庫和私有代碼庫,根據自己的情況在相應平臺創(chuàng)建你的私有索引庫和私有代碼庫。
創(chuàng)建一個私有索引庫,庫名為SYPrivateRepoTest,如下圖所示:

(四)添加(clone)遠程端私有索引庫到本地
- 將遠程端私有索引庫(Repo)添加到本地的~/.cocoapods/repos文件夾下,運行命令如下:
// pod repo add SYPrivateRepoTest 【私有索引庫地址,例::https://gitlab.com/degulade/syprivaterepotest.git】
$ pod repo add SYPrivateRepoTest https://gitlab.com/degulade/syprivaterepotest.git
Cloning spec repo `SYPrivateRepoTest` from `https://gitlab.com/degulade/syprivaterepotest.git`
-
查看~/.cocoapods/repos文件夾下clone的私有索引庫,如下圖:
打開文件夾

SYPrivateRepoTest文件夾是自己創(chuàng)建的私有索引庫文件夾,存放的是自己的私有庫下各個版本的.podsepc文件。
- 校驗你本地添加的私有索引庫的安裝是否成功,并準備好,命令如下:
$ cd ~/.cocoapods/repos/SYPrivateRepoTest
$ $ pod repo lint

至此,遠端索引庫添加到本地成功。
(五)創(chuàng)建本地私有代碼庫工程
- 通過終端Pod命令創(chuàng)建本地私有代碼庫模板工程(項目)
1.新建一個文件夾存放工程 ,如:PrivatePods
- 打開終端,cd 到存放工程的目標目錄下
$ cd .../.../PrivatePods
- 然后,創(chuàng)建本地私有代碼庫(模板工程)到目標目錄下,命令如下:
$ pod lib create SYPrivateRepoTest
- 在創(chuàng)建過程中,需要配置幾個信息,如下:
// 輸出信息:
Cloning `https://github.com/CocoaPods/pod-template.git` into `SYPrivateRepoTest`.
Configuring SYPrivateRepoTest template.
------------------------------
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:
- https://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
# 是否需要在私有代碼庫工程中生成一個demo應用程序(項目),需要或不需要根據自己喜好選擇(建議選擇Yes,因為這樣你可以隨時查看你創(chuàng)建的庫是否正確可用)
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
// 輸出信息:
Running pod install on your new library.
Analyzing dependencies
Downloading dependencies
Installing SYPrivateRepoTest (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `SYPrivateRepoTest.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via `pod repo remove master`. To suppress this warning please add `warn_for_unused_master_specs_repo => false` to your Podfile.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'SYPrivateRepoTest/Example/SYPrivateRepoTest.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
- 關于以上配置問題的解釋請參見Cocoapods - Using Pod Lib Create。
至此,本地私有代碼庫模板工程創(chuàng)建完成。
- 若選擇了
創(chuàng)建demo工程,本地私有代碼庫模板工程創(chuàng)建完成后,會自動打開demo工程。pod lib create [工程名]命令,會自動創(chuàng)建.podspec文件、README.md文件和LICENSE開源協(xié)議文件等:
image.png
- 終端cd 到PrivatePods目錄下,運行tree命令查看本地私有代碼庫模板工程目錄結構,如下:
$ cd /.../.../PrivatePods
$ tree SYPrivateRepoTest -L 2
SYPrivateRepoTest
├── Example # demo工程文件夾
│ ├── Podfile # demo的依賴描述文件
│ ├── Podfile.lock
│ ├── Pods # demo依賴的第三方庫文件夾
│ ├── SYPrivateRepoTest
│ ├── SYPrivateRepoTest.xcodeproj
│ ├── SYPrivateRepoTest.xcworkspace
│ └── Tests
├── LICENSE # 開源協(xié)議,默認MIT
├── README.md
├── SYPrivateRepoTest # 存放源代碼和資源文件的文件夾
│ ├── Assets # 存放資源文件的文件夾
│ └── Classes # 存放源文件的文件夾
├── SYPrivateRepoTest.podspec # 庫的配置文件
└── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
10 directories, 5 files
如果
tree報錯,可通過brew 安裝 tree,命令:$ brew install tree文件夾目錄結構(Example文件夾下是demo工程,SYPrivateRepoTest文件夾下是存放私有代碼庫源代碼文件、資源文件等),如下圖:

- Xcode打開的demo工程目錄,如下圖:

(六) 添加自己封裝的源代碼、資源文件到工程指定目錄下:
- 在Xcode打開的demo工程目錄下的pod中,找到ReplaceMe.swift文件,右擊Show in Finder,查看源文件存放的真正目錄位置。Classes文件夾存放源代碼文件,Assets文件夾存放資源文件。

-
將自己封裝的源代碼文件放到Classes文件夾下、把資源文件放到Assets文件夾下即可??梢詫o用的ReplaceMe.swift文件刪除。
添加后的目錄,如下圖:
路徑說明
// 說明①:源代碼文件存放總路徑
// 說明②:文件夾分塊存儲
// 說明③:源文件
注意:如果你不想用系統(tǒng)自動幫你創(chuàng)建的Classes和Assets文件夾,那么你可以直接刪掉,根據自己喜好重新創(chuàng)建/存放相關文件夾。切記不要忘記修改.podspec文件中相應的路徑配置,否則會由于路徑問題,驗證的時候找不到對應的文件而驗證失敗。
(七)配置工程中的.podspec文件:
.podspec文描述了Pod庫的一個版本。一個索引庫,隨著時間的推移,將有許多的版本。它包括關于應該從何處獲取源、使用什么文件、應用構建設置以及其他通用元數據(如其名稱、版本和描述)的詳細信息。
Specs Repo是GitHub上的存儲庫,其中包含所有可用pods的列表。每個庫都有一個單獨的文件夾,其中包含該pod可用版本的子文件夾。
-
在Xcode打開的demo工程中,找到Podspec Metadata文件夾下的工程名.podspec文件:
SYPrivateRepoTest.podspec - 我的.podspec配置:
# 工程名稱
s.name = 'SYPrivateRepoTest'
# 版本號
s.version = '1.0.0'
# 介紹
s.summary = '這是一個私有庫測試.'
# 描述內容
s.description = '這是私有庫測試,這是一個私有庫測試,這是一個私有庫測試...'
# 主頁,這里要填寫可以訪問到的地址,不然驗證不通過
s.homepage = 'https://gitlab.com/degulade'
# 開源協(xié)議
s.license = { :type => 'MIT', :file => 'LICENSE' }
# 作者信息
s.author = { '秦紫陽' => 'degulade@foxmail.com' }
# 遠程端私有代碼庫地址,這里不支持ssh的地址,驗證不通過,只支持HTTP和HTTPS,最好使用HTTPS
s.source = { :git => 'https://gitlab.com/degulade/syprivaterepotest.git', :tag => s.version.to_s }
# 支持的平臺及版本
s.ios.deployment_target = '9.0'
# 文件路徑,**/*表示Classes目錄及其子目錄下所有文件,如果有多個目錄下則用逗號分開,如果需要在項目中分組顯示,這里也要做相應的設置
s.source_files = 'SYPrivateRepoTest/Classes/**/*' ,'SYPrivateRepoTest/Classes/Field1/*' ,'SYPrivateRepoTest/Classes/Field2/*'
# 圖片資源,子目錄同上
# s.resources = "SYPrivateRepoTest/Assets/*"
# 項目中使用的Swift版本,多個用“,”隔開
s.swift_version = '4.0'
.podspec文件中的配置參數說明,請參見Cocoapods-Podspec語法參考 或者 使用Cocoapods創(chuàng)建/管理公共庫
(八)更新demo工程,并編譯運行demo
- 終端運行命令,
cd到Example文件夾目錄下, - 然后運行pod install或pod update命令更新demo工程:
$ pod install
Analyzing dependencies
Downloading dependencies
Installing SYPrivateRepoTest 1.0.0 (was 0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
- 并在Xcode中編譯運行demo,看是否有錯誤,有錯誤及時修改,保證能運行成功。有錯誤存在會驗證不通過。

(九)通過Pod命令驗證本地.podspec文件:
該驗證過程不會訪問網絡,且僅僅是一個驗證不會添加文件到某文件夾下。
- 終端下,
cd到SYPrivateRepoTest文件夾下,運行pod lib lint --allow-warnings驗證命令,如下:
// --allow-warnings參數:允許忽略警告
$ pod lib lint --allow-warnings
-> SYPrivateRepoTest (1.0.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Analyzing workspace
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
SYPrivateRepoTest passed validation. // passed validation表示驗證通過
注意:若驗證過程中有錯誤,驗證失敗,修改后再次驗證,直到通過驗證。
(十)通過Git命令,提交本地工程到遠程端的私有代碼庫,并且添加tag
- 提交本地工程到代碼庫(一定要用庫的https地址)
$ git add . 或者 $ git add -A
$ git status # 顯示代碼狀態(tài)
// 略
$ git commit -m '提交的描述內容'
// 略
$ git remote add origin https://gitlab.com/degulade/syprivaterepotest.git # 遠端庫的https地址
$ git push -u origin master
// 執(zhí)行結果:
Enumerating objects: 75, done.
Counting objects: 100% (75/75), done.
Delta compression using up to 6 threads
Compressing objects: 100% (67/67), done.
Writing objects: 100% (75/75), 28.16 KiB | 4.02 MiB/s, done.
Total 75 (delta 16), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for master, visit:
remote: https://gitlab.com/degulade/syprivaterepotest/-/merge_requests/new?merge_request%5Bsource_branch%5D=master
remote:
To https://gitlab.com/degulade/syprivaterepotest.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
- 添加tag(每次提交之后一定要添加tag)
$ git tag # 查看所有版本號
$ git tag 1.0.0 # 添加當前版本號
$ git tag # 再次查看所有版本號
1.0.0
$ git push --tags # 推送當前版本
// 執(zhí)行結果:
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/degulade/syprivaterepotest.git
* [new tag] 1.0.0 -> 1.0.0
上傳完代碼后,別忘了為這次提交添加tag,否則下一步驗證會有問題,找不到相應版本的.podspec文件。
注意:添加的
git tag 版本號版本號必須和.podspec文件中的s.version = '版本號'一致。
(十一)通過Pod命令驗證遠程端私有代碼庫的.podspec文件
該驗證過程檢查的是遠程端的repo和相關tag標記,需要網絡。
終端運行命令如下:
$ pod spec lint --allow-warnings
-> SYPrivateRepoTest (1.0.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Analyzing workspace
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
Analyzed 1 podspec.
SYPrivateRepoTest.podspec passed validation. # 驗證成功
pod spec lint命令,是用來驗證.podspec文件及其他信息的規(guī)范。
注意:您的podspec應該在沒有任何錯誤或警告的情況下通過。若驗證過程中有錯誤,驗證失敗,修改后再次驗證,直到通過驗證。
(十二)保存.podsepc文件并提交到遠程端私有索引庫
將.podspec文件提交到遠程端私有索引庫,運行命令如下:
$ pod repo push SYPrivateRepoTest SYPrivateRepoTest.podspec
// 執(zhí)行結果:
Validating spec
-> SYPrivateRepoTest (1.0.0) # 1. 驗證規(guī)范
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Analyzing workspace
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
Updating the `SYPrivateRepoTest' repo # 2. 更新repo
Adding the spec to the `SYPrivateRepoTest' repo # 3. 添加到本地repo
- [Add] SYPrivateRepoTest (1.0.0)
Pushing the `SYPrivateRepoTest' repo # 4. 提交到遠程端的私有索引庫
pod repo push [REPO] [NAME.podspec]此命令的執(zhí)行經歷了四個步驟:
- 驗證spec文件及其他信息的規(guī)范
- 更新本地的repo索引庫
- 添加工程中的spec文件到本地的私有索引庫中(會自動創(chuàng)建工程文件夾和版本文件夾)
- 提交spec文件到遠程端的私有索引庫中
至此就完成了私有庫的創(chuàng)建。
(十三)檢驗1:通過pod search私有庫名命令,搜索自己的私有庫
- 私有庫已經創(chuàng)建成功,搜索我們的私有庫,運行:
$ pod search SYPrivateRepoTest
// 執(zhí)行結果:
-> SYPrivateRepoTest (1.0.0)
這是一個私有庫測試.
pod 'SYPrivateRepoTest', '~> 1.0.0'
- Homepage: https://gitlab.com/degulade
- Source: https://gitlab.com/degulade/syprivaterepotest.git
- Versions: 1.0.0 [SYPrivateRepoTest repo]
(END)
(十四)檢驗2:新建測試項目,pod '私有庫',再次檢查是否可用,及注意事項
新建一個工程,在Podfile文件中pod我們的私有庫,pod 'SYPrivateRepoTest'
注意:
1)引入私有庫的時候,除了pod 'SYPrivateRepoTest'外,還需要通過source引入私有庫對應的索引庫地址,如:
source 'https://gitlab.com/degulade/syprivaterepotest.git' # 自己的私有索引庫地址。
2)若你的私有庫還依賴了第三方公共庫或者自己的其他私有庫,第三方公共庫或者自己的其他私有庫對應的索引庫地址也需要引入。
導入示例如下:
target 'TestDemo' do
use_frameworks!
# 自己的私有索引庫地址
source 'https://gitlab.com/degulade/syprivaterepotest.git'
# 公共索引庫地址
source 'https://github.com/CocoaPods/Specs.git'
pod 'SYPrivateRepoTest'
end
cd到測試項目路徑下,運行pod install
$ pod install
Analyzing dependencies
Downloading dependencies
Installing SYPrivateRepoTest (1.0.0)
Generating Pods project
Integrating client project
Pod installation complete! There are 7 dependencies from the Podfile and 7 total pods installed.
[!] Automatically assigning platform `iOS` with version `14.0` on target `SimpleNeteaseMusic` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
注意:source 后邊是索引庫(版本庫)的地址,而不是代碼庫的地址。
若不引入索引庫地址,在pod install時會報找不到你的私有庫的錯誤,或者其他錯誤。
執(zhí)行 pod install 命令時,會拉取遠程 Podflie 中 source 標記的版本庫到本地的 repos 文件夾中,然后在 版本庫 中搜索我們pod ' SYPrivateRepoTest' 的 SYPrivateRepoTest.podspec 文件。根據 SYPrivateRepoTest.podspec 文件中描述的源碼地址下載并整合到項目中。



