一、组件化启蒙文章:html
iOS应用架构谈ios
二、经过Cocoapods实现组件化:git
Cocoapods 创建私有库 - 官方github
三、组件化相关资料:ide
Cocoapods 系列教程
post
以个人切身经从来说:
一、随着业务增长,需求迭代。整个项目的文件愈来愈多,Build愈来愈慢,按下 Command + R,而后喝杯卡布奇诺再蹲个坑回来,项目尚未跑完。。。
二、干外包,啊不,在外包公司作项目。通用的基础功能代码,如:菊花、网络访问组件、下拉刷新、数据持久化、基础类的分类。若是不把它们弄成私有库,那么咱们只能选择:
言归正传,我对项目进行组件化我干了两件事:
私有库固然要用私有Spec Repo,固然可使用官方的Repo,但若是只想添加本身的Pods,那仍是使用私有的Repo把。打开:~/.cocoapods/repos。你会看到一个master文件夹,这个是 Cocoapods 官方的 Spec Repo。
这里建立 DYDemoSpecs 仓库。
# pod repo add [Private Repo Name] [GitHub HTTPS clone URL]
$ pod repo add DYDemoSpecs https://github.com/liyunxin/DYDemoSpecs.git复制代码
此时若是成功的话,到:~/.cocoapods/repos 目录能够看到DYDemoSpecs。
经过官方文档:Using Pod Lib Create,建立一个DYDemoTools项目。cd到想要建立项目的目录而后执行:
$ pod lib create DYDemoTools复制代码
接着在命令行须要你确认一些参数:
根据命令行的输入会建立不同的工程
这里建立 DYDemoTools 仓库,并clone到本地。
把2.1步骤生成的项目的相关文件复制到DYDemoTools仓库目录下
这里你也能够直接给2.1生成的项目设置远端仓库
podspec是Ruby文件,能够打开Example的项目工程查看。
这是我在DYDemoTools中使用的podspec文件:
相关字段能够到 官方文档 查阅
Pod::Spec.new do |s|
s.name = 'DYDemoTools'
s.version = '0.0.1'
s.summary = 'DYDemoTools.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/liyunxin/DYDemoTools'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'liyunxin' => '447674668@qq.com' }
s.source = { :git => 'https://github.com/liyunxin/DYDemoTools.git', :tag => s.version.to_s }
s.platform = :ios, "10.0"
s.frameworks = 'UIKit'
s.source_files = 'DYDemoTools/Classes/**/*'
end复制代码
从 podspec 中,source_files字段的设置能够知道,该组件的源文件都要放在仓库中DYDemoTools文件夹中。
这里我添加一个自定义Button到Pod/Classes,而后进入Example文件夹执行 pod update 命令。再次打开Example项目工程能够看到:
每一次修改了pod或者之后更新了podspec版本都须要从新执行一遍 pod update 命令
使用示例:
在Example工程的ViewController中直接导入 KHBarButton,这里要先给Main.storyboard添加导航控制器
#import "ViewController.h"
#import "KHBarButton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[KHBarButton rightBtnWithTitle:@"德玛西亚" Color:[UIColor blackColor] ClickOption:^{
NSLog(@"德玛西亚,永世长存");
}] getBarItem];
}
@end复制代码
Pod::Spec.new do |s|
s.name = 'DYDemoTools'
s.version = '0.0.1'
s.summary = 'DYDemoTools.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/liyunxin/DYDemoTools'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'liyunxin' => '447674668@qq.com' }
s.source = { :git => 'https://github.com/liyunxin/DYDemoTools.git', :tag => s.version.to_s }
s.platform = :ios, "10.0"
s.frameworks = 'UIKit'
s.source_files = 'DYDemoTools/**/*'
s.resource_bundles = {
'DYDemoTools' => ['DYDemoTools/Base/*.xcassets']
}
end复制代码
获取DYDemoToolsAsset中的图片有不少中方法,这里采起的方法:
在DYDemoYools中添加一个类方法,专门获取DYDemoToolsAsset中的图片
#import <Foundation/Foundation.h>
@interface DYDemoTools : NSObject
///获取KHealthTools这个Bundle的图片
+ (UIImage *)getToolsBundleImage:(NSString *)imageName;
@end
复制代码
#import "DYDemoTools.h"
@implementation DYDemoTools
///获取DYDemoTools这个Bundle的图片
+ (UIImage *)getToolsBundleImage:(NSString *)imageName {
static NSBundle *bundle;
if (bundle == nil) {
bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:NSClassFromString(@"DYDemoTools")] pathForResource:@"DYDemoTools" ofType:@"bundle"]];
}
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
if (image == nil) {
image = [UIImage imageNamed:imageName];
}
return image;
}
@end
复制代码
使用示例:
在Example工程的ViewController中直接导入DYDemoTools
#import "ViewController.h"
#import "KHBarButton.h"
#import "DYDemoTools.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
self.navigationItem.rightBarButtonItem = [[KHBarButton rightBtnWithTitle:@"德玛西亚" Color:[UIColor blackColor] ClickOption:^{
[weakSelf addImageDemo];
}] getBarItem];
}
- (void)addImageDemo {
UIImage *img = [DYDemoTools getToolsBundleImage:@"img1"];
UIImageView *iV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, img.size.width, img.size.height)];
iV.image = img;
[self.view addSubview:iV];
}
@end复制代码
效果图:
这个未来有大用
在Base里面添加:DYDemoToolsHeader.h
Pod::Spec.new do |s|
s.name = 'DYDemoTools'
s.version = '0.0.1'
s.summary = 'A short description of DYDemoTools.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/liyunxin/DYDemoTools'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'liyunxin' => '447674668@qq.com' }
s.source = { :git => 'https://github.com/liyunxin/DYDemoTools.git', :tag => s.version.to_s }
s.platform = :ios, "10.0"
s.frameworks = 'UIKit'
s.subspec '0_Base' do |sb|
sb.source_files = "DYDemoTools/0_Base/**/*.{h,m}"
sb.resource_bundles = {
'DYDemoTools' => ['DYDemoTools/0_Base/*.xcassets']
}
end
s.subspec '1_Tools' do |st|
st.source_files = "DYDemoTools/1_Tools/**/*.{h,m}"
end
s.subspec '2_View' do |sv|
sv.source_files = "DYDemoTools/2_View/**/*.{h,m}"
end
end
复制代码
终于来到这里了,提交以前要验证 podspec 是否无误。不然没办法提交。而后还要给 git 打上一个 tag,该 tag 要与 podspec 的 version 相同。
#cd到DYDemoTools文件夹,执行下面的命令
pod lib lint --allow-warnings复制代码
若是出现:DYDemoTools passed validation. 那恭喜我,本地校验经过:
#cd到DYDemoTools文件夹,执行下面的命令
pod repo push DYDemoSpecs DYDemoTools.podspec --allow-warnings复制代码
查看:~/.cocoapods/repos/DYDemoSpecs,你会看到新增了一个DYDemoSpecs
到此,第一个组件库建立完成
这里建立 DYDemo666 仓库。
建立一个DYDemo666工程。添加podfile文件以下:
use_frameworks!
platform :ios, '10.0'
source 'https://github.com/CocoaPods/Specs.git' # 官方库
source 'https://github.com/liyunxin/DYDemoSpecs.git' # 私有库
target 'DYDemo666' do
pod 'DYDemoTools'
end复制代码
执行成功后打开项目:
在 DYDemo666 中的 ViewController 使用组件的东西:
#import "ViewController.h"
#import "DYDemoToolsHeader.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
self.navigationItem.rightBarButtonItem = [[KHBarButton rightBtnWithTitle:@"哈哈哈" Color:[UIColor blackColor] ClickOption:^{
[weakSelf addImageDemo];
}] getBarItem];
}
- (void)addImageDemo {
UIImage *img = [DYDemoTools getToolsBundleImage:@"img1"];
UIImageView *iV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, img.size.width, img.size.height)];
iV.image = img;
[self.view addSubview:iV];
}
@end复制代码
使用效果:
接下来的笔记在下一篇文章:iOS组件化 - 项目组件化