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

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

前言


本文主要介绍了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项目,形成的项目脚手架如下:

20201208113247214.png


注意:

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

20201208113803186.png


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,如下图

20201208120745217.png



Sourcetree界面上找不到打标签位置的,可以使用快捷键【command+shift+T】展示,也可以在顶部菜单栏【仓库】中找到:

20201208121022431.png



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
1
2
执行验证,如结果如下,则说明成功了:
-> 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 .

2

就能看到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


相关文章
|
4月前
|
XML JSON API
IOS网络编程:介绍一下 Alamofire 库。
IOS网络编程:介绍一下 Alamofire 库。
37 3
|
10月前
|
缓存 移动开发 前端开发
iOS项目组件化历程
随着业务的发展,App中的页面,网络请求,通用弹层UI,通用TableCell数量就会剧增,需求的开发人员数量也会逐渐增多。 如果所有业务都在同一个App中,并且同时开发人数较少时,抛开代码健壮性不谈,实际的开发体验可能并没有那么糟糕,毕竟作为一个开发,什么地方用什么控件,就跟在HashMap中通过Key获取Value那么简单。 那么当业务成长到需要分化到多个App的时候,组件化的重要性开始体现了。
58 0
|
10月前
|
前端开发 API Android开发
Android侧滑踩坑记(仿IOS侧滑finish页面基于Slidr库)
Android侧滑踩坑记(仿IOS侧滑finish页面基于Slidr库)
180 0
|
Web App开发 存储 网络协议
ios 组件化之Cocoapods私有库详解以及问题解决方案
ios 组件化之Cocoapods私有库详解以及问题解决方案
ios 组件化之Cocoapods私有库详解以及问题解决方案
|
开发工具 iOS开发 git
iOS 开发 - 亲身经历告诉你如何把自己的库通过pod导入(详细步骤)
iOS 开发 - 亲身经历告诉你如何把自己的库通过pod导入(详细步骤)
264 0
iOS 开发 - 亲身经历告诉你如何把自己的库通过pod导入(详细步骤)
|
程序员 定位技术 vr&ar
最新 iOS 库 打包篇.a .bundle .framework
库是共享程序代码的方式,一般分为静态库和动态库。库是程序代码的集合,是共享程序代码的一种方式
625 0
最新 iOS 库 打包篇.a .bundle .framework
|
程序员 定位技术 vr&ar
iOS 库 打包篇.a .bundle .framework
库是程序代码的集合,是共享程序代码的一种方式
232 0
iOS 库 打包篇.a .bundle .framework
|
测试技术 开发工具 Swift
iOS项目组件化
随着公司业务的不断发展,团队不断壮大的同时,项目也随之臃肿起来,如何保障团队协作的高效,自然的想到了组件化这个话题。下面总结下本人的梳理和思考。
30177 12
iOS项目组件化
|
中间件 调度 iOS开发
iOS 组件化方案
现在很多公司在业务扩张到一定程度或者商业模式跑通之后就会考虑到组件化方案,而组件化方案目前主流的做法就是以下三种,这里我们将就组件化方案谈谈自己的思考。 - `URL Scheme` - `Protocol Class` - `Target Action`
iOS 组件化方案
|
Shell 开发工具 Swift
iOS 远程私有库的搭建以及使用(下)
iOS 远程私有库的搭建以及使用(下)
263 0
iOS 远程私有库的搭建以及使用(下)