本文章用到的demo的下载地址
关于组件化前几篇文章请查看这里
iOS组件化(上篇)- 拆分基础组件
iOS组件化(中篇)-拆分业务组件
iOS组件化(下篇)-加载XIB、图片资源
我们创建了一个基础组件库FFBaseKit,假如它里面包含了Category分类、Tool工具库、APIs网络库三个功能,目录结构如下图所示。
此时,我们在项目里pod FFBaseKit
基础库时,需要把它里面包含的三个功能都pod下来。有时候,我们可能仅仅只用到了其中一个功能,但是又不想全部都pod下来,那么此时就涉及到子库的制作。
最初,我们的podspec索引文件是这样的
#
# Be sure to run `pod lib lint FFBaseKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'FFBaseKit'
s.version = '1'
s.summary = 'A short description of FFBaseKit.'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/FFComponent/FFBaseKit'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'JiaJung' => '[email protected]' }
s.source = { :git => 'https://github.com/FFComponent/FFBaseKit.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/'
s.ios.deployment_target = '8.0'
s.source_files = 'FFBaseKit/Classes/**/*'
# s.resource_bundles = {
# 'FFBaseKit' => ['FFBaseKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking'
s.dependency 'FMDB'
s.dependency 'MBProgressHUD'
end
接着,我们需要修改podspec文件,因为要设置subspec子库,所以需要注释掉之前的 s.source_files
和原来的依赖库, 然后设置子库的subspec 和子库依赖库
# s.source_files = 'FFBaseKit/Classes/**/*'
# s.dependency 'AFNetworking'
# s.dependency 'FMDB'
# s.dependency 'MBProgressHUD'
s.subspec 'Category' do |category|
category.source_files = 'FFBaseKit/Classes/Category/**/*'
end
s.subspec 'Tools' do |tools|
tools.source_files = 'FFBaseKit/Classes/Tools/**/*'
tools.dependency 'MBProgressHUD'
end
s.subspec 'APIs' do |apis|
apis.source_files = 'FFBaseKit/Classes/APIs/**/*'
apis.dependency 'AFNetworking'
apis.dependency 'FMDB'
end
- s.subspec后面是我们文件目录对应名称
- do后面是起的一个别名
- 别名.source_files是文件路径 (注意路径别写错)
- 别名. dependency后面是子库的依赖库
接着验证podspec文件通过后 pod repo push
命令提交索引文件,如果不知道怎么提交请参看我之前的这篇文章。
然后就可以回到FlowerField_Component项目
中通过下面几种方式使用pod库了
- pod 'FFBaseKit' 引入pod库中所有模块
- pod 'FFBaseKit/Category' 只引入Category模块
- pod 'FFBaseKit', :subspecs => ['Category', 'Tools']
修改FlowerField_Component项目的podspec文件:
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/FFComponent/FFSpecs.git'
use_frameworks!
platform :ios, '8.0'
target ‘FlowerField’ do
pod 'MBProgressHUD'
pod 'AFNetworking'
pod 'Masonry'
pod 'YYWebImage'
pod 'FMDB'
pod 'ReactiveCocoa', '~> 2.5'
pod 'FFBaseKit/Category' //刚才设置好的库
end
pod install之后别忘了删除FlowerField_Component项目项目里Other下的category文件,不然会报重复冲突的
pod之后发现我们的FFBaseKit下就只有Category一个功能模块了
如果想pod APIs 或者 Tools模块功能,需要注释掉原来podspec文件下的pod,并删除Other下对应的文件夹即可
#pod 'MBProgressHUD'
#pod 'AFNetworking'
#pod 'FMDB'