一、前言
我們已經(jīng)學習了 CocoaPods 的使用和基本原理知識,我們在項目開發(fā)中,不可避免的要工程化與組件化,因此,需要開發(fā)自己的 pod 組件,本文將帶領大家如何創(chuàng)建自己的 pod 庫,并發(fā)布至 pod trunk。
二、自定義 pod 庫
2.1、創(chuàng)建 pod 庫
- 先初始化 Pod

- 然后再 pod install ,最終結(jié)果如下:

- 創(chuàng)建自定義 Pod 庫::
// 命令行
$ pod lib create XXX
整個創(chuàng)建過程如下圖:

2.2、pod template 分析

上圖標示的非常清楚了:
- 我們雖然是創(chuàng)建 pod lib,實現(xiàn)是下載 pod template;
- 根據(jù)我們的輸入選項,最終生成一個自帶『工程』+『Pod Library』的完整項目;
- 左側(cè) Xcode 目錄區(qū)就展示是『工程』如何引用『Pod lib』;
2.3、Example 的 Podfile 分析

一目了然,pod 只需要指明 path,如果不給 path,pod 會自動從 pod repo 中去尋找,這里,我們給定一個本地的 path,pod 就知道了;后面,我們會重新調(diào)整這個目錄結(jié)構。
2.4、podspec 文件
我們先來看一下 podspec 文件內(nèi)容

重點是1、2兩個區(qū)域,其它信息,都是根據(jù)你的 git 的 config 讀取的信息默認生成的,并不準確,后面上傳至 git 時,會修改這里,如果僅是本地使用,這可以無視。
三、調(diào)整目錄 & 引入項目
3.1、調(diào)整 pod lib 目錄
刪除如下,只留我們需要的文件和目錄:

3.2、修改 podspec
Pod::Spec.new do |s|
s.name = 'PodLogger'
s.version = '1.0.0'
s.summary = 'A pod library demo'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
# 先修改為之后要提交到 github 上的地址
s.homepage = 'https://github.com/qingye/PodLogger'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '青葉小小' => '24854015@qq.com' }
s.source = { :git => 'https://github.com/qingye/PodLogger.git', :tag => s.version.to_s }
s.ios.deployment_target = '10.0'
s.source_files = 'PodLogger/**/*'
end
3.3、本地工程引入

接下來,我們在主工程下 pod install --verbose 一下,結(jié)果如下圖:

3.4、實現(xiàn) Logger.swift
public struct Logger {
var DEBUG: Bool = false
public init(_ DEBUG: Bool) {
self.DEBUG = DEBUG
}
public func log(_ msg: String) {
print("Pod..Logger => \(msg)")
}
}
3.5、測試 pod lib

四、上云( github )
4.1、創(chuàng)建 github 倉庫

4.2、提交 & tag
操作步驟如下:
- git add .
- git commit -m “xxx"
$ git add .
$ git commit -m "pod lib for demo"
[master 7256090] pod lib for demo
19 files changed, 38 insertions(+), 1050 deletions(-)
delete mode 100644 Example/PodLogger.xcodeproj/project.pbxproj
delete mode 100644 Example/PodLogger.xcodeproj/project.xcworkspace/contents.xcworkspacedata
delete mode 100644 Example/PodLogger.xcodeproj/xcshareddata/xcschemes/PodLogger-Example.xcscheme
delete mode 100644 Example/PodLogger/AppDelegate.swift
delete mode 100644 Example/PodLogger/Base.lproj/LaunchScreen.xib
delete mode 100644 Example/PodLogger/Base.lproj/Main.storyboard
delete mode 100644 Example/PodLogger/Images.xcassets/AppIcon.appiconset/Contents.json
delete mode 100644 Example/PodLogger/Info.plist
delete mode 100644 Example/PodLogger/ViewController.swift
delete mode 100644 Example/Podfile
delete mode 100644 Example/Tests/Info.plist
delete mode 100644 Example/Tests/Tests.swift
rewrite PodLogger.podspec (73%)
delete mode 100644 PodLogger/Assets/.gitkeep
delete mode 100644 PodLogger/Classes/.gitkeep
delete mode 100644 PodLogger/Classes/ReplaceMe.swift
create mode 100644 PodLogger/Logger.swift
delete mode 120000 _Pods.xcodeproj
- git remote add origin 遠程代碼倉庫地址
- git push origin master
$ git remote add origin https://github.com/qingye/PodLogger.git
$ git push origin master
Counting objects: 40, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (40/40), 14.39 KiB | 2.05 MiB/s, done.
Total 40 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/qingye/PodLogger/pull/new/master
remote:
To https://github.com/qingye/PodLogger.git
* [new branch] master -> master
- git tag 版本號 (注:這里的版本號必須和podspec里寫的版本號一致)
- git push --tags
$ git tag 1.0.0
$ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/qingye/PodLogger.git
* [new tag] 1.0.0 -> 1.0.0
4.3、驗證 & 提交至 pod
- 驗證:pod spec lint --verbose --allow-warnings

