iOS開源:模塊通信YLBModule

使用YLBModule庫:

pod 'YLBModule', :git => 'https://github.com/ProBobo/YLBModule.git'

YLBModule庫地址:https://github.com/ProBobo/YLBModule。

YLBModule庫其實分為兩部分:模塊注冊和服務(wù)注冊。

模塊注冊:是為了能在模塊中做一些初始化操作。比如一些第三方SDK的初始化。
服務(wù)注冊:是為了能調(diào)用模塊,進(jìn)行模塊通信。

一、模塊注冊

模塊設(shè)置

1、創(chuàng)建模塊類文件(.h和.m文件),一般命名規(guī)則為模塊名稱+AppDelegate。這里的模塊類為YLBDesignAppDelegate。

2、模塊類添加YLBModuleProtocol協(xié)議
為什么要添加YLBModuleProtocol協(xié)議?
因為添加YLBModuleProtocol協(xié)議以后才能實現(xiàn)didFinishLaunchingWithOptions方法。

@interface YLBDesignAppDelegate ()<YLBModuleProtocol>

@end

3、將當(dāng)前類注冊為模塊

+ (void)load {
    //注冊模塊
    [[YLBModuleManager sharedInstance] registerModuleClass:[self class]];
}

4、設(shè)置模塊加載優(yōu)先級(加載的先后順序)

- (NSInteger)ylb_modulePriority {
    return 1000;//不同的模塊返回的值可以不同,數(shù)值越小越先加載
}

5、實現(xiàn)didFinishLaunchingWithOptions方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 可以設(shè)置根視圖等操作
    return YES;
}

主工程設(shè)置

主工程的AppDelegate繼承YLBAppDelegate類,并在didFinishLaunchingWithOptions方法中調(diào)用父類的didFinishLaunchingWithOptions方法,即YLBAppDelegate的didFinishLaunchingWithOptions方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    //調(diào)用 YLBAppDelegate 中 didFinishLaunchingWithOptions 方法從而調(diào)用 YLBModuleManager 的 didFinishLaunchingWithOptions 的方法
    //開始加載模塊
    [super application:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}

這樣模塊就可以成功加載。

二、服務(wù)注冊:模塊通信

服務(wù)注冊的功能更為全面。模塊注冊能實現(xiàn)的效果,服務(wù)注冊都能做到,模塊注冊不能實現(xiàn)的功能,服務(wù)注冊都也能做到。

1、創(chuàng)建服務(wù)協(xié)議
服務(wù)協(xié)議放在單獨的Pod庫里面,可以參看YLBDesign庫(https://github.com/ProBobo/YLBDesign)。

YLBDesign(https://github.com/ProBobo/YLBDesign)的服務(wù)協(xié)議放在Pod庫YLBDServices中。

以YLBDHome和YLBDServices為例,我們說明一下如何創(chuàng)建一個協(xié)議庫。
其中YLBDHome為模塊庫,YLBDServices為協(xié)議庫。

1.1、創(chuàng)建協(xié)議庫
使用pod lib create YLBDServices指令創(chuàng)建YLBDServices庫。
1.2、在協(xié)議庫YLBDServices中創(chuàng)建協(xié)議文件YLBDHomeProtocol。
1.3、在需要使用協(xié)議的模塊中添加Pod依賴庫YLBDServices。
在Podfile文件中添加依賴

# 架構(gòu)服務(wù)組件:屬于YLBModule需要注冊的協(xié)議
pod 'YLBDServices', :git => 'https://github.com/YuliboTeam/YLBDServices.git'

在.podspec文件中添加依賴

s.dependency 'YLBDServices' # 用于YLBModule組件的協(xié)議注冊

1.4、注冊協(xié)議

#import <YLBDServices/YLBDHomeProtocol.h>
+ (void)load {
    //注冊服務(wù)
    [[YLBServiceManager sharedInstance] registerService:@protocol(YLBDHomeProtocol) implClass:NSClassFromString(@"YLBDHomeController")];
}

1.5、通過協(xié)議YLBDHomeProtocol獲取實現(xiàn)協(xié)議的類YLBDHomeController

id<YLBDHomeProtocol> homeVC = [[YLBServiceManager sharedInstance] createService:@protocol(YLBDHomeProtocol)];

2、模塊通信:消息回傳/消息回調(diào)
當(dāng)模塊A調(diào)用模塊B的時候,我們通過registerService方法實現(xiàn)。
但是出現(xiàn)異步回調(diào)時,我們需要在模塊B中把消息傳送給模塊A。

以YLBDDetail和YLBDServices為例,我們說明一下如何實現(xiàn)模塊消息回傳。

2.1、注冊協(xié)議

[[YLBServiceManager sharedInstance] impService:@protocol(YLBDDetailImpProtocol) target:self];

2.2、實現(xiàn)協(xié)議

#pragma mark - 實現(xiàn)YLBDDetailImpProtocol協(xié)議
- (void)ylb_impProtocol {
    NSMutableArray *targetArray = [[YLBServiceManager sharedInstance] impOfProtocol:@protocol(YLBDDetailImpProtocol)];
    for (int i = 0; i < targetArray.count; i++) {
        id<YLBDDetailImpProtocol> ylbdDetail = [targetArray objectAtIndex:i];
        if ([ylbdDetail respondsToSelector:@selector(impFromDetail:)]) {
            
            NSDictionary *dict = @{
                @"moduleName":YLB_PROTECT_STR(self.moduleName),
            };
            
            [ylbdDetail impFromDetail:dict];
        }
    }
}

YLBModule源碼說明

模塊注冊
registerModuleClass方法通過數(shù)組moduleArray存儲模塊類,再對moduleArray通過優(yōu)先級ylb_modulePriority進(jìn)行升序排序。

服務(wù)注冊:模塊通信
registerService方法通過可變字典totalServiceDic存儲協(xié)議的對應(yīng)關(guān)系。

服務(wù)注冊:模塊消息回傳。
impService方法通過可變字典totalTargetDic建立包含的數(shù)組targetArray和協(xié)議的對應(yīng)關(guān)系。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容