本教程基于 OSX 10.11.6 Pod 1.1.1 版本,邊操作邊寫的教程,中途難免疏漏,有錯(cuò)請(qǐng)留言。
概述
1.創(chuàng)建兩個(gè)Git倉(cāng)庫(kù),一個(gè)保存podspce,一個(gè)保存公共組件代碼
2.在~/.cocoapods/repos 下創(chuàng)建私有pod spec
3.測(cè)試公共組件代碼,打tag提交遠(yuǎn)程倉(cāng)庫(kù)
4.編輯pod spec文件,測(cè)試文件是否可用
5.向私有Spec Repo 提交pod spec
6.個(gè)人項(xiàng)目中使用制作好的Pod
初次使用CocoaPods請(qǐng)參照 安裝CocoaPods
建立兩個(gè)Git倉(cāng)庫(kù)
SpecRepo Spec Pods倉(cāng)庫(kù) https://github.com/LengYi/SpecRepo.git
DLKit 公共組件代碼倉(cāng)庫(kù) https://github.com/LengYi/DLKit.git
創(chuàng)建私有Spec Repo
Spec Repo:所有 Pods 的索引。
~/.cocoapods/repos 目錄下有個(gè)默認(rèn)master的官方 Spec Repo.
運(yùn)行以下命令,將會(huì)在 ~/.cocoapods/repos 目錄下多一個(gè)dlj目錄。dli 私有Pod的名稱,后一個(gè)參數(shù)你存儲(chǔ)該私有Pod的Git,就是上面創(chuàng)建的SpecRepo Git庫(kù)。
mac-mini:~ ice$ pod repo add dlj https://github.com/LengYi/SpecRepo.git
創(chuàng)建Pod項(xiàng)目工程文件
mac-mini:github ice$ pod lib create DLKit // 指定創(chuàng)建的目錄名稱
Cloning `https://github.com/CocoaPods/pod-template.git` into `DLKit`.
Configuring DLKit template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
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. )
What language do you want to use?? [ Swift / ObjC ]
> ObjC // 當(dāng)前Pod庫(kù)支持的語(yǔ)言類型
Would you like to include a demo application with your library? [ Yes / No ]
> YES // 是否包含一個(gè)例子工程
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> Specta // 選擇測(cè)試框架
Would you like to do view based testing? [ Yes / No ]
> Yes // 是否基于View測(cè)試
What is your class prefix?
> DL // 類的前綴
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `DLKit` from `../`
Downloading dependencies
Installing DLKit (0.1.0)
Installing Expecta (1.0.5)
Installing Expecta+Snapshots (3.1.1)
Installing FBSnapshotTestCase (2.1.4)
Installing Specta (1.0.6)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `DLKit.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'DLKit/Example/DLKit.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`.
運(yùn)行以上工程后會(huì)創(chuàng)建一個(gè)Xcode工程并會(huì)自動(dòng)打開(kāi),調(diào)整Xcode工程配置,運(yùn)行 OK。
創(chuàng)建的內(nèi)容如下:

