iOS组件化方案-总结第一篇

概述

近一年iOS业界讨论组件化方案甚多,大体来说有3种。

  • Protocol注册方案
  • URL注册方案
  • Target-Action runtime调用方案

URL注册方案据我了解很多大公司都在采用,蘑菇街 App 的组件化之路蘑菇街的Limboy在这篇博客中做了很详尽的阐述

Target-Action runtime调用方案Casa在 iOS应用架构谈 组件化方案中也做了很详尽的描述,前阵时间Casa开了一篇博客在现有工程中实施基于CTMediator的组件化方案清楚讲述了如何用这套方案实施组件化

Protocol方案我尚未看到有人做过详尽的分享,也许是我孤陋寡闻,不过在这里,我会教大家用Protocol方案实施组件化,不仅如此..

我会采用以上3种方案详尽的实现3个Demo,Demo会在文尾给到,本文不过多阐述3种方案的优劣,我会在最后做一个总结,希望给想了解组件化方案的同学或者给在项目中准备实施组件化方案的同学提供一个借鉴。

第二篇 也已经出来了传送门iOS组件化方案-总结第二篇

业务模拟场景

  • 首页展示商品列表
  • 商品详情页展示商品的详细信息
  • 确认订单页生成订单

把业务连贯起来 点击首页中A商品,进入A商品的商品详情页 ,点击商品详情页中的 立即购买 进入确认订单页,点击确认订单页中的提交订单 会返回到商品详情页,并且在商品详情页中告知用户下单成功.

真实业务场景下确认订单页提交订单 是不会回到商品详情页的,模拟这个场景是想在Demo中实现2个模块中反向回调。

一、Protocol注册方案

正式实施前先奉上Demo,建议只下一个主项目就可以了(注:下载完不需要pod install或者pod update,pods在我私有源上 我没有填写.gitignore文件,下载完都是可以直接跑的)

主项目地址

商品详情业务接口组件地址

商品详情业务组件地址

确认订单业务接口组件地址

确认订单业务组件地址

业务调度中间件地址

1.基本准备工作

  • 先去gitHub创建一个项目存放私有Repo源,repo地址https://github.com/sun6boys/CRRepositories.git 后面3种方案私有pod源都会放在这里。
  • 本地添加私有源 终端执行命令pod repo add CRRepositories https://github.com/sun6boys/CRRepositories.git(如果之前并未向gitHub push过文件也没有把SSH公钥保存到gitHub,这时候应该会提示你输入gitHub账号密码)
  • 以上操作完成 cd ~/.cocoapods/repos目录下至少会有2个文件夹 CRRepositoriesmaster, master文件下面存放的是公有源文件,CRRepositories*目录下目前是空的,后面会存放我们私有源文件
  • 基本准备工作完成。

2.Xcode创建项目[CRProtocolManager]

CRProtocolManagerMGJRouterCTMediator一样属于模块之间调度的中间件

CRProtocolManager项目下创建名为CRProtocolManager的文件夹,后面我们需要做成私有pod的文件均放在该文件夹下。

创建CRProtocolManager类(.h,.m),定义2个对外接口

@interface CRProtocolManager : NSObject

+ (void)registServiceProvide:(id)provide forProtocol:(Protocol*)protocol;

+ (id)serviceProvideForProtocol:(Protocol *)protocol;

@end

