CocoaPods建立公有Pod庫方法總結

weixin_33727510發表於2017-08-09

1、註冊CocoaPods賬戶資訊

想要建立一個開源pod庫, 首先我們需要註冊CocoaPods, 這裡使用trunk方式, 作為一個iOS開發人員你一定安裝了CocoaPods, 那麼只需要在終端執行:

pod trunk register 郵箱地址 '使用者名稱' --verbose

這裡我們一般使用github郵箱和使用者名稱, 然後在你的郵箱中會收到確認郵件, 在瀏覽器中點選連結確認即註冊成功, 成功之後可以終端執行:

pod trunk me

檢視自己的註冊資訊, 以後當你有了自己的開源Pod庫, 也可以用此方式隨時檢視自己釋出過的Pods;

2、建立共享庫檔案並上傳到公有倉庫

共享庫需要三個必不可少的部分:

  • 共享資料夾(資料夾存放著你要共享的內容, 也就是其他人pod得到的檔案, .podspec檔案中的source_files需要指定此檔案路徑及檔案型別);
  • LICENSE檔案(預設一般選擇MIT);
  • 庫描述檔案.podspec(本庫的各項資訊描述, 需要提交給CocoaPods, pod通過這個檔案查詢到你共享的庫).

這一步分兩種情況:

  • 如果你已經有了現成的想要共享的檔案,你只需要滿足上面三個部分,即可上傳到公有倉庫即可繼續其他的步驟;
  • 你想要建立一個全新的工程去做自己的共享, 參考Using Pod Lib Create
a、執行終端命令:
pod lib create 庫名
b、CocoaPods將立即開啟您的Xcode專案; 從那裡可以編輯CocoaPods生成的所有檔案。
c、開啟*.podspec檔案,修改類庫配置資訊。
#
# Be sure to run `pod lib lint BLPopHandlerController.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'BLPopHandlerController'
  s.version          = '0.1.0'
  s.summary          = 'A short description of BLPopHandlerController.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/upupSue/BLPopHandlerController'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'upupSue' => '594821076@qq.com' }
  s.source           = { :git => 'https://github.com/upupSue/BLPopHandlerController.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'BLPopHandlerController/Classes/**/*'

  # s.resource_bundles = {
  #   'BLPopHandlerController' => ['BLPopHandlerController/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/BLPopHandler/BLPopHandlerController.h'
  # s.frameworks = 'UIKit','Foundation'
end

按照預設配置,類庫的原始檔將位於Pod/Classes資料夾下,資原始檔位於Pod/Assets資料夾下,可以修改s.source_filess.resource_bundles來更換存放目錄。s.public_header_files用來指定標頭檔案的搜尋位置。
s.frameworkss.libraries指定依賴的SDK中的framework和類庫,需要注意,依賴項不僅要包含你自己類庫的依賴,還要包括所有第三方類庫的依賴。
podspec檔案的詳細說明可以看Podspec Syntax Reference

d、進入Example資料夾,執行pod install,讓demo專案安裝依賴項並更新配置。
e、新增程式碼,重新執行Pod install來應用更新,demo中呼叫測試。

Development Pods are different from normal CocoaPods in that they are symlinked files, so making edits to them will change the original files, so you can work on your library from inside Xcode. Your demo & tests will need to include references to headers using the #import <MyLib/XYZ.h> format.

f、把原始碼推送到遠端倉庫
//進入原始碼根目錄
cd ~
//新增到git得暫存區
git add -A
//提交到本地倉庫
git commit -m "first commit"
//新增遠端倉庫地址
git remote add origin https://github.com/upupSue/BLLibPopController.git
//把原生程式碼推送到遠端倉庫
git push -u origin master

3、編輯.podspec檔案

修改s.source

s.source = { :git => "遠端倉庫地址", :tag => '0.1.0' }

檢查Podspec lints是否正確。這可以通過兩種方法完成,pod lib lint和pod spec lint。 它們之間的區別是,pod lib lint不訪問網路,而pod spec lint會檢查外部repo和關聯的標籤。

pod lib lint BLPopHandlerController.podspec
...
BLPopHandlerController.podspec passed validation.

4、打tag, 釋出一個release版本

一切準備就緒後, 我們需要在你的git倉庫裡面存在一個與.podspec檔案中一致的version, 這裡你可以在你的git倉庫中的releases一項去手動釋出, 也可以在當前資料夾下使用終端命令:

git tag -m 'first release' '1.0.1'
git push --tags #推送tag到遠端倉庫

成功之後即可在你的releases裡面看到這個tag的版本.

5、釋出自己的庫描述檔案podspec給cocoapods

同樣在這個資料夾下, 終端執行:

pod trunk push BLPopHandlerController.podspec --allow-warnings

檢視是否提交成功

6、關於查詢和使用新建立的庫

pod search BLPopHandlerController檢驗是否可用

大多情況下會出現這個問題:

[!] Unable to find a pod with name, author, summary, or description matching `BLPopController`

這主要是因為在本地索引裡面沒有, 解決辦法

  1. pod setup (不行,使用方法二)
  2. pod repo update(不行,使用方法三)
  3. 前往這個路徑下~/Library/Caches/CocoaPods刪除search_index.json檔案 , 或者使用終端命令刪除:rm ~/Library/Caches/CocoaPods/search_index.json然後重新搜尋.

7、更新維護podspec

如果有錯誤或者需要迭代版本,修改工程檔案後推送到遠端倉庫後, 需要修改podspec中的版本號, 並重新打tag上傳, 再進行新一輪的驗證和釋出, 當然, 建立一個演示demo工程供其他開發者下載檢視並不會影響我們的pod庫.

擴充套件閱讀

CocoaPods建立公有和私有Pod庫方法總結

iOS:最詳細的建立CocoaPods私有庫教程

相關文章