iOS组件化:从零开始搭设私有库

0、前言

本文主要介绍了iOS私有库的管理方式。在有多个产品线的情况下,其中业务层、自定义的控件、自定义的工具类,如日期处理类、日历、加密类等等,甚至包括App的基础框架都是可以考虑复用,这样一来有效的提高了编码的效率,让开发人员专注主业务的开发,不在为基础的框架、基础的类重复的“造轮子”。
私有库搭设的基本思路:
1、搭设一个脚手架
3、同步远程私有仓库
2、添加组件并同步远程仓库
4、迭代更新

1、搭设一个脚手架

1.1、新建文件夹
mkdir Module
cd Module
1.2、下载工程脚手架
pod lib create ZMinLib

ZMinLib是你要创建的组件工程的名称。安装过程中会提示你输入要下载工程的配置(如下:),依次输入:iOS、ObjC、Yes、Kiwi、Yes、ZMin,其中第二步如果想创建OC库,请输入ObjC。(各版本可能有不同,请根据提示输入)

chenzimin@rattanchen ~ % cd /Users/chenzimin/Desktop/Module 
chenzimin@rattanchen Module % pod lib create ZMinLib    
Cloning `https://github.com/CocoaPods/pod-template.git` into `ZMinLib`.
Configuring ZMinLib template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
2020-12-08 11:22:23.340 defaults[39142:356708] 
The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )
 Press return to continue.
What platform do you want to use?? [ iOS / macOS ]
 > iOS
What language do you want to use?? [ Swift / ObjC ]
 > ObjC
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > Kiwi
Would you like to do view based testing? [ Yes / No ]
 > Yes
What is your class prefix?
 > ZMin
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
Running pod install on your new library.
Analyzing dependencies
Downloading dependencies
Installing FBSnapshotTestCase (2.1.4)
Installing Kiwi (3.0.0)
Installing ZMinLib (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `ZMinLib.xcworkspace` for this project from now on.
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'ZMinLib/Example/ZMinLib.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.

创建成功会自动打开Xcode项目,形成的项目脚手架如下:
在这里插入图片描述

  • 注意:
  • 1、删除文件夹【ZMinLib】,新建【Products】,需要组件置入该文件夹,下面的配置文件路径也要跟着修改。如果用现有的文件路径,虽然构建成功,但是还是无法搜索到新文件。
  • 2、如果不删除【ZMinLib】,可将所需组件添加到【Classes】文件中,在索引库关联成功后,可以另建新的仓库存储索引库。可以参考如下案例:ZMSpecsZMVendersZMFoundation
  • 3、脚手架项目中以下文件可以删除:[_Pods.xcodeproj]、[.travis.yml]、[ZMinLib]。
1.3、配置文件的说明

配置文件就是工程根目录下的后缀为xxx.podspec的文件,在我的例子中就是ZMinLib.podspec文件。

打开这个文件,里面是工程的配置。我们在用pod命令安装库时,就是找到这个文件,获取地址下载库,并根据配置下载好依赖库和其它工程的配置。

Pod::Spec.new do |s|
  s.name             = 'ZMinLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of ZMinLib.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC
  s.homepage         = 'https://mp.csdn.net/console/home'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'rattanchen' => '1005332621@qq.com' }
  s.source           = { :git => 'https://gitee.com/chenzm_186/ZMinLib.git', :tag => s.version.to_s }
  s.ios.deployment_target = '9.0'
  s.source_files = 'ZMinLib/Classes/**/*'
end

常用属性说明:

  • s.name :pod search 搜索的关键词,注意这里一定要和.podspec的名称一样

  • s.version :版本号,这个版本号必须与对应的Tag一致。上面例子中我们设置的为0.1.0

  • s.summary : 简介,这个简介你需要修改一下,对项目的简短介绍,不修改的话会有警告。

  • s.homepage : 项目主页地址,这个地址需要是https地址

  • s.license : 许可证

  • s.author : 作者

  • s.social_media_url : 社交网址

  • s.source : 项目的地址

  • s.source_files : 需要包含的源文件,“*” 表示匹配所有文件,“**” 表示匹配所有子目录。

  • s.resources: 资源文件

  • s.requires_arc : 是否支持ARC

  • s.dependency :依赖库

  • s.ios.deployment_target = ‘8.0’ : 支持的pod最低版本

  • 多个目录

s.source_files = 'FFTest/Classes/*.{h,m}', 'FFTest/UI/*.{h,m}'
# Classes以及的Classes的子文件夹下的所有文件
s.source_files = 'FFTest/Classes/**/*'
# 只是Classes目录
s.source_files = 'FFTest/Classes/*'

更多关于podspec的介绍可以点击进入了解。

公共的控件、工具类、基本框架的植入,以及对相关组件的分类管理主要体现在这里。

2、同步远程私有仓库

2.1、创建Git Hub远程仓库

如果是公司的项目,需要运维同事搭建一个Git Lab仓库并创建项目。这里用Gitee代替。在Gitee上创建一个ZMinLib的项目。地址:https://gitee.com/chenzm_186/ZMinLib.git
在这里插入图片描述

2.2、同步代码

2.2.1、代码提交到远程仓库

cd ZMinLib/
git init
git add .
git commit -m "添加项目"
git remote add origin https://gitee.com/chenzm_186/ZMinLib.git
git push -u origin maste

