如何制作Pod庫

需求:

  1. 管理一些常用的類或者第三方的SDK蟆盐;
  2. 組件化開發(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 下荣暮,如圖:

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

復(fù)制鏈接,下面編輯 [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. 進(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)入成功


pod 庫導(dǎo)入.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 可查看詳細(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 項目:


項目發(fā)布.png

現(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)該是這樣的:

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

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

本地的 Specs 庫.png

至此照卦,我們的私有庫創(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 私有倉庫看看效果

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

接下來讓我們的愉快的玩耍吧!G蘖琛柒傻!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市较木,隨后出現(xiàn)的幾起案子嘿棘,更是在濱河造成了極大的恐慌膘滨,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異矿辽,居然都是意外死亡弦撩,警方通過查閱死者的電腦和手機(jī)艇潭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門坑填,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人虹蒋,你說我怎么就攤上這事糜芳。” “怎么了魄衅?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵峭竣,是天一觀的道長。 經(jīng)常有香客問我徐绑,道長,這世上最難降的妖魔是什么莫辨? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任傲茄,我火速辦了婚禮毅访,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盘榨。我一直安慰自己喻粹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布草巡。 她就那樣靜靜地躺著守呜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪山憨。 梳的紋絲不亂的頭發(fā)上查乒,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音郁竟,去河邊找鬼玛迄。 笑死,一個胖子當(dāng)著我的面吹牛棚亩,可吹牛的內(nèi)容都是我干的蓖议。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼讥蟆,長吁一口氣:“原來是場噩夢啊……” “哼勒虾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瘸彤,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤修然,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后钧栖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體低零,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年拯杠,在試婚紗的時候發(fā)現(xiàn)自己被綠了掏婶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡潭陪,死狀恐怖雄妥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情依溯,我是刑警寧澤老厌,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站黎炉,受9級特大地震影響枝秤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜慷嗜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一淀弹、第九天 我趴在偏房一處隱蔽的房頂上張望丹壕。 院中可真熱鬧,春花似錦薇溃、人聲如沸菌赖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽琉用。三九已至,卻和暖如春策幼,著一層夾襖步出監(jiān)牢的瞬間邑时,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工垄惧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留刁愿,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓到逊,卻偏偏與公主長得像铣口,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子觉壶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容