TheRouter iOS 路由介绍


功能介绍

TheRouter 是货拉拉打造的一款同时支持 AndroidiOS 的轻量级路由中间件,在iOS端吸取了其他其他语言的特性,支持注解功能,极大提升了路由在iOS端的使用体感。摒弃了传统 iOSer 的 target-action 或 protocol 理念,向更广的后台或 Android 应用对齐。

TheRouter 核心功能具备四大能力:

模块描述

.
├── Classes
│   ├── TheRouter+Annotation.h
│   ├── TheRouter+Annotation.m  // 路由注解器及Path功能扩展
│   ├── TheRouter.h
│   └── TheRouter.m             // 路由库核心代码(增删改查,重定向/拦截器)
└── Resources
    └── scan.py                 // 注解扫描及硬编码处理脚本(该脚本只会被引用不会参与编译和打包)

使用介绍

Cocoapods 引入

pod 'TheRouter'

注解使用

step1

创建TheRouterAnnotation.plist文件,必须在MainBundle下。

step2

为项目创建一个Aggregate类型的target:

step3

在新建的target添加脚本:

图中实例脚本参数含义:

python3 $SRCROOT/../TheRouter/Resources/scan.py     # 脚本路径
$SRCROOT/                                           # 参数1:扫描路径,一般为项目根目录
$SRCROOT/TheRouter/                                 # 参数2:路径定义头文件存放目录 一般为存放至公共模块 
$SRCROOT/TheRouter/TheRouterAnnotation.plist        # 参数3:TheRouterAnnotation文件路径

step4

在应用加载完成时注册host,在想要跳转的VC类上添加路由注解或创建对应模块的Service类,在Service中的方法上添加注解即可,例如:

注册该项目的host:

[TheRouter.shared registPathAnnotationsWithHost:@"hd://com.therouter.test"];

添加vc注解:

TheRouterController(test/vc, TestViewController)
@interface TestViewController : UIViewController

@end

添加Service注解:

#import "TestService.h"
#import "TheRouter_Mappings.h"
#import <TheRouter/TheRouter+Annotation.h>

@implementation TestService

TheRouterSelector(test/jump, jumpToTestVC, TestService)
+ (id)jumpToTestVC:(TheRouterInfo *)routerInfo
{
    UIViewController *vc = [TheRouter.shared openVCPath:kRouterPathTestVcVC
                                                    cmd:TheRouterOpenCMDPush
                                             withParams:@{@"title":@"123"}
                                                hanlder:^(NSString * _Nonnull tag, NSDictionary * _Nullable result) {
        !routerInfo.openCompleteHandler ?: routerInfo.openCompleteHandler(tag, result);
    }];
    return vc;
}

@end

step5

在每次对路由进行增删改时编译一次创建好的target,会自动向TheRouterAnnotation.plist文件写入信息,并在指定的目录下生成TheRouter_Mappings.h文件,将此文件拖入对应模块即可

拦截器和重定向

拦截器:

// 只要访问hd://com.therouter.test或其子路径 (hd://com.therouter.test/xxx) 都会进入该回调
// 如果返回YES那么对应的路由事件可以正常执行,反之则会被拦截不会执行路由事件
[TheRouter.shared registInterceptorForURLString:@"hd://com.therouter.test/*" handler:^BOOL(TheRouterInfo * _Nonnull router, id  _Nullable (^ _Nonnull continueHandle)(void)) {
    NSLog(@"will execute router %@", router.URLString);
    return YES;
}];

重定向:

// 重定向是指访问 hd://test.com/test 时会走 hd://test.com/test/vc的事件,用来迁移老路径或线上遇到问题时可快速更改至其他页面承接业务
[TheRouter.shared registRedirect:@"hd://test.com/test" to:@"hd://test.com/test/vc"];

执行路由事件

UIViewController *vc = [TheRouter.shared openVCPath:kRouterPathTestVcVC    // 传入Path
                                                cmd:TheRouterOpenCMDPush   // 指定打开命令
                                         withParams:@{@"title":@"123"}     // 指定参数,这里支持对kvc赋值
                                            hanlder:^(NSString * _Nonnull tag, NSDictionary * _Nullable result) {
        !routerInfo.openCompleteHandler ?: routerInfo.openCompleteHandler(tag, result);
}];

相关推荐:

TheRouterSwift iOS 路由介绍TheRouterSwift iOS 路由介绍

TheRouterSwift是货拉拉TheRouter系列开源框架的Swift版本,为日益增多的Swift开发者提供一高可用路由框架。TheRouterSwift用于模块间解耦和通信,基于Swift协议进行动态懒加载注册路由与打开路由的工具。同时支持通过Service-Protocol寻找对应的模块,并用 protocol进行依赖注入和模块通信。

16 mins
TheRouter 常见问题处理TheRouter 常见问题处理

如无法解决你的问题,建议登记应用,获取优先技术支持 https://github.com/HuolalaTech/hll-wp-therouter-and...

12 mins
动态路由 TheRouter 的设计与实践动态路由 TheRouter 的设计与实践

详细讲解 TheRouter 框架内部实现原理,以及在完整企业级项目中使用的实践效果。这篇文章是我在 2022【GIAC 全球互联网架构大会】分享时所讲内容的文字版本,修改删减了演讲时的冗余言语,现开放给大家阅读,希望能给买不到票参加分享的 开源实验室 读者带来帮助。

1 mins