一. CocoaPods公共倉庫
查看CocoaPods 本地目錄
打開finder command + shift + g ~/.cocoapods

- master是CocoaPos對應(yīng)的公共git倉庫
- mySpecs是自己創(chuàng)建的私有倉庫地址,后文會提到。
cd 到 master目錄
$ cd ~/.cocoapods/repos/master
$ git remote -v
打印臺顯示:
origin https://github.com/CocoaPods/Specs.git (fetch)
origin https://github.com/CocoaPods/Specs.git (push)

我們點開master下的Specs目錄
打開其中一個.podspec.json文件
會發(fā)現(xiàn)CocosPods管理的很多框架以及對應(yīng)的版本號,每一個json文件對應(yīng)該框架的一個版本,它描述了每個對應(yīng)版本的框架的信息、配置、及源碼下載地址。

二. 搭建自己的pods私有庫
1、創(chuàng)建私有的Spec Repo
Spec Repo 是所有公開的Pods 的podspec文件的一個git倉庫,當(dāng)使用Cocoapods后它會被clone到本地的~/.cocoapods/repos目錄下,可以進入到這個目錄看到master文件夾就是這個官方的Spec Repo了。因此我們需要創(chuàng)建一個類似于master的私有Spec Repo 。同理這個私有Spec Repo我們也要有一個遠(yuǎn)程端。那么我們需要創(chuàng)建一個 Git倉庫,這個倉庫你可以創(chuàng)建私有的也可以創(chuàng)建公開的。如果是私有的話,項目中其他同事,你要給他這個Git倉庫的權(quán)限。
在github上創(chuàng)建一個mySpecs倉庫
在終端執(zhí)行命令:
$pod repo add mySpecs https://github.com/xxx/mySpecs.git
之后在~/.cocoapods/repos目錄下就能看到mySpecs
2、創(chuàng)建Pods工程
在本地創(chuàng)建一個test目錄
cd到test目錄并執(zhí)行終端命令
$pod lib create pod_test
終端會顯示
Cloning `https://github.com/CocoaPods/pod-template.git` into `pod_test`.
Configuring pod_test template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
2018-03-01 15:35:21.732 defaults[11084:244747]
The domain/default pair of (org.cocoapods.pod-template, HasRunbefore) does not exist
If this is your first time we recommend running through with the guide:
- http://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
Press return to continue.
依次填寫iOS、ObjC、Yes、Specta、Yes、JkW
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
>ObjC
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> Specta
Would you like to do view based testing? [ Yes / No ]
> Yes
What is your class prefix?
> JKW
設(shè)置完成后打印臺會輸出
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `pod_test` from `../`
Downloading dependencies
Installing Expecta (1.0.6)
Installing Expecta+Snapshots (3.1.1)
Installing FBSnapshotTestCase (2.1.4)
Installing Specta (1.0.7)
Installing pod_test (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `pod_test.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.
[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'pod_test/Example/pod_test.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
成功后會在test目錄下創(chuàng)建一個pod_test工程
目錄如下:

我們來添加一個分類文件

NSString+Test分類文件如下
#import <Foundation/Foundation.h>
@interface NSString (Test)
- (void)test;
@end
#import "NSString+Test.h"
@implementation NSString (Test)
- (void)test{
NSLog(@"只是一個測試");
}
@end
cd 在Example工程目錄下執(zhí)行 pod update命令
打開項目工程,可以看到庫文件都被加載到Pods子項目中了
不過它們并沒有在Pods目錄下,而是跟測試項目一樣存在于Development Pods/MyLib中,這是因為我們是在本地測試,而沒有把podspec文件添加到Spec Repo中的緣故。
注:這里需要注意的是每當(dāng)添加了新的文件或者以后更新了podspec的版本都需要重新執(zhí)行一遍pod update命令。
4、提交pods庫到github上
cd 到pod_test目錄下
$git init
$git add .
$ git commit -am "第一次提交"
$ git remote add origin https://github.com/xxx/pod_test
$ git push origin master
//一定要有標(biāo)簽,不然會有下面的警告
//podspec文件中獲取Git版本控制的項目需要tag號,
$ git tag -m "first release" "0.1.0"
$ git push --tags
5、配置pod_test的podspec文件
該pod_test.podspec文件在執(zhí)行$pod lib create pod_test已經(jīng)被自動創(chuàng)建
podspec 文件格式
Pod::Spec.new do |s|
s.name = 'pod_test'
s.version = '0.1.0'
s.summary = '這是我第一個私有庫項目demo'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/kamto6/pod_test'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'kwjie5566@163.com' => 'kangwei.jie@mydeertrip.com' }
s.source = { :git => 'https://github.com/xxx/pod_test.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'pod_test/Classes/**/*'
# s.resource_bundles = {
# 'pod_test' => ['pod_test/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
s.name :pod search 搜索的關(guān)鍵詞,注意這里一定要和.podspec的名稱一樣
s.version :版本號,每一個版本對應(yīng)一個tag
s.summary : 簡介
s.homepage : 項目主頁地址
s.license : 許可證
s.author : 作者
s.social_media_url : 社交網(wǎng)址
s.source : 項目的地址
s.source_files : 需要包含的源文件
s.resources: 資源文件
s.requires_arc : 是否支持ARC
s.dependency :依賴庫
s.ios.deployment_target = '8.0' : 支持的pod最低版本
如:
s.source_files = 'pod_test/Classes/**/*'
“*” 表示匹配所有文件
“**” 表示匹配所有子目錄
驗證該podspec文件,cd到pod_test目錄
$ pod spec lint
pod spec相對于pod lib會更為精確,pod lib相當(dāng)于只驗證一個本地倉庫,pod spec會同時驗證本地倉庫和遠(yuǎn)程倉庫。
打印臺提示:
-> pod_test (0.1.0)
- WARN | summary: The summary is not meaningful.
- WARN | url: The URL (https://github.com/kwjie5566@163.com/pod_test) is not reachable.
[!] pod_test did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.
當(dāng)然我們驗證時可以忽略這些警告
$ pod spec lint --allow-warnings
--allow-warnings是允許warning的存在,也就是說當(dāng)你在pod spec lint驗證podspec的時候,如果不加這句,而你的代碼里又一些警告的話,是驗證不通過的。而加上這句話的話,有警告也能驗證通過。
終端輸入:vim pod_test.podspec
按i進入編輯狀態(tài)修改:summary (隨便填寫即可) 和 url(post_test對應(yīng)的倉庫) ,再按esc + : + wq 退出
終端再執(zhí)行$ pod spec lint
打印臺提示:
-> pod_test (0.1.0)
pod_test passed validation.
表示這個podspec文件驗證通過,否則修改到驗證通過為止
6、在Example工程修改podfile文件
#pod 'pod_test', :path => '../'
pod 'pod_test',:podspec => '../pod_test.podspec'
再執(zhí)行pod update
有時候當(dāng)你使用pod update時會發(fā)現(xiàn)特別慢,那是因為pod會默認(rèn)先更新一次podspec索引。使用--no-repo-update參數(shù)可以禁止其做索引更新操作。
如:pod update --no-repo-update
$cd Example/
$ pod update
Installing pod_test (0.1.0)
[!] Error installing pod_test
[!] /usr/bin/git clone https://github.com/kamto6/pod_test.git /var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj --template= --single-branch --depth 1 --branch 0.1.0
Cloning into '/var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj'...
warning: Could not find remote branch 0.1.0 to clone.
fatal: Remote branch 0.1.0 not found in upstream origin
[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
cd 到pod_test目錄
$ git tag -m "first release" "0.1.0"
$ git push --tags
然后再執(zhí)行
$cd Example/
$ pod update
再打開Example工程,發(fā)現(xiàn)庫文件都被加載到Pods項目下
6、把pod_test.podspec提交到Spec Repo倉庫
pod repo push [本地Spec Repo名稱][podspec文件路徑]
$ pod repo push mySpecs pod_test.podspec
Validating spec
-> pod_test (0.1.0)
Updating the `mySpecs' repo
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
Adding the spec to the `mySpecs' repo
- [Add] pod_test (0.1.0)
Pushing the `mySpecs' repo
表示這個庫已經(jīng)提交到Spec Repo上了

在對應(yīng)的spec倉庫地址也能看到提交記錄。
這里說的添加到私有的Spec Repo,如果要添加到CocoaPods的官方庫,可以用到trunk工具。
使用終端 pod search pod_test
注意:這里搜索repos目錄,如果緩存里沒有該spec文件,則不會搜索到
-> pod_test (0.1.0)
私有庫項目demo
pod 'pod_test', '~> 0.1.0'
- Homepage: https://github.com/xxx/pod_test
- Source: https://github.com/xxx/pod_test.git
- Versions: 0.1.0 [mySpecs repo]
此時到~/.cocoapods/repos/mySpecs
如圖:
查看本地repo信息
$ pod repo
接下來我們再新建一個工程來測試一下
新建一個工程,在項目的podfile里添加
#私有spec倉庫的地址,而不是某個pod倉庫的地址
source 'https://github.com/xxx/mySpecs'
pod 'pod_test'
注意:如果該倉庫沒有權(quán)限,則會失敗
到此,算是完成pods私有庫的搭建了。
注:如果添加到CocoaPods官方庫上,別人都可以search或者install
cd 到pod_test目錄,執(zhí)行
pod trunk push pod_test.podspec
紅色警告:
[!] You need to register a session first.
$ pod trunk register xxx@163.com 'xxx' --description='pro'
再執(zhí)行
$pod trunk push pod_test.podspec
如果提示:
[!] You (xxx@163.com) are not allowed to push new versions for this pod. The owners of this pod are aaaa@126.com.
呵呵。大致的意思是repo 已經(jīng)被這個所有者占有了
$pod trunk info pod_test
終端打?。?
pod_test
- Versions:
- 0.0.2 (2017-01-13 07:03:27 UTC)
- Owners:
- aaa <aaa@126.com>
如果沒有問題,那么發(fā)布到Cocoapods后,在終端更新本地pods倉庫信息
$ pod setup
然后查詢
$ pod search pod_test