天天看点

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个文件夹 CRRepositories 和 master, master文件下面存放的是公有源文件,CRRepositories*目录下目前是空的,后面会存放我们私有源文件
  • 基本准备工作完成。

2.Xcode创建项目[CRProtocolManager]

CRProtocolManager和MGJRouter、CTMediator一样属于模块之间调度的中间件

在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模块。

新建一个project

CRGoodsDetailServiceProtocol

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

@protocol CRGoodsDetailServiceProtocol <NSObject>

@required;

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

@end

           

参照CRProtocolManager做成私有pod

以上实施完毕,新建一个project

CRGoodsDetail

,新建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(, , , );
    self.statusLabel.center = self.view.center;
    
    self.buyButton.frame = CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width, );
}

#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: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文件的时候添加dependency

CRProtocolManager

CRGoodsDetailServiceProtocol

4.新建主项目

MainProject

为了少建一个项目

首页

模块我是直接放在主项目中的,按理

首页

也应该是一个独立的pod.

首页

业务场景是,显示商品列表,点击某个商品进入该商品详情页. 所以他依赖

CRGoodsDetailServiceProtocol

CRProtocolManager

因为首页模块即是主项目所以他还得依赖

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

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

继续阅读