需求:
- 管理一些常用的類或者第三方的SDK蟆盐;
- 組件化開發(fā);
具體步驟:
1. 打開終端移剪,進(jìn)入要建立私有庫項目工程的路徑誓斥,并執(zhí)行pod lib create [pod name]
創(chuàng)建并下載Cocoapods模板;
過程中會有一些選項斯棒,按需選擇即可盾致,所有選項選擇完成后會自動打開工程。
選項如下:
? XHC-iOS-Libs pod lib create EncryptionMethods
Cloning `https://github.com/CocoaPods/pod-template.git` into `EncryptionMethods`.
Configuring EncryptionMethods 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 ]
> objc
Would you like to include a demo application with your library? [ Yes / No ]
> no
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> none
Would you like to do view based testing? [ Yes / No ]
> no
What is your class prefix?
> LS
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `EncryptionMethods` from `../`
Downloading dependencies
Installing EncryptionMethods (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `EncryptionMethods.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform `ios` with version `9.3` on target `EncryptionMethods_Tests` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'EncryptionMethods/Example/EncryptionMethods.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
? XHC-iOS-Libs
2. 添加源碼文件
將源碼文件復(fù)制到文件夾路徑:[YourPodName]/[YourPodName]/Classes
下荣暮,如圖:
3. 創(chuàng)建私有庫獲取 git 地址绰上,并完善[YourPodName].podspec
配置文件
復(fù)制鏈接,下面編輯 [YourPodName].podspec
更改 s.homepage
和 s.source
使用
4. 編輯 CocoaPods 配置文件 [YourPodName].podspec
(可以直接在 Xcode 中編輯渠驼,也可以用 Sublime Text 或 Visual Studio Code 等編輯都可以)
我的編輯,僅做參考
s.summary = 'XHC EncryptionMethods pod Use.'
s.homepage = 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git'
s.source = { :git => 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git', :tag => s.version.to_s }
- 如果 homepage 有更好的展示頁面鉴腻,可以直接填你的展示頁面迷扇;
- 如果是 MRC 的,不支持 ARC 可添加下面代碼 爽哎;
s.requires_arc = false
- 還有其他一些屬性配置蜓席,有興趣的小伙伴可以自己試試;
5. 進(jìn)入我們的 Example 文件课锌,執(zhí)行 pod update --no-repo-update
指令厨内,安裝本地庫源碼
? Example git:(master) ? pod update --no-repo-update
Update all pods
Analyzing dependencies
Fetching podspec for `EncryptionMethods` from `../`
Downloading dependencies
Installing EncryptionMethods 0.1.0
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
查看 Example 下的項目已經(jīng)導(dǎo)入成功
5. 本地 pod 配置文件驗證合法性
為了保證項目的正確性祈秕,尤其是 pod 配置文件的正確性,在正式提交前雏胃,我們需要執(zhí)行以下本地驗證请毛。在本地驗證正常的情況下,再上傳發(fā)布還是比較穩(wěn)妥的瞭亮。
返回我們的項目根目錄方仿,執(zhí)行 pod 本地驗證指令:
-
pod lib lint
(如果有錯誤,可能會產(chǎn)生很多警告??和錯誤?) - 其他參數(shù):
Usage:
$ pod lib lint
Validates the Pod using the files in the working directory.
Options:
--quick Lint skips checks that would
require to download and build
the spec
--allow-warnings Lint validates even if warnings
are present
--subspec=NAME Lint validates only the given
subspec
--no-subspecs Lint skips validation of
subspecs
--no-clean Lint leaves the build directory
intact for inspection
--fail-fast Lint stops on the first failing
platform or subspec
--use-libraries Lint uses static libraries to
install the spec
--use-modular-headers Lint uses modular headers during
installation
--sources=https://github.com/artsy/Specs,master The sources from which to pull
dependent pods (defaults to
https://github.com/CocoaPods/Specs.git).
Multiple sources must be
comma-delimited.
--platforms=ios,macos Lint against specific
platforms(defaults to all
platforms supported by the
podspec).Multiple platforms must
be comma-delimited
--private Lint skips checks that apply
only to public specs
--swift-version=VERSION The SWIFT_VERSION that should be
used to lint the spec. This
takes precedence over a
.swift-version file.
--skip-import-validation Lint skips validating that the
pod can be imported
--skip-tests Lint skips building and running
tests during validation
--silent Show nothing
--verbose Show more debugging information
--no-ansi Show output without ANSI codes
--help Show help banner of specified
command
終端展示如下:
? EncryptionMethods git:(master) ? pod lib lint
-> EncryptionMethods (0.1.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
EncryptionMethods passed validation.
? EncryptionMethods git:(master) ? pod lib lint --allow-warnings
-> EncryptionMethods (0.1.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
EncryptionMethods passed validation.
? EncryptionMethods git:(master) ?
- 如果出現(xiàn)警告??
- WARN | summary: The summary is not meaningful.
編輯更改一下s.summary
即可消除警告 -
pod lib lint --verbose
可查看詳細(xì)的 log
podspec 本地驗證成功.png
至此统翩,我們的源碼已經(jīng)導(dǎo)入仙蚜、樣例工程已經(jīng)驗證執(zhí)行、podspec 配置文件本地已經(jīng)驗證厂汗,那么我們是不是就可以直接在 pod 里面使用了呢委粉?答案是不行的!目前只是處于本地狀態(tài)娶桦,并沒有發(fā)布贾节,所以還是不能使用的。
6. pod 庫項目發(fā)布到私有倉庫
在項目工程文件下執(zhí)行 git 相關(guān)指令趟紊,并添加 tag 氮双,發(fā)布到 git
分別執(zhí)行以下命令
? EncryptionMethods git:(master) ? git remote add origin http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git
? EncryptionMethods git:(master) ? git add .
? EncryptionMethods git:(master) ? git commit -m "v0.1.0"
? EncryptionMethods git:(master) git push origin master
? EncryptionMethods git:(master) git tag 0.1.0
? EncryptionMethods git:(master) git push origin 0.1.0
相關(guān)指令執(zhí)行結(jié)束后,此時我們再去看我們的 git 項目:
現(xiàn)在我們就可以直接在 Podfile 文件中添加如下代碼就可以用 pod 來添加我們的庫了
pod 'EncryptionMethods', :git => 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git'
但是機(jī)智的同學(xué)發(fā)下此時執(zhí)行 pod search EncryptionMethods
并不能找到相關(guān)的庫霎匈,提示如下:
[!] Unable to find a pod with name, author, summary, or description matching `EncryptionMethods`
為了能讓 pod 搜索到我們的庫戴差,我們還需要把我們的 EncryptionMethods.podspec
文件發(fā)布到我們的 PodSpecs 庫,方便我們管理各種 pod 庫
7. 發(fā)布 .podspec 文件到私有庫
- 首先我們需要創(chuàng)建自己的 Specs 管理庫铛嘱;
- 創(chuàng)建之后獲取到 git 地址:
git@172.17.0.18:XHC-iOS-Libs/Podspecs.git
- 在終端執(zhí)行 Specs 創(chuàng)建指令
pod repo add [REPO_NAME] [SOURCE_URL]
注意:a. 此指令是在本地創(chuàng)建一個 [REPO_NAME] 并且拉取 [SOURCE_URL] 的所有 podSpec暖释,如果 [SOURCE_URL] 為新創(chuàng)建的倉庫,則本地 [REPO_NAME] 文件夾下只包含一個 .git 文件夾
b. 如果需要管理多個 podSpec墨吓,pod repo add [REPO_NAME] [SOURCE_URL] 執(zhí)行一次即可球匕,后續(xù)添加其它 pod 庫時,執(zhí)行 pod repo push [REPO_NAME] [SPEC_NAME.podspec] 就行
? EncryptionMethods git:(master) pod repo add EncryptionMethodsSpec http://172.17.0.18/XHC-iOS-Libs/Podspecs.git
Cloning spec repo `EncryptionMethodsSpec` from `http://172.17.0.18/XHC-iOS-Libs/Podspecs.git`
- 執(zhí)行發(fā)布命令
pod repo push [REPO_NAME] [SPEC_NAME.podspec]
帖烘,直接發(fā)布亮曹,此指令會將[SPEC_NAME.podspec]
部署到本地(~/.cocoapods/repos/[REPO_NAME]
)并且發(fā)布到遠(yuǎn)程PodSpecs
倉庫即上面的 git 地址:git@172.17.0.18:XHC-iOS-Libs/Podspecs.git
, 終端展示如下:
? EncryptionMethods git:(master) ? pod repo push EncryptionMethodsSpec EncryptionMethods.podspec
Validating spec
-> EncryptionMethods (0.1.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
Updating the `EncryptionMethodsSpec' repo
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
Adding the spec to the `EncryptionMethodsSpec' repo
- [Add] EncryptionMethods (0.1.0)
Pushing the `EncryptionMethodsSpec' repo
[!] 'EncryptionMethods' uses the unencrypted 'http' protocol to transfer the Pod. Please be sure you're in a safe network with only trusted hosts. Otherwise, please reach out to the library author to notify them of this security issue.
? EncryptionMethods git:(master) ?
-
發(fā)布成功之后秘症,我們來看一下我們的 Specs 的 git 項目:
Podspecs.png
8. 查看我們本地的 PodSpecs 庫:
你的 repo 結(jié)構(gòu)應(yīng)該是這樣的:
直接 Findle -> 前往 -> 前往文件夾 -> 輸入:~/.cocoapods/repos -> 點(diǎn)擊前往
至此照卦,我們的私有庫創(chuàng)建發(fā)布結(jié)束。但是我們注意到乡摹,EncryptionMethodsSpec 沒有 README.md 役耕,為了后期的維護(hù)更新,以及團(tuán)隊小伙伴的使用方便聪廉,建議完備一下信息瞬痘,新建一個 README.md 文件故慈,添加一些版本記錄信息等。
9. 編輯 Podspecs 的 README.md
終端輸入 vi ~/.cocoapods/repos/EncryptionMethodsSpec/README.md
框全,編輯 README.md
a. 按 i
進(jìn)入編輯狀態(tài)察绷;
b. 開始寫入要編輯的信息;
c. 按 esc
退出編輯狀態(tài)竣况;
d. 按 :wq
保存更改并推出(切記輸入法應(yīng)在英文輸入狀態(tài))
# EncryptionMethodsSpec
項目 | 地址 | 版本 | 日期 | 作者
:-: | :-: | :-: | :-: | :-:
EncryptionMethods | http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git | 0.1.0 | 2019.4.24 | iTruda
README.md 效果展示:
EncryptionMethodsSpec
項目 | 地址 | 版本 | 日期 | 作者 |
---|---|---|---|---|
EncryptionMethods | http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git | 0.1.0 | 2019.4.24 | iTruda |
終端執(zhí)行以下命令克婶,將更改 push 到 Podspecs 私有倉庫
? EncryptionMethods git:(master) ? cd ~/.cocoapods/repos/EncryptionMethodsSpec
? EncryptionMethodsSpec git:(master) ? git add .
? EncryptionMethodsSpec git:(master) ? git commit -m "add README.md"
再去 Podspecs 私有倉庫看看效果
根據(jù)自己的需要,自行編輯吧丹泉,到此我們整個添加完畢情萤。
最后我們需要在 Podfile 文件中添加 source 'http://172.17.0.18/XHC-iOS-Libs/Podspecs.git'
就可以查到我們的 pod 庫了,接下來在終端去執(zhí)行 pod search EncryptionMethods
看看效果
-> EncryptionMethods (0.1.0)
XHC EncryptionMethods pod Use.
pod 'EncryptionMethods', '~> 0.1.0'
- Homepage: http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git
- Source: git@172.17.0.18:XHC-iOS-Libs/EncryptionMethods.git
- Versions: 0.1.0 [EncryptionMethodsSpec repo]
(END)
我們直接在 Podfile 文件中添加 pod 'EncryptionMethods', '~> 0.1.0'
摹恨,然后執(zhí)行 pod install
就可以使用了筋岛。
使用Podfile管理Pods依賴庫版本
pod 'AFNetworking' //不顯式指定依賴庫版本,表示每次都獲取最新版本 pod 'AFNetworking', '2.0' //只使用2.0版本 pod 'AFNetworking', '> 2.0' //使用高于2.0的版本 pod 'AFNetworking', '>= 2.0' //使用大于或等于2.0的版本 pod 'AFNetworking', '< 2.0' //使用小于2.0的版本 pod 'AFNetworking', '<= 2.0' //使用小于或等于2.0的版本 pod 'AFNetworking', '~> 0.1.2' //使用大于等于0.1.2但小于0.2的版本 pod 'AFNetworking', '~>0.1' //使用大于等于0.1但小于1.0的版本 pod 'AFNetworking', '~>0' //高于0的版本晒哄,寫這個限制和什么都不寫是一個效果睁宰,都表示使用最新版本