[TOC]
CocoaPods 不僅可以將開源代碼添加到項目中,而且還可以跨項目共享組件。 您可以使用私人規格回購來執行此操作。
有幾個步驟可以為您的項目獲取專用 pod 設置; 為他們創建一個私有存儲庫,讓 CocoaPods 知道在哪里找到它并將 podspecs 添加到存儲庫。
# 1. 創建流程
## 1.1 創建一個私人 Spec Repo
要處理您的私人 pod 集合,我們建議您創建自己的 Spec 倉庫。 這應該放在所有使用倉庫的人都可以訪問的位置。
你不需要分叉CocoaPods / Specs Master 倉庫。 確保團隊中的每個人都可以訪問此倉庫,但不需要公開。
## 1.2 將您的私人倉庫添加到您的 CocoaPods 安裝中
~~~
$ pod repo add REPO_NAME SOURCE_URL
~~~
注意:如果您打算在本地創建 pod,則應該有權訪問 SOURCE_URL
要檢查您的安裝是否成功并準備就緒:
~~~
$ cd ~/.cocoapods/repos/REPO_NAME
$ pod repo lint .
~~~
## 1.3 將 Podspec 添加到您的倉庫
確保你已經正確標記和版本化你的源代碼,然后運行:
~~~
$ pod repo push REPO_NAME SPEC_NAME.podspec
~~~
這將運行 `pid spec lint`,并在您的私人倉庫中關注設置規范的所有小細節。你的倉庫結構應該反映這一點:
~~~
.
├── Specs
└── [SPEC_NAME]
└── [VERSION]
└── [SPEC_NAME].podspec
~~~
## 1.4 就是這樣!
您的私有 Pod 已準備好在 Podfile 中使用。 您可以在 Podfile 中使用帶有[ source 指令](https://guides.cocoapods.org/syntax/podfile.html#source)的 Specs 倉庫,如以下示例所示:
~~~
source 'URL_TO_REPOSITORY'
~~~
# 2. 示例
## 2.1 創建一個私有 Spec 倉庫
~~~
$ cd /opt/git
$ mkdir Specs.git
$ cd Specs.git
$ git init --bare
~~~
> 本示例的其余部分使用 https://github.com/artsy/Specs 上的倉庫
>
## 2.2 將您的私人倉庫添加到您的 CocoaPods 安裝中
在你的服務器上使用你的倉庫的 URL,添加你的倉庫通過:
~~~
$ pod repo add artsy-specs git@github:artsy/Specs.git
~~~
檢查您的安裝是否成功并準備好:
~~~
$ cd ~/.cocoapods/repos/artsy-specs
$ pod repo lint .
~~~
## 2.3 將 Podspec 添加到您的倉庫
創建 Podspec
~~~
cd ~/Desktop
touch Artsy+OSSUIFonts.podspec
~~~
Artsy + OSSUIFonts.podspec 應該在您選擇的文本編輯器中打開。 典型的內容是
~~~
Pod::Spec.new do |s|
s.name = "Artsy+OSSUIFonts"
s.version = "1.1.1"
s.summary = "The open source fonts for Artsy apps + UIFont categories."
s.homepage = "https://github.com/artsy/Artsy-OSSUIFonts"
s.license = 'Code is MIT, then custom font licenses.'
s.author = { "Orta" => "orta.therox@gmail.com" }
s.source = { :git => "https://github.com/artsy/Artsy-OSSUIFonts.git", :tag => s.version }
s.social_media_url = 'https://twitter.com/artsy'
s.platform = :ios, '7.0'
s.requires_arc = true
s.source_files = 'Pod/Classes'
s.resources = 'Pod/Assets/*'
s.frameworks = 'UIKit', 'CoreText'
s.module_name = 'Artsy_UIFonts'
end
~~~
保存您的 Podspec 并添加到倉庫:
~~~
pod repo push artsy-specs ~/Desktop/Artsy+OSSUIFonts.podspec
~~~
假設您的 Podspec 有效,它將被添加到倉庫。 倉庫現在看起來像這樣
~~~
.
├── Specs
└── Artsy+OSSUIFonts
└── 1.1.1
└── Artsy+OSSUIFonts.podspec
~~~
有關如何包含 repo URL 的示例,請參閱此 [Podfile](https://github.com/artsy/eigen/blob/master/Podfile)。
# 3. 如何刪除私人倉庫
~~~
pod repo remove [name]
~~~
# 4. 外部資源
* [Using CocoaPods to Modularise a Big iOS App by @aroldan](http://dev.hubspot.com/blog/architecting-a-large-ios-app-with-cocoapods)