一搂誉、創(chuàng)建所需要的代碼倉庫
- 創(chuàng)建 Spec 私有索引庫(ZFSpec),用來存放本地spec
- 創(chuàng)建模塊私有庫(ZFPodProject),用來存放項目工程文件
二广辰、私有索引庫添加到本地 CocoaPods
操作命令:pod repo add {私有庫名字} {私有庫git地址}
打開終端,執(zhí)行以下命令:
# cd 到本地的 CocoaPods 倉庫
cd ~/.cocoapods/repos
# 將 ZFSpec 私有庫添加到本地的 CocoaPods 倉庫
pod repo add ZFSpec https://gitee.com/xxx/zfspec.git
三币喧、模塊項目工程
1轨域、創(chuàng)建
操作命令:pod lib create {模塊名}
# cd 到可以創(chuàng)建的目錄
cd Desktop/Module
# 創(chuàng)建模塊項目
pod lib create ZFPodProject
2、目錄介紹
ZFPodProject
?ZFPodProject(私有庫目錄)
??Assets(資源文件存放目錄)
??Classes(代碼文件存放目錄)
?Example(demo 目錄)
將 Classes 文件夾中的 "ReplaceMe.swift" 文件刪除杀餐,然后將自己所需要的代碼文件放到這個目錄下干发;將資源文件(圖片,xib文件...)放到 Assets 文件夾下史翘,如圖:
3枉长、代碼導(dǎo)入
cd 到 Example 文件下,然后pod install琼讽,更新 Example 項目的 pod
打開 Example 項目必峰,Pod 下的 Development Pods 文件夾就是私有庫代碼
四、podspec 文件配置
1钻蹬、修改 podspec 文件
語法參考網(wǎng)站:https://guides.cocoapods.org/syntax/podspec.html
podspec 文件注釋
Pod::Spec.new do |s|
# 項目名稱
s.name = 'ZFPodProject'
# 版本號
s.version = '0.1.0'
# 項目摘要
s.summary = 'A short description of GOOCR.'
# 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
GOOCR的項目描述吼蚁,這是G1項目個人私有庫
DESC
# 主頁,這里要填寫可以訪問到的地址问欠,不然驗證不通過
s.homepage = 'https://www.baidu.com'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
# 作者
s.author = { 'Zephyr' => '2049982764@qq.com' }
# 項目地址肝匆,這里不建議使用ssh地址,會有警告顺献,建議使用http和https旗国,最好是https
# 更改成自己的項目遠(yuǎn)程倉庫地址
s.source = { :git => 'https://gitee.com/ZF_AloneOwl/zfpod-project.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '11.0'
# 源碼
s.source_files = 'ZFPodProject/Classes/**/*'
# 資源文件
s.resource_bundles = {
'ZFPodProject' => [
'ZFPodProject/Assets/*.png',
'ZFPodProject/Assets/*.ttf'
]
}
# swift 版本
s.swift_versions=['5']
# s.frameworks和s.dependency根據(jù)是否有依賴庫來決定是否需要添加
# 依賴的frameworks
s.frameworks = 'UIKit', 'AVKit', 'Foundation'
# 依賴的公開庫或私有庫,如有多個注整,可以重復(fù)聲明
s.dependency 'Alamofire','~> 5.2.2'
s.dependency 'Moya','~> 14.0.0'
end
注:項目中的 Podfile 文件中默認(rèn) iOS 版本是 9.0(
platform :ios, '9.0'
)能曾,需要和 podspec 中設(shè)置的一致(s.ios.deployment_target = '11.0'
)度硝,我這里需要將 Podfile 中的修改為 11.0。這里不修改寿冕,在 pod install 時可能會報錯蕊程。
五、驗證及上傳
1蚂斤、驗證本地 podspec 文件有效性
驗證有效性花費時間較長存捺,建議先構(gòu)建 demo,demo 中沒有報錯后曙蒸,再進行驗證
操作命令:pod lib lint [操作參數(shù)]
參數(shù)說明:
# 指定源捌治,比如你的私有pod同時依賴了公有庫和私有庫,你必須指定源才行纽窟,因為默認(rèn)只會去在公有源中查找對應(yīng)的依賴
--sources='私有庫地址,公有庫地址'
# 顯示詳情
--verbose
# 當(dāng)構(gòu)建當(dāng)前私有庫時使用靜態(tài)庫的情況下使用
--use-libraries
# 跳過驗證 pod 是否可以導(dǎo)入階段
--skip-import-validation
# 允許警告
--allow-warnings
示例:
pod lib lint --sources='https://gitee.com/ZF_AloneOwl/zfspec.git,https://github.com/CocoaPods/Specs.git' --verbose --use-libraries --skip-import-validation --allow-warnings
注:以上參數(shù)不一定全部使用肖油,是項目情況使用
2、本地項目文件上傳到遠(yuǎn)程倉庫中
# 查看遠(yuǎn)程倉庫地址
git remote -v
# 將本地項目與遠(yuǎn)程倉庫相關(guān)聯(lián):
git remote add origin {遠(yuǎn)程倉庫地址}
# 修改遠(yuǎn)程地址
git remote set-url origin {遠(yuǎn)程倉庫地址}
# 拉取遠(yuǎn)端代碼
git pull origin master --allow-unrelated-histories
git status
git add .
git commit -m '提交消息'
git status
git pull origin master
git push origin master
# tag 要與podspec文件中的版本號一致
git tag -a 0.1.0 -m '提交消息'
git push —tags
git tag
3臂港、podspec 文件本地和遠(yuǎn)程有效性的驗證
操作命令:pod spec lint [操作參數(shù)]
pod spec lint --verbose --allow-warnings
注:操作參數(shù)見選項2
4森枪、向私有 spec Repo 遠(yuǎn)程倉庫中提交 podspec
操作命令:pod repo push {倉庫名} {項目名}.podspec [操作參數(shù)]
pod repo push ZFSpec ZFPodProject.podspec --verbose --allow-warnings
注:操作參數(shù)見選項2
5、上傳成功驗證
完成以上步驟审孽,就表示已經(jīng)創(chuàng)建了一個 CocoaPods 私有庫
前往文件夾 ~/.cocoapods/repos 去查看 ZFSpec 目錄下的文件如下:
.cocoapods 屬于隱藏文件夾
Mac 顯示隱藏文件夾快捷鍵:command + shift + .
使用 pod search ZFPodProject 進行搜索县袱,結(jié)果如下就表示 ZFPodProject 可以正常使用了
pod search xxx 報錯
Ignoring ffi-1.12.2 because its extensions are not built. Try: gem pristine ffi --version 1.12.2 Creating search index for spec repo 'ZFSpec'.. Done! [!] CDN: trunk - Cannot perform full-text search because Algolia returned an error: 0: Cannot reach any host: execution expired, execution expired, execution expired, execution expired
解決辦法:終端執(zhí)行 pod repo remove trunk 移除 trunk 源
六、CocoaPods 私有庫使用
在使用私有庫時佑力,必須在 Podfile 文件中使用 source 指定私有庫源
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
inhibit_all_warnings!
# 私有源
source 'https://gitee.com/ZF_AloneOwl/zfspec.git'
# 若還需要使用其他三方庫式散,如 Moya 等,需要指定一個共有源
source 'https://gitee.com/mirrors/CocoaPods-Specs.git'
target 'PodDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'ZFPodProject'
pod 'Moya'
end
參考文章:
CocoaPods搭建私有庫
CocoaPods使用
pod install無響應(yīng)等CDN: trunk - Cannot perform full-text search because Algolia returned an error: 0: Ca