檢查自動(dòng)生成的DLKit.podspec文件
mac-mini:DLKit ice$ pod lib lint
-> DLKit (0.1.0)
- WARN | url: The URL (https://github.com/707689817@qq.com/DLKit) is not reachable.
[!] DLKit did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it) and all results apply only to public specs, but you can use `--private` to ignore them if linting the specification for a private pod.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
You can use the `--no-clean` option to inspect any issue.
解決上述問(wèn)題:
mac-mini:DLKit ice$ echo "3.0" > .swift-version
繼續(xù)檢查DLKit.podspec直到顯示如下:
mac-mini:DLKit ice$ pod lib lint
-> DLKit (0.1.0)
DLKit passed validation.
編輯DLKit.podspec文件,修改檢查出現(xiàn)的錯(cuò)誤
原始內(nèi)容:
#
# Be sure to run `pod lib lint DLKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'DLKit'
s.version = '0.1.0'
s.summary = 'A short description of DLKit.' // 需要修改描述,否則會(huì)有一個(gè)警告
# 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
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/707689817@qq.com/DLKit' // 修改成公共組件代碼庫(kù)的主頁(yè),默認(rèn)生成的有錯(cuò),沒(méi)錯(cuò)就不用改了。
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '707689817@qq.com' => '707689817@qq.com' }
s.source = { :git => 'https://github.com/707689817@qq.com/DLKit.git', :tag => s.version.to_s } // 公共組件代碼庫(kù)的Git地址,Https 的地址,非SSH的地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'DLKit/Classes/**/*'
# s.resource_bundles = {
# 'DLKit' => ['DLKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
修改后:
#
# Be sure to run `pod lib lint DLKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'DLKit'
s.version = '0.1.0'
s.summary = '常用功能庫(kù)封裝'
# 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
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/LengYi/DLKit'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '707689817@qq.com' => '707689817@qq.com' }
s.source = { :git => 'https://github.com/LengYi/DLKit.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'DLKit/Classes/**/*'
# s.resource_bundles = {
# 'DLKit' => ['DLKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
檢查驗(yàn)證成功。
關(guān)聯(lián)DLKit 到遠(yuǎn)程公共組件Git 庫(kù)
默認(rèn)生成的項(xiàng)目就自帶Git,直接關(guān)聯(lián)到遠(yuǎn)程倉(cāng)庫(kù)即可
git add ./
git commit -m "注釋"
git remote add origin https://github.com/LengYi/DLKit.git
git push -u origin master
添加模塊組件目錄如下
mac-mini:DLKit ice$ tree Classes -L 5
Classes
└── Base
├── App
│ ├── DLAppInfo.h
│ └── DLAppInfo.m
├── DLNet
│ └── Request
│ ├── DLHttp.h
│ └── DLHttp.m
└── DLUIKitExtended
└── Device
├── UIDevice+extended.h
├── UIDevice+extended.m
├── UIDevice+name.h
└── UIDevice+name.m
修改DLKit.podspec如下
#
# Be sure to run `pod lib lint DLKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'DLKit'
s.version = '0.1.0'
s.summary = 'Http請(qǐng)求,獲取設(shè)備信息,獲取App信息'
# 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
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/LengYi/DLKit'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '707689817@qq.com' => '707689817@qq.com' }
s.source = { :git => 'https://github.com/LengYi/DLKit.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
#s.source_files = 'DLKit/Classes/**/*'
s.subspec 'DLHttp' do |http|
http.source_files = 'DLKit/Classes/Base/DLNet/Request/**/*'
end
s.subspec 'UIDevice+extended' do |dev|
dev.source_files = 'DLKit/Classes/Base/DLUIKitExtended/Device/**/*'
end
s.subspec 'DLAppInfo' do |info|
info.source_files = 'DLKit/Classes/Base/App/**/*'
end
# s.resource_bundles = {
# 'DLKit' => ['DLKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'UIKit', 'AdSupport','Foundation'
# s.dependency 'AFNetworking', '~> 2.3'
end
進(jìn)入Example 測(cè)試工程
pod update --no-repo-update
運(yùn)行測(cè)試工程測(cè)試剛添加的模塊,檢查是否能夠正常調(diào)用。
注意:每次向Pod添加了新的文件或者更新了 podspec文件內(nèi)容都需要重新執(zhí)行一遍 pod update --no-repo-update 命令。
檢查pod spec,直到?jīng)]有錯(cuò)誤,沒(méi)有警告
mac-mini:DLKit ice$ pod lib lint
給DLKit公共組件代碼模塊打發(fā)行版本號(hào)
mac-mini:DLKit ice$ git add ./
mac-mini:DLKit ice$ git commit -m "添加Http請(qǐng)求,獲取設(shè)備信息,獲取App信息模塊"
mac-mini:DLKit ice$ git tag -m "添加Http請(qǐng)求,獲取設(shè)備信息,獲取App信息模塊" 0.1.0 // 該版本號(hào)需跟podspec中的版本號(hào)一致
mac-mini:DLKit ice$ git push --tags // 將tag推送到遠(yuǎn)程服務(wù)器
向SpecRepo倉(cāng)庫(kù)提交pod spec
注意:如果碰到本地使用 pod lib lint 驗(yàn)證通過(guò),但是下面的操作失敗了,請(qǐng)重新生成新的tag版本號(hào)并推送到服務(wù)器,使用 pod repo push dlj DLKit.podspec --allow-warnings --verbose 查看具體原因
mac-mini:DLKit ice$ pod repo push dlj DLKit.podspec
Validating spec
-> DLKit (0.1.0)
Updating the `dlj' 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 `dlj' repo
- [Add] DLKit (0.1.0)
Pushing the `dlj' repo
To https://github.com/LengYi/SpecRepo.git
* [new branch] master -> master
之后到 ~/.cocoapods/repos/dlj 目錄查看
mac-mini:repos ice$ tree dlj -L 3
dlj
└── DLKit
└── 0.1.0
└── DLKit.podspec
以上步驟成功將能搜索到DLKit,結(jié)果如下
-> DLKit (0.1.0)
Http請(qǐng)求,獲取設(shè)備信息,獲取App信息
pod 'DLKit', '~> 0.1.0'
- Homepage: https://github.com/LengYi/DLKit
- Source: https://github.com/LengYi/DLKit.git
- Versions: 0.1.0 [dlj repo]
- Subspecs:
- DLKit/DLHttp (0.1.0)
- DLKit/UIDevice+extended (0.1.0)
- DLKit/DLAppInfo (0.1.0)
(END)
本地測(cè)試podspec文件
新建一個(gè)測(cè)試工程,添加 Podfile 文件
CocoaPodsDemo
├── CocoaPodsDemo
├── CocoaPodsDemo.xcodeproj
└── Podfile
直接使用本地DLKit庫(kù),一般用于測(cè)試podspec是否正常使用 Podfile寫法
platform :ios,'8.0'
target 'CocoaPodsDemo' do
pod 'DLKit',:path => '/Users/ice/Desktop/work/github/DLKit' // 組件代碼Git倉(cāng)庫(kù)文件路徑
end
沒(méi)有推送到CocoaPod 官方源正常使用方法
使用整個(gè)DLKit庫(kù)的Podfile
source 'https://github.com/CocoaPods/Specs.git' // 如果有使用到官方庫(kù)需添加本語(yǔ)句
source 'https://github.com/LengYi/SpecRepo.git' // pod spec對(duì)應(yīng)的Git庫(kù)非組件Git庫(kù)
platform :ios,'8.0'
target 'CocoaPodsDemo' do
pod 'DLKit/UIKit','0.1.0'
end
執(zhí)行以下命令,然后在工程中包含需要使用的對(duì)應(yīng)頭文件就可以使用封裝好的庫(kù)了。
pod update --no-repo-update
僅使用部分DLKit庫(kù)的Podfile
source 'https://github.com/CocoaPods/Specs.git' // 如果有使用到官方庫(kù)需添加本語(yǔ)句
source 'https://github.com/LengYi/SpecRepo.git' // pod spec對(duì)應(yīng)的Git庫(kù)非組件Git庫(kù)
platform :ios,'8.0'
target 'CocoaPodsDemo' do
pod 'DLKit/DLHttp', '~> 0.1.0'
pod 'DLKit/UIDevice+extended','~> 0.1.0'
end
如何刪除一個(gè)私有庫(kù)
mac-mini:~ ice$ pod repo remove dlj
以上為創(chuàng)建過(guò)程,如果想直接使用封裝好的Pod
1.首先本地創(chuàng)建私有Pod
mac-mini:~ ice$ pod repo add dlj https://github.com/LengYi/SpecRepo.git
2.具體工程Podfile參照上面寫法即可。
遇到的問(wèn)題及解決方法
1.pod lib lint執(zhí)行報(bào)錯(cuò)
- ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- ERROR | xcodebuild: ....
- error: include of non-modular header inside framework module '' [-Werror,-Wnon-modular-include-in-framework-module]
...
庫(kù)的某個(gè)頭文件中直接import了第三方庫(kù)(我對(duì)它有依賴)的頭文件,將它放到 .m中
2.pod lib lint驗(yàn)證成功了,但是pod repo push 的時(shí)候提示podspec無(wú)法通過(guò)驗(yàn)證
重新提交一個(gè)新的tag并push,再執(zhí)行pod repo push,驗(yàn)證通過(guò)。
3.pod search可以成功的搜索到自己的庫(kù),但是在項(xiàng)目中執(zhí)行pod install報(bào)錯(cuò)
在podfile 文件中添加
source 'https://github.com/CocoaPods/Specs.git' // 如果有使用到官方庫(kù)需添加本語(yǔ)句
source 'https://github.com/LengYi/SpecRepo.git' // pod spec對(duì)應(yīng)的Git庫(kù)非組件Git庫(kù)
4.添加新的類文件到庫(kù)后 pod lib lint 無(wú)法驗(yàn)證通過(guò)
-> DLKit (0.1.2)
- ERROR | [iOS] unknown: Encountered an unknown error (757: unexpected token at '2017-04-20 11:23:47.824 simctl[46081:1822584] CoreSimulator is attempting to unload a stale CoreSimulatorService job. Detected Xcode.app relocation or CoreSimulatorService version change. Framework path (/Applications/Xcode.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework) and version (201.3) does not match existing job path (/Users/ice/Desktop/Xcode.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc/Contents/MacOS/com.apple.CoreSimulator.CoreSimulatorService) and version (338.16).
2017-04-20 11:23:48.833 simctl[46081:1822584] Failed to locate a valid instance of CoreSimulatorService in the bootstrap. Adding it now.
') during validation.
[!] DLKit did not pass validation, due to 1 error.
You can use the `--no-clean` option to inspect any issue.
進(jìn)入Example 工程文件運(yùn)行
pod update --no-repo-update
再次驗(yàn)證即可通過(guò)
參考鏈接:
CocoaPods 官網(wǎng)