手动组件化基本步骤
一、远程索引库
- 什么是远程索引库
每创建一个组件都会带有一个 xxx.podspec 的索引文件。专门用来存放这些索引文件的库就叫做索引库。我们需要将这些索引文件上传到远程索引库才能保证其他的同事能够拿来用。
-
创建远程索引库
在 git 代码仓库中创建一个所以库:
image -
在 git 中本地添加索引库地址
利用
pod repo
来查看当前的索引库,然后pod repo add SampleSpecs https://XXXX/XXX/SampleSpecs.git
命令添加新创建的索引库地址image如果使用自己搭建的 Git 服务器,因为证书是自签的,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
二、创建私有 Framework
-
在 git 代码仓库中创建一个代码仓库image
通过 Xcode 创建一个框架工程,将需要编译的代码添加到工程中,保证编译通过,将需要暴露的头文件在 Xcode 中设为 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
设置为Legacy Build System
image 创建 PodSpec 文件,这个文件是这个 Framework 在索引库的 “身份证”,其中主要的内容如下:
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" }
// 支持的平台 和 系统版本
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
要填创建索引库的代码仓库中的地址:
[图片上传失败...(image-77b457-1551403437812)]
-
本地对 podSpec 文件进行校验
在终端中,输入:
pod spec lint XXX.podspec --verbose --allow-warnings
来直接进行线上 podSpec 文件进行校验,如果有错误主要会出现在 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 中使用,需要保证编译通过。