组件化 本文主要介绍iOS组件化的三种方案 1、常⽤的三种方案 URL SchemeTarget - ActionProtocol - Class 匹配 1.1、 URL Scheme路由 使 URL 处理本地的跳转通过中间层进⾏注册 &
本文主要介绍iOS组件化的三种方案
URL Scheme路由示例
//MTMediator.h --- starttypedef void(^MTMediatorProcessBlock)(NSDictionary *params);+ (void)registerScheme:(NSString *)scheme processBlock:(MTMediatorProcessBlock)processBlock;+ (void)openUrl:(NSString *)url params:(NSDictionary *)params;//MTMediator.h --- end//MTMediator.m --- start+ (NSMutableDictionary *)mediatorCache{ static NSMutableDictionary *cacheScheme; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ cacheScheme = @{}.mutableCopy; }); return cacheScheme;}+ (void)registerScheme:(NSString *)scheme processBlock:(MTMediatorProcessBlock)processBlock{ if (scheme.length > 0 && processBlock) { [[[self class] mediatorCache] setObject:processBlock forKey:scheme]; }}+ (void)openUrl:(NSString *)url params:(NSDictionary *)params{ MTMediatorProcessBlock block = [[[self class] mediatorCache] objectForKey:url]; if (block) { block(params); }}//MTMediator.m --- end//注册 --- start+ (void)load { [MTMediator reGISterScheme:@"detail://" processBlock:^(NSDictionary * _Nonnull params) { NSString *url = (NSString *)[params objectForKey:@"url"]; UINavigationController *navigationController = (UINavigationController *)[params objectForKey:@"controller"]; MTDetailViewController *controller = [[MTDetailViewController alloc] initWithUrlString:url];// controller.title = [NSString stringWithFORMat:@"%@", @(indexPath.row)]; [navigationController pushViewController:controller animated:YES]; }];}//注册 --- end//调用 --- start//URL Scheme[MTMediator openUrl:@"detail://" params:@{@"url":item.articleUrl,@"controller":self.navigationController}];//调用 --- end复制代码
目前iOS上大部分路由工具都是基于URL 进行匹配的,或者命名约定,通过runtime方法进行动态调用
优点:实现简单
缺点:需要维护字符串表,依赖于命名约定,无法在编译时暴露出所有问题,需要在运行时才能发现错误。
URL路由方式主要是以蘑菇街为代表的的MGJRouter
实现原理:
ModuleManager
注册Url
,有些时候不需要实例化,使用class注册ModuleManager
传递URL,参数跟随URL以GET方式传递,类似openURL。然后由ModuleManager负责调度组件B,最后完成任务。// 1、注册某个URLMGJRouter.registerURLPattern("app://home") { (info) in print("info: (info)")}//2、调用路由MGJRouter.openURL("app://home")复制代码
URL 路由的优点
URl 路由的缺点
Target - Action示例
//MTMediator.h#import #import NS_ASSUME_NONNULL_BEGIN@interface MTMediator : NSObject//target action+ ( __kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl;@endNS_ASSUME_NONNULL_END//MTMediator.m#import "MTMediator.h"@implementation MTMediator+ ( __kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl{ Class detailVC = NSClassFromString(@"MTDetailViewController"); UIViewController *controller = [[detailVC alloc] performSelector:NSSelectorFromString(@"initWithUrlString:") withObject:detailUrl]; return controller;}@end//调用 //Target - Action UIViewController *vc = [MTMediator detailViewControllerWithUrl:item.articleUrl]; vc.title = @"详情啊"; [self.navigationController pushViewController:vc animated:YES];复制代码
原理是通过oc的runtime、cateGory特性动态获取模块,例如通过NSClassFromString
获取类并创建实例,通过performSelector + NSInvocation
动态调用方法。
实现原理:
CTMediator使用
//******* 1、分类定义新接口extension CTMediator{ @objc func A_showHome()->UIViewController?{ //在swift中使用时,需要传入对应项目的target名称,否则会找不到视图控制器 let params = [ kCTMediatorParamsKeySwiftTargetModuleName: "CJLBase_Example" ] //CTMediator提供的performTarget:action:params:shouldCacheTarget:方法 通过传入name,找到对应的targer和action if let vc = self.performTarget("A", action: "Extension_HomeViewController", params: params, shouldCacheTarget: false) as? UIViewController{ return vc } return nil }}//******* 2、模块提供者提供target-action的调用方式(对外需要加上public关键字)class Target_A: NSObject { @objc func Action_Extension_HomeViewController(_ params: [String: Any])->UIViewController{ let home = HomeViewController() return home }}//******* 3、使用if let vc = CTMediator.sharedInstance().A_showHome() { self.navigationController?.pushViewController(vc, animated: true) }复制代码
模块间的关系:
模块A——Mediator——target——模块B
优点
缺点
OC runtime
创建对象,不支持swiftprotocol
和 class
的匹配,不支持更复杂的创建方式 和依赖注入protocol
和对应的类
进行字典匹配
)动态创建实例
Protocol - Class示例
//具体的Protocol//MTMediator.h --- start@protocol MTDetailViewControllerProtocol + (__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl;@end@interface MTMediator : NSObject+ (void)registerProtol:(Protocol *)protocol class:(Class)cls;+ (Class)classForProtocol:(Protocol *)protocol;@end//MTMediator.h --- end//MTMediator.m --- start+ (void)registerProtol:(Protocol *)protocol class:(Class)cls{ if (protocol && cls) { [[[self class] mediatorCache] setObject:cls forKey:NSStringFromProtocol(protocol)]; }}+ (Class)classForProtocol:(Protocol *)protocol{ return [[[self class] mediatorCache] objectForKey:NSStringFromProtocol(protocol)];}//MTMediator.m --- end//被调用//MTDetailViewController.h --- start@protocol MTDetailViewControllerProtocol;@interface MTDetailViewController : UIViewController@end//MTDetailViewController.h --- end//MTDetailViewController.m --- start+ (void)load { [MTMediator registerProtol: @protocol(MTDetailViewControllerProtocol) class:[self class]];}#pragma mark - MTDetailViewControllerProtocol+ ( __kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl{ return [[MTDetailViewController alloc]initWithUrlString:detailUrl];}//MTDetailViewController.m --- end//调用Class cls = [MTMediator classForProtocol: @protocol(MTDetailViewControllerProtocol)];if ([cls respondsToSelector: @selector(detailViewControllerWithUrl:)]) { [self.navigationController pushViewController:[cls detailViewControllerWithUrl:item.articleUrl] animated:YES];}复制代码
protocol比较典型的三方框架就是阿里的BeeHive。BeeHive
借鉴了spring Service、Apache DSO的架构理念,采用aop+扩展App生命周期api
形式,将业务功能
、基础功能
模块以模块方式以解决大型应用中的复杂问题,并让模块之间以Service形式调用
,将复杂问题切分,以AOP方式模块化服务。
BeeHive 核心思想
Service
的形式,避免了直接依赖。AppDelegate
中逻辑拆分,每个模块以微应用的形式独立存在。示例如下:
//******** 1、注册[[BeeHive shareInstance] registerService:@protocol(HomeServiceProtocol) service:[BHViewController class]];//******** 2、使用#import "BHService.h"id< HomeServiceProtocol > homeVc = [[BeeHive shareInstance] createService:@protocol(HomeServiceProtocol)];复制代码
优点
缺点
OC runtime
创建对象,不支持swiftprotocol
和 class
的匹配,不支持更复杂的创建方式 和依赖注入建议:URL Scheme - handler 配合 Protocol - Class 使用
附带:iOS组件化方案架构设计图
来源地址:https://blog.csdn.net/yezuiqingxin/article/details/126018623
--结束END--
本文标题: iOS 组件化的三种方案
本文链接: https://lsjlt.com/news/411271.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0