使用 CocoaPods 建立一個(gè)私有庫(kù),將項(xiàng)目中的公共組件交由它管理,方便于其它項(xiàng)目的引用,也是組件化項(xiàng)目開(kāi)發(fā)的第一步。
整體說(shuō)明一下創(chuàng)建私有庫(kù)的步驟:
- 創(chuàng)建并設(shè)置一個(gè)私有的 Spec Repo。
- 創(chuàng)建Pod的所需要的項(xiàng)目工程文件,并且有可訪問(wèn)的項(xiàng)目版本控制地址。
- 創(chuàng)建 Pod 所對(duì)應(yīng)的 podspec 文件。
- 上傳項(xiàng)目到 GitHub,并打上 tag。
- 本地測(cè)試配置好的 podspec文件是否可用。
- 向私有的 Spec Repo 中提交 podspec。
- 在個(gè)人項(xiàng)目中的 Podfile 中增加剛剛制作的好的 Pod 并使用。
創(chuàng)建私有的 Spec Repo
什么是Spec Repo?
他是所有的 Pods 的一個(gè)索引,就是一個(gè)容器,所有公開(kāi)的 Pods 都在這個(gè)里面,他實(shí)際是一個(gè) Git 倉(cāng)庫(kù) remote 端
在 GitHub 上,但是當(dāng)你使用了 Cocoapods 后他會(huì)被 clone 到本地的 ~/.cocoapods/repos 目錄下,可以進(jìn)入到這個(gè)目錄看到 master 文件夾就是這個(gè)官方的 Spec Repo 了。這個(gè) master 目錄的結(jié)構(gòu)是這個(gè)樣子的:
.
├── Specs
└── [SPEC_NAME]
└── [VERSION]
└── [SPEC_NAME].podspec
因此我們需要?jiǎng)?chuàng)建一個(gè)類似于 master 的私有 Spec Repo,這里我們可以 fork 官方的 Repo,也可以自己創(chuàng)建,建議不 fork,因?yàn)槟阒皇窍胩砑幼约旱?Pods,沒(méi)有必要把現(xiàn)有的公開(kāi) Pods 都 copy 一份,就如官方文檔所說(shuō):
1.Create a Private Spec Repo
To work with your collection of private pods, we suggest creating your own Spec repo. This should be in a location that is accessible to all who will use the repo.
You do not need to fork the CocoaPods/Specs Master repo. Make sure that everyone on your team has access to this repo, but it does not need to be public.
在 github 創(chuàng)建完私有庫(kù) VJRepos 后,執(zhí)行下面命令:
$ pod repo add VJRepos https://github.com/Vergil-wj/VJRepos
成功的話就會(huì)在~/.cocoapods/repos目錄下看到 VJRepos 文件夾了,如下圖:

創(chuàng)建 Pod 的所需要的項(xiàng)目工程文件,并且有可訪問(wèn)的項(xiàng)目版本控制地址
如果是有現(xiàn)有的組件項(xiàng)目,并且在 Git 的版本管理下,那么這一步就算完成了,可以直接進(jìn)行下一步了。
或者從 github 上新建一個(gè)空項(xiàng)目,勾選上 LICENSE 文件,并 clone 到本地,將項(xiàng)目中的公共組件拖進(jìn)來(lái)。
創(chuàng)建 Pod 所對(duì)應(yīng)的 podspec 文件
在項(xiàng)目根目錄下,輸入:
$ pod spec create VJDviceInfo
會(huì)看到項(xiàng)目中,多出一個(gè) .podspec 文件

