Cocoapods私有庫的OC版本網上已經有不少介紹了,也介紹得很好。可是發現對Swift版的介紹得很少,雖然二者差距不大,但總歸仍是有不一致的地方。今天就和小夥伴們一塊兒來了解如何製做一個Swift版的私有庫。html
建立私有庫以前,咱們先看看公有庫。在Finder中打開: ~/.cocoapods/repos。能夠看到目錄下有 master 文件夾,這就是公有庫的git倉庫。ios
1.1 建立私有git倉庫做爲私有的Repo 這裏我使用github建立遠程倉庫,小夥伴可執行選擇本身的遠程倉庫。 git
按上圖操做,完成遠程倉庫【BOTestSpec】的建立。github
1.2 執行repo 命令添加私有庫Repo 打開終端,在任意目錄下執行下面的命令:swift
pod repo add BOTestSpec https://github.com/Lwindy/BOTestSpec.git
複製代碼
解釋:
pod repo add 【私有庫名稱】【1.1中建立的遠程倉庫的git地址】
複製代碼
再次打開 ~/.cocoapods/repos,若是能看到多一個 BOTestSpec 文件中,則說明建立成功。 ruby
私有Spec建立完成了,可是裏面並無內容,因此咱們還須要添加組件庫到私有庫裏,豐富咱們的私有庫。bash
pod 建立組件庫的命令使用能夠查看官方文檔:Using Pod Lib Create。ide
cd到你要保存項目的目錄而後執行下面的命令:函數
pod lib create BOTestTools
複製代碼
緊接着,會有一些參數須要配置: 測試
配置完成後,會自動打開建立的項目。
若是你的選擇和上圖的不一致,可能會生成不一樣的項目。
按上圖建立【BOTestTools】倉庫,存放組件項目。
遠程倉庫建立好後,clone到本地,存放在你想要保存的目錄。
將這四個文件拷貝到clone下面的文件夾下。
打開 BOTestTools本地倉庫中的 Example 中的工程。
選擇 BOTestTools.podspec 文件:
相關字段能夠查詢官方文檔【Podspec Syntax Reference】。
這是我項目中podspec文件,小夥伴們能夠做爲參考。
Pod::Spec.new do |s|
s.name = 'BOTestTools'
s.version = '0.1.0'
s.summary = 'A short description of BOTestTools.'
s.description = <<-DESC TODO: Add long description of the pod here. DESC
s.homepage = 'https://github.com/LWindy/BOTestTools'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'LWindy' => 'windy.lin@163.com' }
s.source = { :git => 'https://github.com/LWindy/BOTestTools.git', :tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.source_files = 'BOTestTools/Classes/**/*'
end
複製代碼
添加文件 Tools.swift 到 BOTestTools文件夾下的Classes文件夾中。
Tools.swift 文件中簡單的給 UIView 添加一個分類。能夠設置 UIView 的 corner 和 border。
爲了方便鏈式調用,函數返回值都是Self。並且爲了能訪問函數,須要給函數加public。
向庫中添加文件或者修改文件,都須要執行
pod update
才能夠在 Example 工程中使用。
在 Main.storyboard 中給 View 添加一個 按鈕。
再在 ViewController.swift 中使用 import BOTestTools
導入私有庫 BOTestTools。
在 viewDidLoad
方法中,設置 按鈕btn 的圓角和邊框。
運行結果:
假如要向私有庫中添加一張圖片,須要使用 Assets來保存資源。
在Assets目錄下添加 ToolsAsset.xcassets,用來存放圖片。
如上圖所示,添加一張測試圖片進入Assets中。
同時,因爲添加了資源,因此須要更新 podspec 文件,增長 s.resource_bundles 字段。
Pod::Spec.new do |s|
s.name = 'BOTestTools'
s.version = '0.1.0'
s.summary = 'A short description of BOTestTools.'
s.description = <<-DESC TODO: Add long description of the pod here. DESC
s.homepage = 'https://github.com/LWindy/BOTestTools'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'LWindy' => 'windy.lin@163.com' }
s.source = { :git => 'https://github.com/LWindy/BOTestTools.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'BOTestTools/Classes/**/*'
s.resource_bundles = {
'BOTestTools' => ['BOTestTools/Assets/*.xcassets']
}
end
複製代碼
BOTestTools/Assets/*.xcassets
爲圖片資源的路徑,須要和你存放圖片的路徑保持一致。
因爲私有庫中的Bundle內的資源沒法直接訪問,因此添加 BOTools
類來獲取資源。 向 Classes 目錄下添加 BOBundleTool.swift 文件。
import UIKit
public class BOTools {
static var bundle: Bundle = {
let bundle = Bundle.init(path: Bundle.init(for: BOTools.self).path(forResource: "BOTestTools", ofType: "bundle", inDirectory: nil)!)
return bundle!
}()
public static func getBundleImg(with name: String) -> UIImage? {
var image = UIImage(named: name, in: bundle, compatibleWith: nil)
if image == nil {
image = UIImage(named: name)
}
return image
}
}
複製代碼
BOTools 類也須要添加 public 關鍵字,不然外界沒法訪問。
其中須要特別注意的是bundle路徑的獲取。
Bundle.init(for: BOTools.self).path(forResource: "BOTestTools", ofType: "bundle", inDirectory: nil)
複製代碼
forResource參數:必須和組件庫的名稱保持一致。
複製代碼
添加圖片完成後,必定要執行 pod update
才能夠在 Example 工程中訪問。
在視圖上添加 UIImageView,同時給按鈕綁定點擊事件。實現點擊按鈕,加載圖片。
加載圖片的代碼以下:
let img = BOTools.getBundleImg(with: "1")
imgView.image = img
複製代碼
運行結果以下:
ps:若是圖片沒法加載或者報錯,能夠 clean 一下項目,而後重試。
實現到這一步,基本上一個組件庫已經制做完成了。可是如今還只是在本地,只能本身使用。若是和你一塊兒協同開發的小夥伴也要使用你的庫,那麼你還須要將組件庫上傳至git。
在上傳以前,還須要驗證 podspec 是否正確。不然別人是沒法使用的。
cd 到 BOTestTools 文件夾下。其目錄下有 BOTestTools.podspec 文件。
在執行以下命令:
pod lib lint --allow-warnings
複製代碼
執行結果以下:
若是出現:BOTestTools passed validation. 那麼說明本地校驗經過。
可是會發現有兩個WARN警告⚠️,小夥伴可能也會遇到,那麼咱們來看看這兩個警告是什麼意思。
summary: The summary is not meaningful.
這是由於你沒有修改 .podspec 文件中的 s.summary
字段。
只須要修改 .podspec 文件便可。以下:
s.summary = '這是一個測試組件庫'
複製代碼
[iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified.
這是由於咱們尚未指定當前組件庫中Swift的使用版本。
在當前目錄下執行以下命令,指定Swift版本爲4.0:
echo "4.0" > .swift-version
複製代碼
解決完上面兩個WARN以後,再執行pod lib lint --allow-warnings
命令就會發現沒有警告了。
將代碼提交到git倉庫: 一、執行 git add . 二、執行 git commit -m 'first commit',注意寫好註釋 三、執行 git push origin master 將代碼提交到遠程倉庫
如今你能夠在遠程倉庫中看到你提交的代碼了。
僅將代碼提交到git倉庫還不夠,還須要打上tag。而且該tag須要和 .podspec 文件中的版本 s.version = '0.1.0'
保持一致。
四、執行 git tag 0.1.0 打好tag 五、執行 git push --tags 將tag推送到git倉庫
在上一步上將組件庫提交到git倉庫後,你的小夥伴仍是沒法使用。還須要將 .podspec 文件提交到 BOTestSpec git庫中。
執行以下命令:
pod repo push BOTestSpec BOTestTools.podspec --allow-warnings
複製代碼
pod repo push 【私有庫名稱】 【podspec文件名】 --allow-warnings
複製代碼
結果以下:
則說明,BOTestSpec庫上傳成功。
同時,你還能夠查看 ~/.cocoapods/repos。在 BOTestSpec 倉庫下會多新增 BOTestTools 文件夾。以下所示:
**至此,你的私有庫已製做完成。**你和你的小夥伴們能夠在項目中使用它了。
建立 【BOSpecDemo】 測試項目。
在項目目錄下,執行命令:
pod init
複製代碼
編輯 podfile 文件:
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git' # 官方庫
source 'https://github.com/Lwindy/BOTestSpec.git' # 私有庫Repo地址
target 'BOSpecDemo' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for BOSpecDemo
pod 'BOTestTools'
end
複製代碼
注意:私有倉庫的地址必定是Spec Repo 的地址,不要錯誤的使用BOTestTools組件的git倉庫。
而後執行 pod install
,添加私有庫到工程中。
效果以下:
總結:私有庫的製做不難,只是步驟比較繁瑣。上述6步是我在實際使用中總結的步驟順序,雖然不甚精簡,但勝在穩定,基本能夠一次性成功。
易錯點: 一、混淆私有庫repo倉庫與組件倉庫,之間的區別能夠查看官方文檔。 二、組件庫製做完成後,要先push到git倉庫再將.podspec push到repo倉庫。 三、組件庫必定要打tag標籤。而且tag要和版本號一致。 四、.podspec 文件中的 source_files 和 resource_bundles 路徑必定要正確。