前言
使用cocoapods管理iOS開發(fā)所使用的第三方庫(kù)已經(jīng)是一個(gè)非常常見的操作。同樣的,利用cocoapods實(shí)現(xiàn)組件化開發(fā),對(duì)于大型項(xiàng)目的合理分工,版本控制,提高編譯效率具有非常明顯的作用。這里我對(duì)本地私有庫(kù)和遠(yuǎn)程私有庫(kù)的創(chuàng)建進(jìn)行一個(gè)總結(jié),而這是組件化開發(fā)的基礎(chǔ)。
一.準(zhǔn)備
1.創(chuàng)建工程文件

2.生成pod庫(kù)配置文件

bogon:XTProtocolManager xiaotei$ pod spec create XTProtocolManager
當(dāng)然,你也可以手動(dòng)創(chuàng)建一個(gè)podspec文件,將必要內(nèi)容粘貼進(jìn)去
Pod::Spec.new do |s|
s.name = "XTProtocolManager"
s.version = "0.0.1"
s.ios.deployment_target = '7.0'
s.summary = "XTProtocolManager是一個(gè)iOS組件化開發(fā)的組件之一,主要用來管理模塊跳轉(zhuǎn)"
s.homepage = "https://github.com/dingpuyu/XTProtocolManager.git"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "dingpuyu" => "ding13525163308@163.com" }
s.social_media_url = "http://twitter.com/dingpuyu"
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
s.source_files = "XTProtocolManager/XTProtocolManager/*.{h,m}"
s.requires_arc = true
end
s.name:名稱,pod search 搜索的關(guān)鍵詞,注意這里一定要和.podspec的名稱一樣,否則報(bào)錯(cuò)
s.version:版本號(hào)
s.ios.deployment_target:支持的pod最低版本
s.summary: 簡(jiǎn)介
s.homepage:項(xiàng)目主頁(yè)地址
s.license:許可證
s.author:作者
s.social_media_url:社交網(wǎng)址,這里我寫的Twitter,如果你寫Twitter的話,你的podspec發(fā)布成功后會(huì)@你
s.source:項(xiàng)目的地址
s.source_files:需要包含的源文件
s.resources: 資源文件
s.requires_arc: 是否支持ARC
s.dependency:依賴庫(kù),不能依賴未發(fā)布的庫(kù)
s.dependency:依賴庫(kù),如有多個(gè)可以這樣寫
3.編輯podspec文件
二.本地私有庫(kù)
1.為庫(kù)工程創(chuàng)建本地git倉(cāng)庫(kù)
1.進(jìn)入庫(kù)工程文件夾

2.git初始化

3.查看文件狀態(tài),可以發(fā)現(xiàn)文件都還沒有添加到本地庫(kù)中

4.添加文件到緩沖區(qū)

5.從緩沖區(qū)提交文件到本地代碼倉(cāng)庫(kù)

6.打標(biāo)簽查看標(biāo)簽及刪除標(biāo)簽的命令,這里我們打上0.0.1的標(biāo)簽

完成這些操作,就可以去編輯podspec文件的source和source_file了
7.編輯podspec
工程的目錄結(jié)構(gòu)是這樣的

此時(shí)配置podspec文件的source和source_file如下
s.source = { :git => "", :tag => s.version }
s.source_files = "XTProtocolManager/XTProtocolManager/*.{h,m}"
2.創(chuàng)建并編輯Podfile