打開(kāi) .podspec 文件,并修改信息如下:
Pod::Spec.new do |s|
s.name = "VJDeviceInfo"
s.version = "1.0.0"
s.ios.deployment_target = '8.0'
s.summary = "ios device info"
s.homepage = "https://github.com/Vergil-wj/VJDeviceInfo"
s.license = "MIT"
s.author = { "houweijia" => "houweijiav@163.com" }
s.source = { :git => "https://github.com/Vergil-wj/VJDeviceInfo.git", :tag => s.version }
s.source_files = "VJDeviceInfo"
s.requires_arc = true
end
參數(shù)說(shuō)明:
- s.name:庫(kù)名,和.podspec名字保持一致。
- s.versin:版本號(hào)。
- s.ios.deployment_target:支持最低版本。
- s.summary:簡(jiǎn)介
- s.homepage:項(xiàng)目主頁(yè)地址
- s.license:許可證
- s.author:作者
- s.source:項(xiàng)目的地址
- s.source_files:需要包含的源文件
- s.requires_arc:是否支持ARC
其它:
s.social_media_url:社交網(wǎng)址,你的podspec發(fā)布成功后會(huì)@你
-
s.dependency:依賴庫(kù),不能依賴未發(fā)布的庫(kù),例如:
s.dependency 'AFNetworking'
未涉及到的請(qǐng)看官方文檔。
上傳項(xiàng)目到 GitHub,并打上 tag
1、上傳到 GitHub
git add .
git commit -m '1.0.0 release'
git push
2、打上 tag
git tag 1.0.0
git push origin 1.0.0
tag 的版本號(hào)要和 .podspec 中的版本號(hào)保持一致。
本地測(cè)試配置好的 podspec 文件是否可用
兩種方式驗(yàn)證:
第一種:$ pod lib lint
第二種:$ pod spec lint VJDeviceInfo.podspec --verbose
- verbose:輸出詳細(xì)信息
我使用的第二種,輸出結(jié)果如下:

驗(yàn)證成功!如果驗(yàn)證出錯(cuò),請(qǐng)看文章末尾。
兩種方式驗(yàn)證的不同之處:
The difference between them is that pod lib lint does not access the network, whereas pod spec lint checks the external repo and associated tag.
pod lib lint 是只從本地驗(yàn)證你的 pod 能否通過(guò)驗(yàn)證.
pod spec lint 是從本地和遠(yuǎn)程驗(yàn)證你的 pod 能否通過(guò)驗(yàn)證.
向私有的 Spec Repo 中提交 podspec
向我們的私有 Spec Repo 提交 podspec 只需要一個(gè)命令:
$ pod repo push VJRepos VJDeviceInfo.podspec
完成之后這個(gè)組件庫(kù)就添加到我們的私有 Spec Repo 中了,可以進(jìn)入到~/.cocoapods/repos/VJRepos目錄下查看:

再去看我們的 Spec Repo 遠(yuǎn)端倉(cāng)庫(kù),也有了一次提交,這個(gè) podspec 也已經(jīng)被Push上去了。
在個(gè)人項(xiàng)目中的 Podfile 中增加剛剛制作的好的 Pod 并使用

第一行是官方的 Spec repo,第二行是自己的 Spec reop;
然后打開(kāi)項(xiàng)目可以看到,我們自己的庫(kù)文件已經(jīng)出現(xiàn)在 Pods 子項(xiàng)目中的 Pods 子目錄下了。
到此,使用 CocoaPods 創(chuàng)建私有庫(kù)已經(jīng)完成,我們可以方便的管理自己的組件了。
以上步驟中可能出現(xiàn)的錯(cuò)誤
1、
ERROR | [iOS] unknown: Encountered an unknown error (Could not find a
iossimulator (valid values: com.apple.coresimulator.simruntime.ios-12-2, com.apple.coresimulator.simruntime.tvos-12-2, com.apple.coresimulator.simruntime.watchos-5-2). Ensure that Xcode -> Window -> Devices has at least oneiossimulator listed or otherwise add one.) during validation.
升級(jí)CocoaPods:
$ sudo gem install cocoapods
2、
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 29913 and retry the command.
輸入以下命令:
$ git config merge.renameLimit 999999
$ git config --unset merge.renameLimit
3、
[!] There was an error pushing a new version to trunk: execution expired
執(zhí)行:
$ pod trunk push VJUtils.podspec
若還是報(bào)這條錯(cuò)誤,那就多試幾次;
參考資料
CocoaPods官網(wǎng) https://guides.cocoapods.org/making/private-cocoapods.html
http://blog.wtlucky.com/blog/2015/02/26/create-private-podspec/