Cocoapods 通常用來管理第三方庫,當咱們本身封裝了一個功能模塊時也可使用 Cocoapods 發佈給其餘人使用,如下是我按照官方教程實際操做的流程。html
假設咱們已經完成了一個功能模塊的封裝,以 HelloPods 爲例。首先,咱們要在 GitHub 上建立一個名爲 HelloPods 的倉庫,接着咱們要將本地封裝好的代碼提交到 GitHub 上。具體操做以下:git
echo "# HelloPods" >> README.md
git init
git add .
git commit -m 'commit'
git remote add origin https://github.com/hiXgb/HelloPods.git
git push -u origin master
git tag 0.0.1
git push --tag
執行完以上操做後咱們就成功將代碼提交到了 GitHub 上,接下去要作的是發佈到 Cocoapods 上。github
首先,咱們執行 pod spec create
生成 HelloPods.podspec 文件,生成的模板文件有大量註釋,咱們只須要其中一部份內容,整理後的內容以下:swift
Pod::Spec.new do |spec|
spec.name = 'HelloPods'
spec.version = '0.0.1'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/hiXgb/HelloPods'
spec.authors = { 'xgb' => 'xieguobiyi@gmail.com' }
spec.summary = "Learn about creating Podspec's and the Spec repo."
spec.source = { :git => "https://github.com/hiXgb/HelloPods.git", :tag => "0.0.1" }
spec.source_files = '*.{h,m}'
spec.requires_arc = true
end複製代碼
HelloPods.podspec 文件編輯完成後執行 pod spec lint
驗證 podspec 文件是否合法,結果以下:緩存
驗證經過後須要在 Cocoapods 上註冊 trunk,執行bash
pod trunk register xieguobiyi@gmail.com 'xgb'複製代碼
而後查收郵箱點擊連接便可完成註冊,註冊完成後能夠經過 pod trunk me
查詢註冊信息ide
而後再執行 工具
pod trunk push HelloPods.podspec --allow-warnings複製代碼
不出意外的話咱們能看到以下結果測試
至此咱們就完成了庫的發佈,後續就能夠按照其餘第三方庫同樣的用法來使用咱們本身的庫了~ui
若是一個庫有多個子模塊,咱們能夠經過添加 subspec 使結構更加清晰。假設 HelloPods 下有兩個子模塊,一個 Util,一個 Model,咱們首先修改 HelloPods.podspec 文件,修改後的內容以下:
Pod::Spec.new do |spec|
spec.name = 'HelloPods'
spec.version = '0.0.3'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/hiXgb/HelloPods'
spec.authors = { 'xgb' => 'xieguobiyi@gmail.com' }
spec.summary = "Learn about creating Podspec's and the Spec repo."
spec.source = { :git => "https://github.com/hiXgb/HelloPods.git", :tag => "0.0.3" }
spec.source_files = '*.{h,m}'
spec.requires_arc = true
spec.subspec 'Util' do |util|
util.source_files = 'Util/*.{h,m}'
end
spec.subspec 'Model' do |model|
model.source_files = 'Model/*.{h,m}'
end
end複製代碼
修改完成後再重複前面的步驟,將子模塊都先提交到 GitHub 上,而後修改 tag 爲 0.0.3,接着再執行 pod trunk push HelloPods.podspec --allow-warnings
將修改後的內容發佈到 Cocoapods trunk 上,發佈完成後咱們再執行 pod search 'HelloPods'
結果以下:
[!] The validator for Swift projects uses Swift 2.3 by default, if you are using a different version of swift you can use a
.swift-version
file to set the version for your Pod. For example to use Swift 3.0, run:echo "3.0" > .swift-version
.
執行 echo "3.0" > .swift-version
便可解決
warning: Could not find remote branch 0.0.1 to clone.
fatal: Remote branch 0.0.1 not found in upstream origin
執行
git tag 0.0.1
git push --tag複製代碼
ERROR | File Patterns: File patterns must be relative and cannot start with a slash (source_files).
這個錯誤主要是實際文件目錄和配置文件裏的沒有匹配上,須要根據實際項目文件結構具體配置,在上述例子裏的配置是
spec.source_files = '*.{h,m}'複製代碼
能夠依次嘗試如下幾種方法:
rm -rf ~/Library/Caches/CocoaPods複製代碼
pod update
以上就是我跟着教程實際操做的流程和遇到問題的記錄,最重要的仍是要本身動手操做一遍,但願你們在操做過程當中也能有收穫,enjoy~