在我们工程进行组件化时,我们一般会创建一些远程私有库,这些一般是基础库或者功能库,对于工程模块划分后,我们如果也制作为远端私有库,那么后续功能迭代时更新这个私有库就太频繁,一点也不方便。
所以一般会将模块划分的代码放在本地库,本地库放在跟工程目录同级文件夹中,这样既能起到隔离解耦作用,又能保持跟随项目git迭代。
下面是本地库的制作方式。
一、制作步骤
- 在项目的根目录下创建一个文件夹Library,这个文件夹专门用来存项目本地库的,比如我们在里面创建一个
AJCategory
. - cd 到
AJCategory
文件夹,然后执行下面命令行创建AJCategory.podspec
文件。
pod spec create AJCategory
3. 再新建一个LICENSE文件,放在AJCategory文件夹下,LICENSE文件的内容是:
The MIT License (MIT)
Copyright © 2018 <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4. 新建一个Classes文件夹,放在AJCategory文件夹下,把自己要制作成本地库的文件放到Classes文件夹内。
- 编辑好podspec文件,跟私有库类似,下面是一个模板:
spec.name = "AJCategory"
spec.version = "0.0.1"
spec.summary = "about colors use in project."
spec.description = <<-DESC
集成了项目中使用的颜色的情况 集成了项目中使用的颜色的情况
DESC
spec.homepage = "https://www.qyizhong.cn" // 瞎写的
spec.license = "LICENSE"
spec.source = { :git => "", :tag => "#{spec.version}" }
spec.source_files = "Classes", "Classes/**/*.{h,m}"
spec.exclude_files = "Classes/Exclude"
- cd到podspec文件所在目录,执行命令行
pod lib lint --allow-warnings
去检查一下。 如果是下面这个报错是可以不用改的.
pod lib lint --allow-warnings
nil versions are discouraged and will be deprecated in Rubygems 4
-> AJCategory (0.0.1)
- ERROR | [OSX] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
二、使用步骤
- 在项目的Podfile文件中使用
pod 'AJCategory', :path => './LocolLib/AJCategory'
- cd 到项目所在目录下,执行
pod install
. - 可以看到自己制作的本地库已经安装在
Development Pods
目录下面了。
更多文章: 组件化之CocoaPods私有库的创建和管理