美文网首页面试
ios 基于CTMediator的组件化搭建历程

ios 基于CTMediator的组件化搭建历程

作者: Baffin | 来源:发表于2021-03-01 17:48 被阅读0次

    需求前提:希望App由多个业务组件组装起来,每个业务模块都可单独成为子App。
    解耦,快速开发!

    流程

    1. 创建远程私有库
    2. 创建业务组件项目xxx,并同步到私有库
    3. 创建组件与外界联系媒介xxx_Category项目并同步远端仓库
    4. 编写代码
    5. 主项目Podfile本地引用组件项目,并使项目编译通过.
    6. 将本地引用改为远程引用,运行项目并编译成功,组件化完成.

    搭建项目

    1.创建远程私有库

    • github上开了一个orgnization
    • 在orgnization中创建私有pod源仓库,命名为XXXPod(用来存放后面打包的私有库)
    • 在orgnization中创建业务组件项目xxx远程仓库
    • 在orgnization中创建业务组件项目xxx与外界联系媒介xxx_Category远程仓库

    如果有多个便创建多个仓库,目录如下:


    ·

    2. 创建业务组件项目

    cd 项目存放目录
    
    //创建组件项目
    pod lib create xxx
    
    • 根据提示输入


      image.png
    • 得到如下工程:
      Class:存放组件对外接口
      Assets:存放资源


      image.png
    • 同步代码到github上(这里就不做多说了)

    3.创建组件与外界联系媒介xxx_Category项目及远端仓库

    cd 项目存放目录
    
    //创建组件项目
    pod lib create xxx_Category
    
    • 根据提示输入


      image.png
    • 得到如下工程


      image.png
    • 同步代码到github上

    4.编写组件工程代码

    以跳转到ExampleViewController为例

    1.创建ExampleViewController并编写需要的业务代码
    2.创建Target_Business1(后面会说明为何这么命名)用以跳转到ExampleViewController
    @interface Target_Business1 : NSObject
    
    - (UIViewController *)Action_viewController:(NSDictionary *)params;
    
    @end
    
    
    
    
    #import "Target_Business1.h"
    #import "ExampleViewController.h"
    
    @implementation Target_Business1
    
    - (UIViewController *)Action_viewController:(NSDictionary *)params
    {
        ExampleViewController *viewController = [[ExampleViewController alloc] init];
        viewController.title = params[@"title"];//以设置title为例
        return viewController;
    }
    
    @end
    

    将业务代码文件及Target_Business1文件拖入class文件夹重新pod update, 运行成功即可!在这里可以在Example demo中写个接口跳转到业务代码页面进行调试!

    5.编写xxx_Category中的对外代码

    注意xxx_Category需要依赖CTMediator,在 XXX_Category.podspec文件中添加依赖:

    s.dependency 'CTMediator'
    

    创建对外联系接口文件CTMediator+XXX

    #import <CTMediator/CTMediator.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface CTMediator (BUSINESS1_CATEGORY)
    
    - (UIViewController *)toBusiness1WithParam:(NSDictionary *)param;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    @implementation CTMediator (BUSINESS1_CATEGORY)
    
    - (UIViewController *)toBusiness1WithParam:(NSDictionary *)param{
        return [self performTarget:@"Business1" action:@"viewController" params:param shouldCacheTarget:NO];
    }
    
    @end
    

    这里用到的performTarget方法我们可以点击进去查看源码,可以看到固定写法Target_XXX,所以在之前我们创建了命名为Target_XXX的文件!

    运行成功后将CTMediator+XXX拖入Classes文件夹,重新pod update,运行!

    6. 将两个组件项目复制到主项目工程目录中,在Podfile中引用组件

      pod 'Business1',:path => 'Business1'
      pod 'Business1_Category',:path => 'Business1_Category'
    
    pod update 运行成功即可 image.png

    7. 主项目工程中引用

    #import <CTMediator+BUSINESS1_CATEGORY.h>
    UIViewController *viewController = [[CTMediator sharedInstance] toBusiness1WithParam:@{@"title":@"业务1"}];
            [self.navigationController pushViewController:viewController animated:YES];
    

    8. 将本地引用改为远程引用

    1. 进入私有库工程找到XXX.podspec,并根据具体需要修改,如: image.png
    2. 将组件代码都push到远程库
    3. 上传到我们的私有pod源仓库(也就是第一步我们创建的私有pod源仓库)
    cd 到私有库文件
    
    git tag 0.1.0(注意,这里的tag必须和.podSpec文件的版本一致)
    
    git push --tags
    
    pod repo add 源仓库名称 源仓库远程地址(第一步创建的私有pod源仓库地址)
    如:pod repo add ConfigPrivatePod https://github.com/Baffin-TM/ConfigPrivatePod.git
    
    这样我们可以在.cocoapods中看到: image.png

    再执行:

    pod repo push ConfigPrivatePod Business1.podspec --allow-warnings
    

    可以看到github源仓库ConfigPrivatePod以及本地.cocoapods中的ConfigPrivatePod将会将会多出一个文件:


    image.png
    image.png
    1. 同上,将Business1_Category上传到远程私有库
    git tag 0.1.0  
    git push --tags
    #无需在pod repo add 源仓库名称 源仓库远程地址
    pod repo push ConfigPrivatePod Business1_Category.podspec --allow-warnings
    

    9. Podfile引用远程组件

    #一定要加远程索引库地址
    source 'https://github.com/Baffin-TM/ConfigPrivatePod.git'
    source 'https://github.com/CocoaPods/Specs.git'
    
    pod 'Business1'
    pod 'Business1_Category'
    

    pod update 运行成功即可

    10. 更新库

    1.组件中podspec文件把版本提高一个

    2.提交代码到远端库,打上tag,tag和podspec文件里面版本一样

    3.pod repo push 源仓库名称 XXX.podspec --allow-warnings

    相关文章

      网友评论

        本文标题:ios 基于CTMediator的组件化搭建历程

        本文链接:https://www.haomeiwen.com/subject/wfxdfltx.html