2.2.2、Sourcetree提交
先用Sourcetree把远程库同步到本地,将项目copy到本地仓库,使用Sourcetree提交代码
不管黑猫白猫,能抓得到老鼠的都是好猫。两个方法都能实现,看个人习惯。

3、添加组件并同步远程仓库

3.1、添加组件

导入组件,做好配置关系后,提交代码跟平时一样提交代码到远程仓库就可以。这里的核心是在xxx.podspec文件中配置好依赖关系,详细的见上面的配置文件说明。点击这里有实例可以参考,不再做详述。

3.2、添加Tag

每一个版本我们需要添加一个Tag,如下图
在这里插入图片描述
Sourcetree界面上找不到打标签位置的,可以使用快捷键【command+shift+T】展示,也可以在顶部菜单栏【仓库】中找到:

在这里插入图片描述

3.3、校验配置文件是否填写正确
# 验证本地podspec文件
pod lib lint --allow-warnings
# 校验远程podspec文件
pod spec lint --allow-warnings

其中–allow-warnings参数代表忽略警告,如果你的代码在编译时有警告,如果不加这个参数就会报错。结果如下:

chenzimin@rattanchen ZMinLib % pod spec lint --allow-warnings
 -> ZMinLib (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'ZMinLib' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
Analyzed 1 podspec.
ZMinLib.podspec passed validation.
  • 这里如果有error 就得先解决error。
  • 注意:如果校验配置文件出现提示tag版本没有或者不对的话,可能是只设置了本地tag而没有推送到远程成库,先执行3.3步骤。还有其他原因,如source、source_files等路径配置错误。
3.4、发布版本
# 创建本地索引
pod repo add ZMinLib https://gitee.com/chenzm_186/ZMinLib.git
# 进入本地索引库可以看到新建索引库
cd ~/.cocoapods/repos/
open .
# 推送至索引库,执行这个操作之后,才会生成对应的索引文件
pod repo push ZMinLib ZMinLib.podspec --allow-warnings
或
pod repo push ZMinLib ZMinLib.podspec --sources='https://gitee.com/chenzm_186/ZMinLib.git' --allow-warnings

执行这两步操作,操作结果如下:

chenzimin@rattanchen ZMinLib % pod repo add ZMinLib https://gitee.com/chenzm_186/ZMinLib.git
Cloning spec repo `ZMinLib` from `https://gitee.com/chenzm_186/ZMinLib.git`
chenzimin@rattanchen ZMinLib % pod repo push ZMinLib ZMinLib.podspec --allow-warnings
Validating spec
 -> ZMinLib (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'ZMinLib' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
Updating the `ZMinLib' repo
Adding the spec to the `ZMinLib' repo
 - [Update] ZMinLib (0.1.0)
Pushing the `ZMinLib' repo

生成对应的索引文件后,执行

# 验证
pod search ZMinLib

执行验证,如结果如下,则说明成功了:

-> ZMinLib (0.2.0)
   简述说明ZMinLib:例子
   pod 'ZMinLib', '~> 0.2.0'
   - Homepage: https://mp.csdn.net/console/home
   - Source:   https://gitee.com/chenzm_186/ZMinLib.git
   - Versions: 0.2.0, 0.1.9 [ZMinLib repo]
   - Subspecs:
     - ZMinLib/WCDB (0.2.0)
     - ZMinLib/ZipArchive (0.2.0)
     - ZMinLib/All (0.2.0)

4、迭代更新

组件的迭代更新,同项目代码更新一致。

5、命令行说明

5.1、pod repo add
pod repo add 库名称 库地址

是将原创仓库添加到本地,执行下面命令:

cd ~/.cocoapods/repos/
open .

就能看到Cocopods的所有本地仓库列表了,例子的库是ZMinLib。我们也可以浏览一下其它的目录,就能找到很多后缀为podspec的文件。
【pod install】命令就是根据要安装的库的名字在这些目录中遍历,找到对应的配置文件后,解析里面的地址和配置进行下载。
【pod update】命令是从远程库,把这些配置文件下载到本地的这个目录中,再install。

5.2、pod repo push
pod repo push 库名 库配置文件(后缀为podspec) --allow-warnings

这个命令就是发布版本的命令,将版本push到远程,我们在~/.cocoapods/repos/ZMinLib/ZMinLib /目录中,可以找到对应版本号的目录,目录里面就是配置文件(后缀为podspec)。
【pod install】时指定版本或最新版本时,就是根据版本号找到对应的配置文件的。

5.3、命令行总结
# 创建空目录并进入目录位置
mkdir Module
cd Module
# 新建脚手架
pod lib create ZMinLib
# 验证本地podspec文件
pod lib lint --allow-warnings
# 校验远程podspec文件
pod spec lint --allow-warnings
# 创建本地索引
pod repo add ZMinLib https://gitee.com/chenzm_186/ZMinLib.git
# 进入本地索引库可以看到新建索引库
cd ~/.cocoapods/repos/
open .
# 推送至索引库,执行这个操作之后,才会生成对应的索引文件
pod repo push ZMinLib ZMinLib.podspec --allow-warnings
# 验证
pod search ZMinLib

参鉴:
1、https://www.jianshu.com/p/cbb8931499da
2、https://www.jianshu.com/p/607ceb7557f7
3、https://www.jianshu.com/p/838f36c65a20
4、https://blog.csdn.net/sacrifice123/article/details/83958405

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜子三百克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值