手動組件化基本步驟
一、遠程索引庫
- 什么是遠程索引庫
每創(chuàng)建一個組件都會帶有一個 xxx.podspec 的索引文件。專門用來存放這些索引文件的庫就叫做索引庫。我們需要將這些索引文件上傳到遠程索引庫才能保證其他的同事能夠拿來用。
-
創(chuàng)建遠程索引庫
在 git 代碼倉庫中創(chuàng)建一個所以庫:
image -
在 git 中本地添加索引庫地址
利用
pod repo來查看當前的索引庫,然后pod repo add SampleSpecs https://XXXX/XXX/SampleSpecs.git命令添加新創(chuàng)建的索引庫地址image如果使用自己搭建的 Git 服務(wù)器,因為證書是自簽的,Git 驗證后會拒絕,可能會遇到如下問題:
fatal: unable to access 'https://XXXXX/XX/SampleSpecs.git/': SSL certificate problem: self signed certificate這里可以用如下命令臨時禁用驗證方法來解決:
git -c http.sslVerify=false clone https://XXXXX/XX/SampleSpecs.git或者直接徹底禁用:
git config --global http.sslVerify false
二、創(chuàng)建私有 Framework
-
在 git 代碼倉庫中創(chuàng)建一個代碼倉庫image
通過 Xcode 創(chuàng)建一個框架工程,將需要編譯的代碼添加到工程中,保證編譯通過,將需要暴露的頭文件在 Xcode 中設(shè)為 public
-
編譯融合編譯器和真機的 Framework:
示例合并打包腳本:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">FMK_NAME=
{SRCROOT}/Products/
{WRK_DIR}/Release-iphoneos/
{WRK_DIR}/Release-iphonesimulator/
{FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "{INSTALL_DIR}" ]
then
rm -rf "{INSTALL_DIR}"
cp -R"{INSTALL_DIR}/"
lipo -create "{FMK_NAME}" "
{FMK_NAME}" -output "
{FMK_NAME}"
rm -r "{INSTALL_DIR}"</pre>
在使用腳本進行合并打包的時候,如果 Xcode 10 需要在
File->Project Settings中對Build System設(shè)置為Legacy Build Systemimage 創(chuàng)建 PodSpec 文件,這個文件是這個 Framework 在索引庫的 “身份證”,其中主要的內(nèi)容如下:
framework podSpec 文件樣例
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;">Pod::Spec.new do |s|
s.platform = :ios
s.ios.deployment_target = '8.0'
s.name = 'PrivateFrameworkTest'
s.version = '1.0.0'
s.summary = 'A short description of PrivateFrameworkTest.'
s.requires_arc = true
s.homepage = 'https://XXXX/XXX/PrivateFrameworkTest.git'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Chinastock' => XXX' }
s.source = { :git => 'https://XXXX/XXX/PrivateFrameworkTest.git', :tag => '1.0.0' }
?
s.ios.deployment_target = '8.0'
?
Framework 的頭文件地址
s.source_files = 'XXX/PrivateFrameworkTest.framework/Headers/*.{h}'
Framework 文件地址
s.vendored_frameworks = 'XXX/PrivateFrameworkTest.framework'
項目頭文件地址
s.public_header_files = 'XXX/PrivateFrameworkTest.framework/Headers/PrivateFrameworkTest.h'
end</pre>
podSpec 文件樣例
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;">Pod::Spec.new do |s|
// 工程名
s.name = "PrivatePodTest"
// 版本(需要和 Git 的 tag 保持一致)
s.version = "0.0.1"
// 工程的概述
s.summary = "Private Pod Test."
// 主頁
s.homepage = "https://xx/PrivatePodTest.git"
// 填寫 Git 組件地址
?
s.license = { :type => "MIT", :file => "LICENSE" }
?
// 作者
s.author = { "xxxx" => "xxxx@xxx.com" }
// 支持的平臺 和 系統(tǒng)版本
s.platform = :ios, "8.0"
// 倉庫的地址 和 tag(和version相同)
s.source = { :git => "git@git.coding.net:xxxx/SampleFramework.git", :tag => "#{s.version}" }
// 工程的源文件
s.source_files = "Source/.{h,m}", 'FrameworkPath/.{framework}'
// iOS ARC要求
s.requires_arc = true
// 該工程依賴了哪些cocoapods庫(未使用,此項注釋)
?
// 依賴的第三方框架
s.dependency "AFNetworking", "~> 0.1"
end</pre>
其中,s.source 要填創(chuàng)建索引庫的代碼倉庫中的地址:
[圖片上傳失敗...(image-77b457-1551403437812)]
-
本地對 podSpec 文件進行校驗
在終端中,輸入:
pod spec lint XXX.podspec --verbose --allow-warnings來直接進行線上 podSpec 文件進行校驗,如果有錯誤主要會出現(xiàn)在 podSpec 文件中。
[圖片上傳失敗...(image-5e99e2-1551403437810)]
這里常見錯誤
file patterns: The XXX pattern did not match any file需要檢查一下 podSpec 文件中引用 Git 倉庫中對應的版本中相應的路徑是否有正確的文件。
-
將校驗通過的Framework 推送到代碼索引庫
pod repo push XXXSpecs XXX.podspec --allow-warnings
三、如何使用 Framework
-
在基于 Cocoapods 的工程中使用,在 Podfile 頭部中索引庫的引用添加:
source 'https://XXXX/XXX/SampleSpecs.git'然后用正常的語法進行引用:
pod 'SampleFramewokr'~>'0.1'就可以使用 Framework 了。
在其他 Framework 中使用,在 podSpecs 文件中添加索引庫的引用,并且在
s.dependency中添加引用庫的名字;在其他 Framework 中使用,需要保證編譯通過。



