CocoaPods 私有化
一、創建所需要的代碼倉庫
- 創建 Spec 私有索引庫(ZFSpec),用來存放本地spec
- 創建模塊私有庫(ZFPodProject),用來存放項目工程文件
二、私有索引庫添加到本地 CocoaPods
操作命令:pod repo add {私有庫名字} {私有庫git地址}
打開終端,執行以下命令:
# cd 到本地的 CocoaPods 倉庫
cd ~/.cocoapods/repos
# 將 ZFSpec 私有庫添加到本地的 CocoaPods 倉庫
pod repo add ZFSpec //gitee.com/xxx/zfspec.git
三、模塊項目工程
1、創建
操作命令:pod lib create {模塊名}
# cd 到可以創建的目錄
cd Desktop/Module
# 創建模塊項目
pod lib create ZFPodProject
2、目錄介紹
ZFPodProject
ZFPodProject(私有庫目錄)
Assets(資源文件存放目錄)
Classes(代碼文件存放目錄)
Example(demo 目錄)
將 Classes 文件夾中的 “ReplaceMe.swift” 文件刪除,然後將自己所需要的代碼文件放到這個目錄下;將資源文件(圖片,xib文件…)放到 Assets 文件夾下,如圖:
3、代碼導入
cd 到 Example 文件下,然後pod install,更新 Example 項目的 pod
打開 Example 項目,Pod 下的 Development Pods 文件夾就是私有庫代碼
四、podspec 文件配置
1、修改 podspec 文件
語法參考網站://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 = '//www.baidu.com'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
# 作者
s.author = { 'Zephyr' => '[email protected]' }
# 項目地址,這裡不建議使用ssh地址,會有警告,建議使用http和https,最好是https
# 更改成自己的項目遠程倉庫地址
s.source = { :git => '//gitee.com/ZF_AloneOwl/zfpod-project.git', :tag => s.version.to_s }
# s.social_media_url = '//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根據是否有依賴庫來決定是否需要添加
# 依賴的frameworks
s.frameworks = 'UIKit', 'AVKit', 'Foundation'
# 依賴的公開庫或私有庫,如有多個,可以重複聲明
s.dependency 'Alamofire','~> 5.2.2'
s.dependency 'Moya','~> 14.0.0'
end
註:項目中的 Podfile 文件中默認 iOS 版本是 9.0(
platform :ios, '9.0'
),需要和 podspec 中設置的一致(s.ios.deployment_target = '11.0'
),我這裡需要將 Podfile 中的修改為 11.0。這裡不修改,在 pod install 時可能會報錯。
五、驗證及上傳
1、驗證本地 podspec 文件有效性
驗證有效性花費時間較長,建議先構建 demo,demo 中沒有報錯後,再進行驗證
操作命令:pod lib lint [操作參數]
參數說明:
# 指定源,比如你的私有pod同時依賴了公有庫和私有庫,你必須指定源才行,因為默認只會去在公有源中查找對應的依賴
--sources='私有庫地址,公有庫地址'
# 顯示詳情
--verbose
# 當構建當前私有庫時使用靜態庫的情況下使用
--use-libraries
# 跳過驗證 pod 是否可以導入階段
--skip-import-validation
# 允許警告
--allow-warnings
示例:
pod lib lint --sources='//gitee.com/ZF_AloneOwl/zfspec.git,//github.com/CocoaPods/Specs.git' --verbose --use-libraries --skip-import-validation --allow-warnings
註:以上參數不一定全部使用,是項目情況使用
2、本地項目文件上傳到遠程倉庫中
# 查看遠程倉庫地址
git remote -v
# 將本地項目與遠程倉庫相關聯:
git remote add origin {遠程倉庫地址}
# 修改遠程地址
git remote set-url origin {遠程倉庫地址}
# 拉取遠端代碼
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 文件本地和遠程有效性的驗證
操作命令:pod spec lint [操作參數]
pod spec lint --verbose --allow-warnings
註:操作參數見選項2
4、向私有 spec Repo 遠程倉庫中提交 podspec
操作命令:pod repo push {倉庫名} {項目名}.podspec [操作參數]
pod repo push ZFSpec ZFPodProject.podspec --verbose --allow-warnings
註:操作參數見選項2
5、上傳成功驗證
完成以上步驟,就表示已經創建了一個 CocoaPods 私有庫
前往文件夾 ~/.cocoapods/repos 去查看 ZFSpec 目錄下的文件如下:
.cocoapods 屬於隱藏文件夾
Mac 顯示隱藏文件夾快捷鍵:command + shift + .
使用 pod search ZFPodProject 進行搜索,結果如下就表示 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
解決辦法:終端執行 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 '//gitee.com/ZF_AloneOwl/zfspec.git'
# 若還需要使用其他三方庫,如 Moya 等,需要指定一個共有源
source '//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無響應等CDN: trunk – Cannot perform full-text search because Algolia returned an error: 0: Ca