使用終端進(jìn)入工程主目錄如圖2.1
1.執(zhí)行命令$pod init
成功則可以在當(dāng)前目錄下看到Podfile文件
2.使用vim命令進(jìn)行編輯
target 'XTComponentBase' do
pod 'XTProtocolManager', :path=>'../XTProtocolManager/XTProtocolManager.podspec'
end
此時(shí)如果直接pod install的話,會(huì)報(bào)錯(cuò)很多,我們來一個(gè)一個(gè)解決
[!] The `XTProtocolManager` pod failed to validate due to 3 errors.
[!] 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`:
- ERROR | license: Sample license type.
- WARN | homepage: The homepage has not been updated from default
- ERROR | source: The Git source still contains the example URL.
- WARN | summary: The summary is not meaningful.
- ERROR | description: The description is empty.
① 協(xié)議問題
將默認(rèn)的s.license修改為s.license = { :type => "MIT", :file => "LICENSE" }
創(chuàng)建協(xié)議很簡(jiǎn)單,一個(gè)名為L(zhǎng)ICENSE的空文件拷貝如下內(nèi)容,只需要將前邊的版權(quán)修改一下即可
MIT License
Copyright (c) 2017 dingpuyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
咱們程序員并不care 警告,但是改一改內(nèi)容就能很容易的解決警告。
接下來就把描述給補(bǔ)充一下,讓它能夠install通過
② s.description的修改

描述這里,要以這種格式寫,不會(huì)出錯(cuò)。
③ homepage一般寫的是git上的路徑,我這里用的就是github得鏈接
④ source 這個(gè)比較關(guān)鍵,一定是要可用的git路徑其中可以有四種設(shè)置方式
s.source = { :git => "./XTProtocomManager", :tag => s.version }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :commit => "881daa" }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => 0.0.1 }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
第一種是本地git倉(cāng)庫(kù)
commit => "68defea" 表示將這個(gè)Pod版本與Git倉(cāng)庫(kù)中某個(gè)commit綁定
tag => 1.0.0 表示將這個(gè)Pod版本與Git倉(cāng)庫(kù)中某個(gè)版本的comit綁定
tag => s.version 表示將這個(gè)Pod版本與Git倉(cāng)庫(kù)中相同版本的comit綁定
④ summary就是一段概要,根據(jù)項(xiàng)目作用寫一寫就好了。
3.pod install

如果順利的話,本地私有庫(kù)就已經(jīng)可以用了

三.創(chuàng)建遠(yuǎn)程私有庫(kù)
這里我們使用的是github作為遠(yuǎn)程倉(cāng)庫(kù),如果是私有項(xiàng)目的話就不要用github,可以自己搭建git或者使用coding
步驟如下:
1.spec repo創(chuàng)建
①創(chuàng)建一個(gè)podspec倉(cāng)庫(kù)

②關(guān)聯(lián)pod spec庫(kù),這里如果第一次做操作,需要輸入賬號(hào)密碼
bogon:XTComponentBase xiaotei$ pod repo add XTPrivateLib https://github.com/dingpuyu/XTPrivateLib.git
Cloning spec repo `XTPrivateLib` from `https://github.com/dingpuyu/XTPrivateLib.git`
③查看

2.組件代碼倉(cāng)庫(kù)創(chuàng)建
① 創(chuàng)建遠(yuǎn)程代碼倉(cāng)庫(kù)

② 添加倉(cāng)庫(kù)關(guān)聯(lián)及提交代碼到遠(yuǎn)程倉(cāng)庫(kù)
bogon:XTProtocolManager xiaotei$ git remote add origin https://github.com/dingpuyu/XTProtocolManager.git
將本地庫(kù)的代碼推到遠(yuǎn)程庫(kù)
bogon:XTProtocolManager xiaotei$ git push -f origin master
Counting objects: 36, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (36/36), 20.65 KiB | 0 bytes/s, done.
Total 36 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/dingpuyu/XTProtocolManager.git
+ 22c24f5...6b370f7 master -> master (forced update)
將本地創(chuàng)建的標(biāo)簽推到遠(yuǎn)程庫(kù)
bogon:XTProtocolManager xiaotei$ git push --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 180 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/dingpuyu/XTProtocolManager.git
* [new tag] 0.0.1 -> 0.0.1
刪除標(biāo)簽可以這樣來
bogon:XTProtocolManager xiaotei$ git push origin :0.0.1
To https://github.com/dingpuyu/XTProtocolManager.git
- [deleted] 0.0.1
git在使用過程中也會(huì)有各種各樣的問題,有遇到什么問題的,可以給我留言
3.向私有spec repo中提交podspec
① 在本地組件工程目錄編輯podspec文件,如果沒有創(chuàng)建,使用pod spec create XXX
此時(shí)的source應(yīng)該如下,tag就是剛剛設(shè)置0.0.1
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
②進(jìn)行驗(yàn)證

③上傳podspec
bogon:XTProtocolManager xiaotei$ pod repo push XTPrivateLib XTProtocolManager.podspec
Validating spec
-> XTProtocolManager (0.0.1)
Updating the `XTPrivateLib' repo
Already up-to-date.
Adding the spec to the `XTPrivateLib' repo
- [Add] XTProtocolManager (0.0.1)
Pushing the `XTPrivateLib' repo
To https://github.com/dingpuyu/XTPrivateLib.git
9610a4d..505f5a5 master -> master
4.如何使用呢?
① 查找其路徑
bogon:XTProtocolManager xiaotei$ pod search XTProtocolManager
-> XTProtocolManager (0.0.1)
這是一個(gè)組件管理工具 XTProtocolManager.
pod 'XTProtocolManager', '~> 0.0.1'
- Homepage: https://github.com/dingpuyu/XTProtocolManager
- Source: https://github.com/dingpuyu/XTProtocolManager.git
- Versions: 0.0.1 [XTPrivateLib repo]
② 修改Podfile文件
source 'https://github.com/dingpuyu/XTPrivateLib.git'
source 'https://github.com/CocoaPods/Specs.git' #官方倉(cāng)庫(kù)的地址
target 'XTComponentBase' do
pod 'XTProtocolManager'
end
③ 下載
bogon:XTComponentBase xiaotei$ pod install
Analyzing dependencies
Downloading dependencies
Installing XTProtocolManager (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
好了,到這里就結(jié)束了私有庫(kù)的創(chuàng)建,現(xiàn)在打開工程就可以看到自己的組件工程了