- 如果還沒有注冊過 pod trunk,可以如下注冊
$ pod trunk register 24854015@qq.com '青葉' --description='Personal PodLib'
[!] Please verify the session by clicking the link in the verification email that has been sent to 24854015@qq.com
- 郵箱驗證

點擊驗證鏈接:

- 命令行查看 pod 賬戶
$ pod trunk me
- Name: 青葉
- Email: 24854015@qq.com
- Since: March 2nd, 00:58
- Pods: None
- Sessions:
- March 2nd, 00:58 - July 8th, 01:05. IP: 203.156.215.154 Description: Personal PodLib
- 提交索引至遠程倉庫
根據(jù) CocoaPods 官方描述:
公有庫發(fā)布(全體可見):pod trunk push [NAME.podspec]
私有庫發(fā)布(自己可見):pod repo push REPO [NAME.podspec]
如果報如下錯誤,就是沒有注冊,請回看第2步:

- 公庫提交
pod trunk push PodLogger.podspec --verbose --allow-warnings
- 私庫提交
pod repo push PodLogger PodLogger.podspec --verbose --allow-warnings
我這里是用『公有庫』提交的,提交完結(jié)果如下:

我們可以去 CocoaPods/Specs 中去查看,已經(jīng)被索引了:

4.4、更新本地 pod repo
如果此時,你直接使用 pod search 會發(fā)現(xiàn)找不到自己的庫:
$ pod search PodLogger
雖然我們提交至 pod trunk(即 pod specs)成功,但是我們本地還沒有,所以,我們需要先更新一下本地的 pod 倉庫:
$ pod repo update
Updating spec repo `master`
$ /usr/local/bin/git -C /Users/chris/.cocoapods/repos/master fetch origin --progress
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 5), reused 0 (delta 0), pack-reused 0
From https://github.com/CocoaPods/Specs
ab416c347b51..3bd397424c41 master -> origin/master
$ /usr/local/bin/git -C /Users/chris/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
master
$ /usr/local/bin/git -C /Users/chris/.cocoapods/repos/master reset --hard origin/master
HEAD is now at 3bd397424c41 [Add] AudioLibrary 0.7
Updating spec repo `trunk`
如果本地已有(默認如下路徑):
~/.cocoapods/repos
但還是搜索不到,那可能需要重建搜索索引(search_index.json),路徑在:
~/Library/Caches/CocoaPods
刪除該索引文件,然后再執(zhí)行 search,如下(會提示創(chuàng)建索引中....完成后,就有了):
$ pod search PodLogger
Creating search index for spec repo 'master'.. Done!
-> PodLogger (1.0.0)
A pod library demo
pod 'PodLogger', '~> 1.0.0'
- Homepage: https://github.com/qingye/PodLogger
- Source: https://github.com/qingye/PodLogger.git
- Versions: 1.0.0 [master repo]
4.5、修改主工程
# Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
target 'PodDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# 默認使用最新的版本,即 1.0.0
pod 'PodLogger'
end
然后 pod install --verbose
$ pod install --verbose
Analyzing dependencies
Downloading dependencies
Installing PodLogger (1.0.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
重新編譯運行工程,結(jié)果如下:

注意:我們的 pod 庫發(fā)布后,再引入,pod lib 將不再放在『Development Pods』下面,還是在『Pods』下面了!
至此,所有與 CocoaPods 相關的知識我們就分享在這,雖然還有很多其它的知識點,但核心點主要是這些!