【iOS】制作Cocoapods庫

注冊(cè)trunk

終端執(zhí)行以下命令,這里的作者名和podspec中設(shè)置的s.author保持一致

$ pod trunk register 郵箱 '作者名稱’  

收到郵件并確認(rèn)之后再執(zhí)行以下命令

$ pod trunk me

創(chuàng)建一個(gè)pod庫

如果當(dāng)前已經(jīng)存在一個(gè) project,創(chuàng)建pod也非常簡單:

$ pod spec create PodName

隨后編輯podspec文件并執(zhí)行

$ pod spec lint PodName.podspec

然而創(chuàng)建并配置工程這種費(fèi)力的活交給 cocoapods 會(huì)更好。
所以我們這里使用 pod lib create PodName 去創(chuàng)建一個(gè)pod,過程如下:

$ pod lib create YDDemoSDK
  Cloning `https://github.com/CocoaPods/pod-template.git` into `YDDemoSDK`.
  Configuring YDDemoSDK 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 ]
 > Yes

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?
 > YD

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `YDDemoSDK` from `../`
Downloading dependencies
Installing YDDemoSDK (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `YDDemoSDK.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 YDDemoSDK_Example 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 'YDDemoSDK/Example/YDDemoSDK.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`.

替換代碼

將Classes文件夾下的代碼替換為自己將要發(fā)布的代碼

編輯podspec

.podspec 文件描述了一個(gè) pod 庫的版本。它詳細(xì)說明了這個(gè) pod 庫中源碼應(yīng)該從哪里取出、應(yīng)用怎樣的構(gòu)建設(shè)置以及其他基本的信息,比如名稱、版本、描述等。
podspec 文件的內(nèi)容如下:

Pod::Spec.new do |s|
s.name          = 'YDDemoSDK' #項(xiàng)目名
s.version       = '0.0.1' #相應(yīng)的版本號(hào)
s.summary       = 'A short description of YDDemoSDK.' #簡述
s.description   = <<‐ DESC #詳細(xì)描述
TODO: Add long description of the pod here.
DESC

s.homepage      = 'https://github.com/herody/YDDemoSDK' #項(xiàng)目主頁
s.license       = { :type => 'MIT', :file => 'LICENSE' } #開源協(xié)議
s.author        = { 'herody' => '314622981@qq.com' } #作者
s.platform      = :ios, '8.0' #支持的平臺(tái)
s.requires_arc  = true #arc和mrc選項(xiàng)
spec.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'  #A list of flags which should be passed to the compiler.

s.libraries     = 'z', 'sqlite3' #表示依賴的系統(tǒng)類庫,比如libz.dylib等
s.frameworks    = 'UIKit','AVFoundation' #表示依賴的系統(tǒng)framework
s.ios.vendored_frameworks = 'YDDemoSDK/Classes/YDKit.framework' # 依賴的第三方/自己的framework
s.vendored_libraries = 'YDDemoSDK/Classes/libWeChatSDK.a' #表示依賴第三方/自己的 .a 庫
#依賴的第三方的或者自己的靜態(tài)庫文件必須以lib為前綴進(jìn)行命名,否則會(huì)出現(xiàn)找不到的情況,這一點(diǎn)非常重要

s.preserve_paths = "IMPORTANT.txt","Frameworks/*.framework"  #Any file that should not be removed after being downloaded.

#平臺(tái)信息
s.platform      = :ios, '7.0' 
s.ios.deployment_target = '7.0'

#文件配置項(xiàng)
s.source        = { :git => 'https://github.com/herody/YDDemoSDK.git', :tag => s.version.to_s }
#配置項(xiàng)目的目標(biāo)路徑,如果不是本地開發(fā),pod init/update會(huì)從這個(gè)路去拉去代碼

s.source_files = 'YDDemoSDK/Classes/**/*.{h,m}' #你的源碼位置

#cocoapods 官方推薦使用 resource_bundles,因?yàn)?resources  指定的資源會(huì)被直接拷貝到目標(biāo)應(yīng)用中,
#因此不會(huì)被 Xcode 優(yōu)化,在編譯生成 product 時(shí),與目標(biāo)應(yīng)用的圖片資源以及其他同樣使用 resources 的 Pod 的圖片一起打包為一個(gè) Assets.car 文件。
#這樣全部混雜在一起,就使得資源文件容易產(chǎn)生命名沖突。而 resource_bundles 指定的資源,
#會(huì)被編譯到獨(dú)立的 bundle 中,bundle 名就是你的 pod 名,
#這樣就很大程度上減小了資源名沖突問題,并且 Xcode 會(huì)對(duì) bundle 進(jìn)行優(yōu)化。
#一個(gè) bundle 包含一個(gè) Assets.car,獲取圖片的時(shí)候要嚴(yán)格指定 bundle 的位置,
#很好的隔離了各個(gè)庫或者一個(gè)庫下的資源文件。
#s.resources     = ['YDDemoSDK/Assets/*.png'] #資源,比如圖片,音頻文件,xib等資源
s.resource_bundles = {
    'MapBox' => ['MapView/Map/Resources/*.png'],
    'OtherResources' => ['MapView/Map/OtherResources/*.png']
  }
s.public_header_files = 'YDDemoSDK/Classes/YDDemoSDK.h'   #需要對(duì)外開放的頭文件

#依賴的項(xiàng)目內(nèi)容 可以多個(gè)
s.dependency 'YYModel'
s.dependency 'AFNetworking' '2.3'
#s.static_framework = true
end

項(xiàng)目調(diào)試

進(jìn)入Example目錄,執(zhí)行

$ pod install

隨后運(yùn)行項(xiàng)目,保證項(xiàng)目正確性

進(jìn)行本地校驗(yàn)

推送之前,先在本地進(jìn)行校驗(yàn)

$ pod lib lint PodName.podspec

如果使用了其他私有庫,需要指定其他私有庫的倉庫地址和公有庫倉庫的地址

$ pod lib lint PodName.podspec --verbose --sources='私有庫地址,https://github.com/CocoaPods/Specs.git'

將代碼push到遠(yuǎn)程倉庫

在github上創(chuàng)建遠(yuǎn)程倉庫,并將本地代碼推送到遠(yuǎn)程倉庫

$ git status -- 查看當(dāng)前git存了什么文件
$ git add . -- 將所有文件緩存到待提交文件區(qū)域
$ git commit -m "上傳工程" -- 提交文件,寫上備注
$ git remote add origin 遠(yuǎn)程倉庫地址 -- 添加要推送的遠(yuǎn)程倉庫地址
$ git push -u origin master -- 將代碼推送到遠(yuǎn)程倉庫的master分支

為項(xiàng)目打標(biāo)簽

這時(shí)候直接進(jìn)行遠(yuǎn)程校驗(yàn)會(huì)報(bào)錯(cuò),需要先為項(xiàng)目打標(biāo)簽

$ git tag "0.0.1"
$ git push --tags

遠(yuǎn)程校驗(yàn)

打完標(biāo)簽之后,再進(jìn)行遠(yuǎn)程校驗(yàn),遠(yuǎn)程校驗(yàn)與本地校驗(yàn)幾乎一致,命令如下

$ pod spec lint PodName.podspec

上傳spec

$ pod trunk push PodName.podspec --allow-warnings

最后驗(yàn)證

終端執(zhí)行以下命令進(jìn)行搜索

$ pod search PodName

如果沒有搜索到,就刪除本地索引庫,再進(jìn)行嘗試

$ rm ~/Library/Caches/CocoaPods/search_index.json
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容