在創(chuàng)建自己的cocoapod庫之前,先要確保兩件事:1.自己的電腦已經(jīng)安裝了cocoapod。2.確保你要創(chuàng)建的庫名別人沒有使用過,可以用pod search xxx(庫名)來搜索下。
詳情可參考官方文檔https://guides.cocoapods.org/making/using-pod-lib-create.html
創(chuàng)建本地pod
pod lib create LCTreasureKit
創(chuàng)建本地pod的時候會回答幾個問題,按照項目的實際情況回答即可。
//選擇什么平臺來構(gòu)建pod
What platform do you want to use?? [ iOS / macOS ]
//選擇什么語言來構(gòu)建pod
What language do you want to use?? [ Swift / ObjC ]
//是否為你的庫生成一個xcode demo
Would you like to include a demo application with your library? [ Yes / No ]
//選擇測試框架,如果不知道官方建議選第一個
Which testing frameworks will you use? [ Specta / Kiwi / None ]
//基于試圖的測試
Would you like to do view based testing? [ Yes / No ]
//類的前綴
What is your class prefix?
成功創(chuàng)建后,會自動打開項目工程,如下圖所示

image.png
創(chuàng)建遠端倉庫
可以在github、碼云等平臺,為該pod創(chuàng)建一個遠端倉庫,這里已github為例:

image.png
綁定遠端庫
1.進入本地pod項目中,去綁定遠端倉庫。
git remote add origin httpsUrl
2.然后將本地代碼推送到遠端倉庫
git push -u origin master
修改本地.podspec文件
Pod::Spec.new do |s|
//名字
s.name = 'LCTreasureKit'
//版本
s.version = '1.0.1'
//摘要
s.summary = 'Treasure chest for iOS developers.'
//描述
s.description = "This is a collection of commonly used tool classes to help developers develop better."
//主頁
s.homepage = 'https://github.com/xxx-xx/LCTreasureKit.git'
//截圖
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Xu Lichao' => 'hzxx_xlc@163.com' }
//資源,遠端庫地址
s.source = { :git => 'https://github.com/xxx-xx/LCTreasureKit.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
//庫里包含的文件
s.source_files = 'LCTreasureKit/Classes/**/*'
//bundle資源
# s.resource_bundles = {
# 'LCTreasureKit' => ['LCTreasureKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
//依賴三方庫,如果需要多個,則寫多行
# s.dependency 'AFNetworking', '~> 2.3'
# s.dependency 'MJRefresh',
end
具體詳情可參考官網(wǎng)文檔https://guides.cocoapods.org/syntax/podspec.html
導入代碼

image.png

image.png
把你要新增的代碼放到classes文件夾下,然后導入到Kit目錄下。
進行git add 、git commit 和git push 三個常規(guī)步驟。
代碼準備推到pod庫的時候,要先打標簽,并推送標簽
git tag -m "first" "1.0.0" OR git tag 1.0.1
git push --tags
提交Pod
1.注冊pod 帳戶
pod trunk register yourMail yourName
注冊完記得去郵箱激活,激活成功后可以輸入pod trunk me 查看pod帳戶信息。
2.驗證pod
pod lib lint yourKit.podspec
3.提交pod
pod trunk push NAME.podspec

image.png
最后出現(xiàn)congrats信息,那么pod已經(jīng)提交成功了,可以在其他項目中添加使用該pod。