1、注冊CocoaPods賬戶信息
想要創(chuàng)建一個開源pod庫, 首先我們需要注冊CocoaPods, 這里使用trunk方式, 作為一個iOS開發(fā)人員你一定安裝了CocoaPods, 那么只需要在終端執(zhí)行:
pod trunk register 郵箱地址 '用戶名' --verbose
這里我們一般使用github郵箱和用戶名, 然后在你的郵箱中會收到確認郵件, 在瀏覽器中點擊鏈接確認即注冊成功, 成功之后可以終端執(zhí)行:
pod trunk me
查看自己的注冊信息, 以后當你有了自己的開源Pod庫, 也可以用此方式隨時查看自己發(fā)布過的Pods;
2、創(chuàng)建共享庫文件并上傳到公有倉庫
共享庫需要三個必不可少的部分:
-
共享文件夾
(文件夾存放著你要共享的內容, 也就是其他人pod得到的文件, .podspec文件中的source_files需要指定此文件路徑及文件類型); -
LICENSE文件
(默認一般選擇MIT); -
庫描述文件.podspec
(本庫的各項信息描述, 需要提交給CocoaPods, pod通過這個文件查找到你共享的庫).
這一步分兩種情況:
- 如果你已經(jīng)有了現(xiàn)成的想要共享的文件,你只需要滿足上面三個部分,即可上傳到公有倉庫即可繼續(xù)其他的步驟;
- 你想要創(chuàng)建一個全新的工程去做自己的共享, 參考Using Pod Lib Create
a、執(zhí)行終端命令:
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_files
和s.resource_bundles
來更換存放目錄。s.public_header_files
用來指定頭文件的搜索位置。
s.frameworks
和s.libraries
指定依賴的SDK中的framework和類庫,需要注意,依賴項不僅要包含你自己類庫的依賴,還要包括所有第三方類庫的依賴。
podspec文件的詳細說明可以看Podspec Syntax Reference。
d、進入Example文件夾,執(zhí)行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得暫存區(qū)
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。 它們之間的區(qū)別是,pod lib lint不訪問網(wǎng)絡,而pod spec lint會檢查外部repo和關聯(lián)的標簽。
pod lib lint BLPopHandlerController.podspec
...
BLPopHandlerController.podspec passed validation.
4、打tag, 發(fā)布一個release版本
一切準備就緒后, 我們需要在你的git倉庫里面存在一個與.podspec文件中一致的version, 這里你可以在你的git倉庫中的releases一項去手動發(fā)布, 也可以在當前文件夾下使用終端命令:
git tag -m 'first release' '1.0.1'
git push --tags #推送tag到遠端倉庫
成功之后即可在你的releases里面看到這個tag的版本.
5、發(fā)布自己的庫描述文件podspec給cocoapods
同樣在這個文件夾下, 終端執(zhí)行:
pod trunk push BLPopHandlerController.podspec --allow-warnings
6、關于查找和使用新創(chuàng)建的庫
pod search BLPopHandlerController
檢驗是否可用
大多情況下會出現(xiàn)這個問題:
[!] Unable to find a pod with name, author, summary, or description matching `BLPopController`
這主要是因為在本地索引里面沒有, 解決辦法
- pod setup (不行,使用方法二)
- pod repo update(不行,使用方法三)
- 前往這個路徑下~/Library/Caches/CocoaPods刪除search_index.json文件 , 或者使用終端命令刪除:
rm ~/Library/Caches/CocoaPods/search_index.json
然后重新搜索.
7、更新維護podspec
如果有錯誤或者需要迭代版本,修改工程文件后推送到遠端倉庫后, 需要修改podspec中的版本號, 并重新打tag上傳, 再進行新一輪的驗證和發(fā)布, 當然, 創(chuàng)建一個演示demo工程供其他開發(fā)者下載查看并不會影響我們的pod庫.
擴展閱讀