具体方法实现很简单可以参看Demo,我这里只是简单处理。
接下来就是要把项目提交到gitHub,做私有pod了

  • gitHub新建一个project名为CRProtocolManager

  • 终端cd至CRProtocolManager项目目录下执行命令git remote add origin https://github.com/sun6boys/CRProtocolManager.git

  • 因cocoaPods强制添加开源许可文件执行命令echo MIT>FILE_LICENSE创建名为FILE_LICENSE的文件

  • 终端cd至CRProtocolManager目录下执行命令pod spec create CRProtocolManager

  • 执行命令vim .CRProtocolManager.podspec编辑podspec文件,具体如何编辑可参看Demo中的podspec文件或者google

  • 退出编辑执行命令git add .

  • `git commit -m 'log'

  • git tag 0.0.1 tag一定要和podspec中的version一致

  • git push origin master --tags --tags为了把刚才添加的tag提交上去

  • 执行命令pod repo push CRRepositories CRProtocolManager.podspec --verbose --allow-warnings 注:CRRepositories即为准备工作中的私有源仓库

  • 成功后pod search CRProtocolManager应该就能搜索到了

万里长征终于走完第一步,基础设施已经构建完毕

3.商品详情业务模块

既然组件化了,那我们所有的业务模块都是单独的project,但是这里我会分2个project,一个是商品详情业务入口模块,一个是商品详情业务模块。业务入口模块即是定义该模块对外提供业务接口的protocol,如果A模块需要调用到B模块,那A模块只需要引入CRProtocolManager和B模块的protocol,而不是引入整个B模块。

新建一个projectCRGoodsDetailServiceProtocol,创建一个和项目名一样的protocol文件,定义接口如下


@protocol CRGoodsDetailServiceProtocol <NSObject>

@required;

- (UIViewController *)goodsDetailViewControllerWithGoodsId:(NSString*)goodsId goodsName:(NSString *)goodsName;

@end

参照CRProtocolManager做成私有pod

以上实施完毕,新建一个projectCRGoodsDetail,新建2个类

CRGoodsDetailServiceProvide
CRGoodsDetailViewController

CRGoodsDetailServiceProvide即是CRGoodsDetailServiceProtocol的实现者 所以他依赖
CRGoodsDetailServiceProtocol,因为商品详情模块需要跳转到订单确认页,所以他也依赖CRProtocolManager

添加Podfile文件编辑如下

source 'https://github.com/sun6boys/CRRepositories.git'
source 'https://github.com/CocoaPods/Specs.git'

target 'CRGoodsDetail' do

pod "CRProtocolManager"
pod "CRGoodsDetailServiceProtocol"

end

执行pod install --verbose --no-repo-update

最终CRGoodsDetailServiceProvide实现代码如下

#import "CRGoodsDetailServiceProvide.h"
#import <CRGoodsDetailServiceProtocol/CRGoodsDetailServiceProtocol.h>
#import <CRProtocolManager/CRProtocolManager.h>

#import "CRGoodsDetailViewController.h"

@interface CRGoodsDetailServiceProvide()<CRGoodsDetailServiceProtocol>

@end

@implementation CRGoodsDetailServiceProvide

+ (void)load
{
    [CRProtocolManager registServiceProvide:[[self alloc] init] forProtocol:@protocol(CRGoodsDetailServiceProtocol)];
}

- (UIViewController *)goodsDetailViewControllerWithGoodsId:(NSString*)goodsId goodsName:(NSString *)goodsName
{
    CRGoodsDetailViewController *goodsDetailVC = [[CRGoodsDetailViewController alloc] initWithGoodsId:goodsId goodsName:goodsName];
    return goodsDetailVC;
}

@end

CRGoodsDetailViewController实现代码如下

#import "CRGoodsDetailViewController.h"

@interface CRGoodsDetailViewController ()

@property (nonatomic, copy) NSString *goodsId;
@property (nonatomic, copy) NSString *goodsName;

@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UIButton *buyButton;
@end

@implementation CRGoodsDetailViewController

- (instancetype)initWithGoodsId:(NSString *)goodsId goodsName:(NSString *)goodsName
{
    self = [super init];
    if (self) {
        _goodsId = goodsId;
        _goodsName = goodsName;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.title;
    
    [self.view addSubview:self.statusLabel];
    [self.view addSubview:self.buyButton];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.statusLabel.frame = CGRectMake(0, 0, 100, 20);
    self.statusLabel.center = self.view.center;
    
    self.buyButton.frame = CGRectMake(0, self.view.frame.size.height - 45, self.view.frame.size.width, 45);
}

#pragma mark - event 
- (void)didClickBuyButton:(UIButton *)button
{
    
}

#pragma mark - getters
- (UILabel *)statusLabel
{
    if (_statusLabel == nil) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.textColor = [UIColor redColor];
        _statusLabel.font = [UIFont systemFontOfSize:15.f];
        _statusLabel.textAlignment = NSTextAlignmentCenter;
        _statusLabel.text = @"暂未购买";
    }
    return _statusLabel;
}

- (UIButton *)buyButton
{
    if (_buyButton == nil) {
        _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
        [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_buyButton setBackgroundColor:[UIColor redColor]];
        [_buyButton addTarget:self action:@selector(didClickBuyButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyButton;
}
@end

CRGoodsDetail做成私有pod 记得编辑podspec文件的时候添加dependencyCRProtocolManager CRGoodsDetailServiceProtocol

4.新建主项目MainProject

为了少建一个项目首页模块我是直接放在主项目中的,按理首页也应该是一个独立的pod.
首页业务场景是,显示商品列表,点击某个商品进入该商品详情页. 所以他依赖CRGoodsDetailServiceProtocolCRProtocolManager因为首页模块即是主项目所以他还得依赖CRGoodsDetail

最终首页核心代码如下

#pragma mark - event
- (void)didClickGoodsButton:(UIButton *)button
{
    id<CRGoodsDetailServiceProtocol> goodsServiceProvide = [CRProtocolManager serviceProvideForProtocol:@protocol(CRGoodsDetailServiceProtocol)];
    UIViewController *goodsDetailVC = [goodsServiceProvide goodsDetailViewControllerWithGoodsId:@"123" goodsName:@"农夫山泉矿泉水"];
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
    
}

5.确认订单模块

参照商品详情新建确认订单业务入口pod 以及确认订单业务pod.和商品详情有区别的是,提交订单完成后要回到商品详情并且通知商品详情用户已经购买,所以CRConfirmOrderServiceProtocol接口定义如下

@protocol CRConfirmOrderServiceProtocol <NSObject>

- (UIViewController *)confirmOrderViewControllerWithGoodsId:(NSString *)goodsId sureComplete:(dispatch_block_t)sureComplete;

@end

最后记得在商品详情加上跳转并且podspec里面加上dependency

Protocol注册方案完结



作者:sun6boys
链接:https://www.jianshu.com/p/2cb4cc8d216e
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/super_man_ww/article/details/79493033