如何制作Pod庫

需求:

  1. 管理一些常用的類或者第三方的SDK;
  2. 組件化開發(fā);

具體步驟:

1. 打開終端,進入要建立私有庫項目工程的路徑,并執(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. 添加源碼文件

將源碼文件復制到文件夾路徑:[YourPodName]/[YourPodName]/Classes 下,如圖:

代碼放置路徑.png
3. 創(chuàng)建私有庫獲取 git 地址,并完善[YourPodName].podspec 配置文件

復制鏈接,下面編輯 [YourPodName].podspec 更改 s.homepages.source 使用

私有庫地址.png
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
  • 還有其他一些屬性配置,有興趣的小伙伴可以自己試試;
編輯podspec.png
5. 進入我們的 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)導入成功


pod 庫導入.png
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 可查看詳細的 log
    podspec 本地驗證成功.png

至此,我們的源碼已經(jīng)導入、樣例工程已經(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 項目:


項目發(fā)布.png

現(xiàn)在我們就可以直接在 Podfile 文件中添加如下代碼就可以用 pod 來添加我們的庫了

pod 'EncryptionMethods', :git => 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git'

但是機智的同學發(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ā)布到遠程 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)應該是這樣的:

repo 結(jié)構(gòu).png

直接 Findle -> 前往 -> 前往文件夾 -> 輸入:~/.cocoapods/repos -> 點擊前往

本地的 Specs 庫.png

至此,我們的私有庫創(chuàng)建發(fā)布結(jié)束。但是我們注意到,EncryptionMethodsSpec 沒有 README.md ,為了后期的維護更新,以及團隊小伙伴的使用方便,建議完備一下信息,新建一個 README.md 文件,添加一些版本記錄信息等。

9. 編輯 Podspecs 的 README.md

終端輸入 vi ~/.cocoapods/repos/EncryptionMethodsSpec/README.md,編輯 README.md
a. 按 i 進入編輯狀態(tài);
b. 開始寫入要編輯的信息;
c. 按 esc 退出編輯狀態(tài);
d. 按 :wq 保存更改并推出(切記輸入法應在英文輸入狀態(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 私有倉庫看看效果

Podspecs 私有倉庫.png

根據(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的版本,寫這個限制和什么都不寫是一個效果,都表示使用最新版本 

接下來讓我們的愉快的玩耍吧?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容