TheRouter
特性
TheRouter 是 Android 和 iOS 轻量级路由中间件,iOS 端吸收了其他语言特性,支持 注解 功能,极大地提高了在 iOS 中使用路由的感觉。不再像传统的 ioser target-action 或协议概念,而是与更广泛的应用程序背景或 Android 应用程序保持一致。
TheRouter 核心功能有四个主要能力
- 页面导航跳转能力:支持常规 vc 或 Storyboard 推送/present 跳转能力;
- 自动注册能力:类似于 Java 注解功能,通过标记 vc 类或任何方法即可完成路由注册;
- 硬编码消除:内置脚本自动将已注册路径转换为业务使用的静态字符串常量;
- 动态能力:支持添加权重、拦截器等;
模块描述
.
├── Classes
│ ├── TheRouter+Annotation.h
│ ├── TheRouter+Annotation.m // Route annotator and Path functionality extension
│ ├── TheRouter.h
│ └── TheRouter.m // Routing library core code (add, delete, check, redirect/interceptor)
└── Resources
└── scan.py // Annotation scanning and hardcoded processing scripts (this script will only be referenced and will not be involved in compilation and packaging)
介绍
Cocoapods 导入
pod 'TheRouter'
使用注释
步骤1
创建 TheRouterAnnotation.plist
文件,必须在主Bundle下。
步骤2
为项目创建一个类型为Aggregate的目标
步骤3
向新创建的目标添加一个脚本
示例脚本参数
python3 $SRCROOT/../TheRouter/Resources/scan.py # Script path
$SRCROOT/ # Parameter 1: Scan path, usually the project root
$SRCROOT/TheRouter/ # Parameter 2: Path definition header file storage directory is generally stored in the public module
$SRCROOT/TheRouter/TheRouterAnnotation.plist # Parameter 3: TheRouterAnnotation file path
步骤4
当应用程序加载时,注册宿主,为你要跳转的VC类添加路由注释,或为相应模块创建Service类,并在Service中的方法上添加注释,例如
注册项目宿主
[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
步骤5
每次编译创建新目标时,都会自动向TheRouterAnnotation.plist
中增加权限。该文件将写入信息,并在指定目录中生成TheRouter_Mappings.h
文件,该文件包含对应模块
拦截器和重定向
拦截器
// As long as access to hd://com.therouter.test or its path (hd://com.therouter.test/xxx) will enter the callback
// If YES is returned, the corresponding route event can be executed; otherwise, the route event will be blocked and not executed
[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;
}];
重定向
// Redirection refers to the hd://test.com/test/vc event when visiting hd://test.com/test, which is used to migrate the old path or quickly change to another page to take on the business when encountering problems on production environment
[TheRouter.shared registRedirect:@"hd://test.com/test" to:@"hd://test.com/test/vc"];
执行路由事件
UIViewController *vc = [TheRouter.shared openVCPath:kRouterPathTestVcVC // Passing Path
cmd:TheRouterOpenCMDPush // Specifying the open command
withParams:@{@"title":@"123"} // Specifies the parameters.Assignment to kvc is supported here
hanlder:^(NSString * _Nonnull tag, NSDictionary * _Nullable result) {
!routerInfo.openCompleteHandler ?: routerInfo.openCompleteHandler(tag, result);
}];
作者
许可协议
TheRouter 在 Apache2.0 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。