目錄
- 前言
- CocoaPods原理
- 發(fā)布與更新自己的pod
- 安裝CocoaPods
- 創(chuàng)建編輯自己的pod
- 本地初始化pod并提交到Github
- 注冊(cè)CocoaPods并提交pod
- 更新自己的pod
- 其他命令
前言
最近App在整理一些第三方庫(kù),包括一些沒(méi)怎么再維護(hù)的替換為自己實(shí)現(xiàn),所以做了一些簡(jiǎn)單的庫(kù)上傳到CocoaPods方便管理和維護(hù)。
CocoaPods原理
每種語(yǔ)言發(fā)展到一定階段,就會(huì)出現(xiàn)相應(yīng)的依賴管理工具,例如Node.js的npm、Swift的Swift Package Manager,早期為iOS提供依賴管理的工具叫做:CocoaPods。
特點(diǎn):
- CocoaPods會(huì)將所有的第三方庫(kù)以target的方式組成一個(gè)名為Pods的工程。整個(gè)第三方庫(kù)工程會(huì)生成一個(gè)庫(kù)提供給我們使用。
- 我們的工程和Pods工程會(huì)被以workspace的形式組織和管理,也就是*.xcworkspace文件。
好處:
- CocoaPods上有很多好用的庫(kù),以此縮短我們的開(kāi)發(fā)周期和提升軟件的質(zhì)量。
- CocoaPods會(huì)自動(dòng)將第三方開(kāi)源庫(kù)的源碼下載下來(lái),并且為工程設(shè)置好相應(yīng)的系統(tǒng)依賴和編譯參數(shù)。
- 通過(guò)Podfile可以很方便地更新第三方庫(kù)。
發(fā)布與更新自己的pod
下面以我提交的庫(kù)ZInAppPurchase為例,講解下整個(gè)過(guò)程和遇到的問(wèn)題。
參考官方文檔Making CocoaPods
安裝CocoaPods
CocoaPods是用Ruby實(shí)現(xiàn)的,而Gem是一個(gè)管理Ruby庫(kù)和程序的標(biāo)準(zhǔn)包,它通過(guò)Ruby Gem源來(lái)查找、安裝、升級(jí)和卸載軟件包。
- 更新Gem
如果沒(méi)有翻墻可能會(huì)比較慢,可以將原來(lái)的源替換成新的。
// 查看本機(jī)當(dāng)前設(shè)置的源
ZackZhengdeMacBook-Pro:~ zack$ gem source -l
*** CURRENT SOURCES ***
https://rubygems.org/
// 移除掉原有的源
ZackZhengdeMacBook-Pro:~ zack$ gem sources --remove https://rubygems.org/
// 添加國(guó)內(nèi)的源
ZackZhengdeMacBook-Pro:~ zack$ gem sources -a https://ruby.taobao.org/
淘寶的源已不再維護(hù),可以使用新的源,參考RubyGems 鏡像 - Ruby China
接下來(lái)更新Gem:
ZackZhengdeMacBook-Pro:~ zack$ sudo gem update --system
- 安裝并初始化CocoaPods
ZackZhengdeMacBook-Pro:~ zack$ sudo gem install cocoapods --pre
Password:
Successfully installed cocoapods-1.1.0.rc.3
Parsing documentation for cocoapods-1.1.0.rc.3
Done installing documentation for cocoapods after 2 seconds
1 gem installed
使用 --pre 是為了盡量使用最新的版本,沒(méi)更新時(shí)遇到過(guò)pod lib lint
發(fā)生下面錯(cuò)誤的情況:
xcodebuild: error: 'App.xcworkspace' does not exist.
初始化:
ZackZhengdeMacBook-Pro:~ zack$ pod setup
Setting up CocoaPods master repo
$ /usr/bin/git -C /Users/zack/.cocoapods/repos/master pull --ff-only
From https://github.com/CocoaPods/Specs
cae692c..3231da7 master -> origin/master
Updating cae692c..3231da7
Fast-forward
......
Setup completed
所有項(xiàng)目的Podspec文件都托管在 https://github.com/CocoaPods/Specs 第一次執(zhí)行 pod setup時(shí),CocoaPods會(huì)將這些podspec索引文件更新到本地的 ~/.cocoapods/目錄下,這個(gè)索引文件比較大。網(wǎng)上有講解更換repo鏡像的方法。
創(chuàng)建編輯自己的pod
使用pod lib
的命令。
ZackZhengdeMacBook-Pro:~ zack$ pod lib create ZInAppPurchase
Cloning `https://github.com/CocoaPods/pod-template.git` into `ZInAppPurchase`.
Configuring ZInAppPurchase 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 ]
> Swift
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Quick / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `ZInAppPurchase` from `../`
Downloading dependencies
Installing ZInAppPurchase (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `ZInAppPurchase.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'ZInAppPurchase/Example/ZInAppPurchase.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`.
修改文件:
/Users/zack/ZInAppPurchase/ZInAppPurchase/Classes/ReplaceMe.swift
/Users/zack/ZInAppPurchase/ZInAppPurchase.podspec
.podspec文件主要是修改s.homepage、s.source、s.frameworks*。
ZInAppPurchase.podspec內(nèi)容如下:
Pod::Spec.new do |s|
s.name = 'ZInAppPurchase'
s.version = '0.1.1'
s.summary = 'A short description of ZInAppPurchase.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/sapphirezzz/ZInAppPurchase'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'sapphirezzz' => 'zhengzuanzhe@gmail.com' }
s.source = { :git => 'https://github.com/sapphirezzz/ZInAppPurchase.git', :tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.source_files = 'ZInAppPurchase/Classes/**/*'
s.frameworks = 'Foundation', 'StoreKit'
end
安裝pod:
ZackZhengdeMacBook-Pro:~ zack$ cd ZInAppPurchase/Example/
ZackZhengdeMacBook-Pro:Example zack$ pod install
Analyzing dependencies
Fetching podspec for `ZInAppPurchase` from `../`
Downloading dependencies
Installing ZInAppPurchase 0.1.0 (was 0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
此時(shí)會(huì)看到工程目錄下多出ZInAppPurchase.xcworkspace、Podfile.lock文件和Pods目錄。
接下來(lái)可以打開(kāi)下面文件編寫(xiě)庫(kù):
/Users/zack/ZInAppPurchase/Example/ZInAppPurchase.xcworkspace
本地初始化pod并提交到Github
先到Github建立倉(cāng)庫(kù)ZInAppPurchase,并提交代碼:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git add -A && git commit -m "Release 0.1.0"
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git tag '0.1.1'
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git remote add origin git@github.com:sapphirezzz/ZInAppPurchase.git
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push --tags
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push origin master
初始化pod:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod lib lint --allow-warnings
-> ZInAppPurchase (0.1.0)
- WARN | summary: The summary is not meaningful.
ZInAppPurchase passed validation.
使用--allow-warnings是使得有warning時(shí)也能驗(yàn)證通過(guò)。比如:WARN | summary: The summary is not meaningful.
注冊(cè)CocoaPods并提交pod
先注冊(cè):
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk register zhengzuanzhe@gmail.com 'zackzheng' --description='macbook pro'
[!] Please verify the session by clicking the link in the verification email that has been sent to zhengzuanzhe@gmail.com
然后打開(kāi)收到的標(biāo)題為[CocoaPods] Confirm your session.的郵件,點(diǎn)擊鏈接之后跳轉(zhuǎn)頁(yè)面,會(huì)看到:
You can go back to your terminal now.
最后就deploy了:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk push ZInAppPurchase.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
-> ZInAppPurchase (0.1.0)
- WARN | summary: The summary is not meaningful.
Updating spec repo `master`
- Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/ae961ca47afb2a9171653382214897c4e160a679/Specs/ZInAppPurchase/0.1.0/ZInAppPurchase.podspec.json
- Log messages:
- October 13th, 11:28: Push for `ZInAppPurchase 0.1.0' initiated.
- October 13th, 11:28: Push for `ZInAppPurchase 0.1.0' has been pushed
(1.423909231 s).
看看庫(kù)是否已經(jīng)可以找到:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod search ZInAppPurchase
-> ZInAppPurchase (0.1.0)
A short description of ZInAppPurchase.
pod 'ZInAppPurchase', '~> 0.1.0'
- Homepage: https://github.com/sapphirezzz/ZInAppPurchase
- Source: https://github.com/sapphirezzz/ZInAppPurchase.git
- Versions: 0.1.0 [master repo]
(END)
組合鍵shift+Q退出
更新自己的pod
修改完代碼之后,提交到Github并push到CocoaPods:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git add -A && git commit -m
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git tag '0.1.1'
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push --tags
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk push ZInAppPurchase.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
-> ZInAppPurchase (0.1.1)
- WARN | summary: The summary is not meaningful.
Updating spec repo `master`
- Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/1fba9c650a4212c0b7be707698b8597556f0ece7/Specs/ZInAppPurchase/0.1.1/ZInAppPurchase.podspec.json
- Log messages:
- October 13th, 12:42: Push for `ZInAppPurchase 0.1.1' initiated.
- October 13th, 12:43: Push for `ZInAppPurchase 0.1.1' has been pushed
(1.090057228 s).
其他命令
查看CocoaPods上的個(gè)人信息:
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk me
- Name: Zackzheng
- Email: zhengzuanzhe@gmail.com
- Since: May 30th, 21:28
- Pods:
- ZInAppPurchase
- ZPlaceholderTextView
- ZPopTipView
- Sessions:
- May 30th, 21:28 - October 6th, 00:10. IP: 14.18.48.167
- September 28th, 02:06 - February 18th, 2017 10:43. IP: 121.33.185.113
Description: macbook pro
- October 13th, 11:24 - February 18th, 2017 11:31. IP: 183.240.19.242
Description: macbook pro
有時(shí)候如果提交有問(wèn)題,可以執(zhí)行一下命令刪除:
// 直接廢去這個(gè)pod
pod trunk deprecate ZInAppPurchase
// 廢去這個(gè)pod的某個(gè)版本
pod trunk delete ZInAppPurchase 1.0.0
測(cè)試過(guò),兩個(gè)命令使用完都沒(méi)返回成功,且pod search是可以找到的,但實(shí)際卻是成功的。