一、創(chuàng)建github倉庫
步驟1 創(chuàng)建新倉庫
這里我們設(shè)置倉庫為公開的牙咏。
步驟2 新建一個(gè)文件夾,將我們剛才在github上創(chuàng)建的庫clone下來
git clone https://github.com/Shaw003/PrivatePodDemo.git
可以看到如下的目錄結(jié)構(gòu)
步驟3 創(chuàng)建Pod本地庫嘹裂。
#該命令會(huì)從github上CocoaPods的庫中下載一個(gè)pod模版
#這里筆者創(chuàng)建了一個(gè)名為PrivatePodDemo的Pod庫
pod lib create PrivatePodDemo
#之后終端會(huì)詢問你幾個(gè)問題妄壶,默認(rèn)左側(cè)選項(xiàng),選擇默認(rèn)可直接回車
#選擇運(yùn)行平臺(tái)寄狼,筆者這里選擇了iOS
What platform do you want to use?? [ iOS / macOS ]
>
ios
#選擇語言丁寄,筆者這里選擇了Swift
What language do you want to use?? [ Swift / ObjC ]
>
swift
#你希望創(chuàng)建一個(gè)帶有示例工程的庫么?筆者這里選擇是
Would you like to include a demo application with your library? [ Yes / No ]
>
yes
#使用測試框架么?筆者這里選擇否
Which testing frameworks will you use? [ Quick / None ]
> none
#是否要進(jìn)行基于視圖的測試伊磺,其實(shí)具體含義我也不太清楚盛正,筆者這里選擇否
Would you like to do view based testing? [ Yes / No ]
> no
之后終端會(huì)自動(dòng)打開你的Example工程,可以看到文件目錄結(jié)構(gòu)如下所示:
最外層的<PrivatePodDemo>
就是步驟2從github上clone下來的文件夾屑埋,<PrivatePodDemo/PrivatePodDemo>
是步驟3輸入的名稱
步驟4 編輯PrivatePodDemo.podspec文件
內(nèi)容如下所示:
Pod::Spec.new do |s|
s.name = 'PrivatePodDemo'
s.version = '0.1.0'
s.summary = 'A short description of PrivatePodDemo.'
# 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/Shaw003/PrivatePodDemo'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Shaw' => 'shawtun1211@163.com' }
s.source = { :git => 'https://github.com/Shaw003/PrivatePodDemo.git', :tag => s.version.to_s }
# s.social_media_url = 'http://www.reibang.com/u/596fa2382f62'
# 如果需要設(shè)置語言版本的話可以這樣做
s.swift_version = '4.2'
s.ios.deployment_target = '8.0'
s.source_files = 'PrivatePodDemo/Classes/**/*'
# s.resource_bundles = {
# 'PrivatePodDemo' => ['PrivatePodDemo/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'Alamofire'
s.dependency 'WCDB.swift', '1.0.7.5'
end
以上按照自己的情況修改即可豪筝,值得注意的是兩個(gè)地方,
關(guān)注點(diǎn)1: s.dependency
如果你開發(fā)的庫引用到了別的第三方庫雀彼,可以使用s.dependency
字段來寫你引用了哪些三方庫壤蚜,值得注意的是,如果引用了多個(gè)徊哑,需要寫多個(gè)s.dependency,舉例:
s.dependency 'Alamofire'
s.dependency 'WCDB.swift'
關(guān)注點(diǎn)2: s.source_files
這里它默認(rèn)寫的路徑是PrivatePodDemo/Classes/**/*
聪富,我們可以打開對應(yīng)的文件夾莺丑,發(fā)現(xiàn)默認(rèn)有一個(gè)Replace.swift
的文件,這樣給人的理解是墩蔓,我們可以將希望通過Pod分享出去的源代碼放到<PrivatePodDemo/PrivatePodDemo/PrivatePodDemo/Classes>
文件夾下梢莽,當(dāng)我們對這里的源代碼有任何修改的時(shí)候,都需要先設(shè)置工作路徑為包含Podfile
文件的目錄奸披,然后執(zhí)行pod install
命令昏名。
首先我們找到并刪除掉Classes
文件夾中的Replace.swift
,以我們自己的代碼對其進(jìn)行替換阵面,替換后的目錄結(jié)構(gòu)如下所示轻局,
這里我們嘗試刪除掉Replace.swift
文件,并將你的工程代碼文件拖入到這里样刷,這時(shí)候我們可以執(zhí)行pod install
了
// 這里我們發(fā)現(xiàn)`Podfile`文件在`Example`文件夾中
cd PrivatePodDemo/PrivatePodDemo/Example
文件
pod install
執(zhí)行完畢后仑扑,我們可以看到,在Xcode中置鼻,目錄結(jié)構(gòu)已變成如下所示镇饮,
我們對工程進(jìn)行編譯一下,發(fā)現(xiàn)沒有報(bào)錯(cuò)箕母,編譯通過储藐。
步驟5 提交git
操作1 提交到本地倉庫
git add -A
git commit -m "commit code"
在嘗試提交git時(shí)時(shí)會(huì)報(bào)一個(gè)錯(cuò)誤,
原因是因?yàn)閯?chuàng)建pod lib時(shí)已經(jīng)包括了.git
文件了,此時(shí)需要終端切換工作路徑到<PrivatePodDemo/PrivatePodDemo>
cd PrivatePodDemo/
ls -a
操作2 刪除.git文件
此時(shí)只需要?jiǎng)h除.git文件即可嘶是,執(zhí)行如下命令:
rm -rf .git
刪除后钙勃,我們再嘗試添加到本地倉庫,執(zhí)行命令
操作3 提交到遠(yuǎn)端
git commit -m "commit code"
git push
提交后我們發(fā)現(xiàn)只提交了一個(gè)PrivatePodDemo
的空文件夾俊啼,里面什么都沒有肺缕,這時(shí)候我們可以刪除遠(yuǎn)端剛才的提交,重新提交一次
操作4 刪除遠(yuǎn)端提交
git rm -r --cached PrivatePodDemo
git commit -m "刪除錯(cuò)誤提交"
git push
刪除完成后我們重復(fù)操作1,再次提交就可以了同木。
步驟6 創(chuàng)建git tag
此處創(chuàng)建的tag需要和podspec
文件中s.version
字段保持一致浮梢,這里我們寫0.1.0
git tag 0.1.0
git push --tags
二、 上傳Pod
步驟1 驗(yàn)證pod
執(zhí)行如下命令進(jìn)行驗(yàn)證
pod spec lint
等了一會(huì)彤路,發(fā)現(xiàn)驗(yàn)證不通過秕硝,原因有一個(gè)錯(cuò)誤和幾個(gè)警告,我們看下錯(cuò)誤信息
- ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.
這時(shí)洲尊,我們可以對PrivatePodDemo.podspec
文件進(jìn)行如下修改
s.source_files = 'PrivatePodDemo/PrivatePodDemo/Classes/**/*'
再次執(zhí)行驗(yàn)證远豺,我們可以忽略警告
pod spec lint --allow-warnings
這次驗(yàn)證結(jié)果
PrivatePodDemo.podspec passed validation.
驗(yàn)證通過了,這時(shí)你可能會(huì)問坞嘀,那剛才第一次在編輯s.source_files
字段時(shí)為什么不直接寫成最后一次修改這樣呢躯护?具體原因我還不太清楚,不過經(jīng)過我測試發(fā)現(xiàn)丽涩,source_files
兩種寫法棺滞,
寫法1:PrivatePodDemo/Classes/**/*
,
寫法2:PrivatePodDemo/PrivatePodDemo/Classes/**/*
矢渊,
如果采用 寫法1 在執(zhí)行pod install
命令時(shí)继准,可以正常獲取保存在Classes
文件夾中的代碼;當(dāng)執(zhí)行pod spec lint
命令時(shí)卻由于沒找打源碼文件而未能驗(yàn)證通過矮男。
如果采用 寫法2 則在執(zhí)行pod install
命令時(shí)移必,無法正常獲取到保存在Classes
文件夾中的代碼,而執(zhí)行pod spec lint
命令時(shí)卻能驗(yàn)證通過毡鉴。
因此崔泵,筆者采用了先用 寫法1 提交到github的倉庫,再修改為 寫法2 去進(jìn)行驗(yàn)證的方式眨补。
步驟2 上傳Pod
由于之前我們創(chuàng)建的這個(gè)示例工程的名字不太好管削,和線上的有沖突了,我們這里修改.podspec
文件名為PrivatePodDemo001.podspec
撑螺,同時(shí)含思,調(diào)整文件內(nèi)的s.name = 'PrivatePodDemo001'
接下來執(zhí)行命令
pod trunk push PrivatePodDemo001.podspec --allow-warnings
等待一會(huì),出現(xiàn)如下內(nèi)容甘晤,說明上傳成功了
步驟3 搜索Pod
直接輸入如下指令
pod search PrivatePodDemo001
報(bào)錯(cuò)了含潘,提示如下:
[!] Unable to find a pod with name, author, summary, or description matching `PrivatePodDemo001`
這時(shí)我們可以嘗試重新設(shè)置一下pod的索引
rm ~/Library/Caches/CocoaPods/search_index.json
pod setup
之后再執(zhí)行一下搜索就可以搜到了。
三线婚、 示例工程驗(yàn)證
步驟1 新建一個(gè)工程
使用pod 'PrivatePodDemo001'
可以將需要的源碼下載下來遏弱。