CRRouter 0.1.1

CRRouter 0.1.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2018年12月

Charon 维护。



CRRouter 0.1.1

  • Charon

CRRouter

License MIT

CRRouter是什么

CRRouter是一个通过URL注册,利用URL获取对象或打开页面的解耦模块框架。

CRRouter提供了哪些功能

  • 通过一个URL获取一个对象
  • 通过一个URL打开相应页面
  • 参数验证
  • 参数映射

安装

CocoaPods

  1. 在 Podfile 中添加 pod "CRRouter".
  2. 请输入命令 pod installpod update
  3. 导入 <CRRouter/CRRouter.h>

如何使用

注册

CRRouter提供了两种注册方式

//第一种注册方式
CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];
[CRRouter registRouteNode:routeNode];
//第二种注册方式
[CRRouter registURLPattern:@"cr://goods/goodsDetail"]

参数映射

有时候我们需要从第三方app中打开我们的app,并进入某个页面。我们需要提供给他们scheme、host、path以及所需的参数,例如cr://goods/goodsDetail?goodsId=12389,但我们不想告诉第三方页面所需的真正参数名,所以我们告诉他们通过cr://goods/goodsDetail?p1=12389来打开商品详情页面,而我们真正需要的参数名是goodsIdCRRouter提供了以下方式来进行参数映射。

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode paramsMap:^NSDictionary *(NSDictionary *originParams) {
    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
    if(originParams[@"p1"]){
        //说明是第三方调用
        temp[@"goodsId"] = originParams[@"p1"];
     }
    [temp addEntriesFromDictionary:originParams];
    return temp;
 }];

因此,我们内部仍然可以通过cr://goods/goodsDetail?goodsId=12389进行调用,而第三方通过cr://goods/goodsDetail?p1=12389也可以调用到相应的模块

参数验证

以上面的例子,打开一个商品详情页,一个goodsId是必须的,CRRouter提供了相应的参数验证

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {
     NSString *goodsId = routeParams[@"goodsId"];
     return goodsId.length > 0;
 }];

如果参数验证失败,将不会继续执行。如果实现了参数映射的block,那验证的是映射后的参数

实现注册的URL相应的获取实例操作

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode objectHandler:^id(NSDictionary *routeParams) {
     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
     goodsVC.goodsId = routeParams[@"goodsId"];
     return goodsVC;
 }];

如果实现了参数验证,如果这个block被调用,那肯定是通过了参数验证。

实现注册的URL打开页面的操作

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode openHandler:^(NSDictionary *routeParams) {
     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
     goodsVC.goodsId = routeParams[@"goodsId"];
     UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
     [navigationVC pushViewController:goodsVC animated:YES];
 }];

如果实现了参数验证,如果这个block被调用,那肯定是通过了参数验证。

如何调用

通过URL获取一个实例

UIViewController *vc = [CRRouter objectForURL:@"cr://goods/goodsDetail?p1=1"];

如果一些参数不方便通过URL查询传递(比如block)可以使用以下方式获取实例

dispatch_block_t completionHandler = ^{
        
};
UIViewController *vc = [CRRouter objectForURL:@"cr://goods/goodsDetail?goodsId=1" withParams:@{@"block" : completionHandler}];

通过URL打开一个页面

[CRRouter openURL:@"cr://goods/goodsDetail?p1=1"];

dispatch_block_t completionHandler = ^{
        
};
[CRRouter openURL:@"cr://goods/goodsDetail?goodsId=1" withParams:@{@"block" : completionHandler}];

支持链式语法

如果你不喜欢上面的调用方式,“CRRouter”也提供类似ReactiveCocoa的调用方式

[[[[[CRRouter registURLPattern:@"cr://goods/goodsDetail"] paramsMap:^NSDictionary *(NSDictionary *originParams) {
        
    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
    temp[@"goodsId"] = originParams[@"p1"];
    [temp addEntriesFromDictionary:originParams];
    return temp;
        
}] paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {
        
    NSString *goodsId = routeParams[@"goodsId"];
    return goodsId.length > 0;
        
}] objectHandler:^id(NSDictionary *routeParams) {
        
    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
    goodsVC.goodsId = routeParams[@"goodsId"];
    return goodsVC;
        
}] openHandler:^(NSDictionary *routeParams) {
        
    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
    goodsVC.goodsId = routeParams[@"goodsId"];
    UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [navigationVC pushViewController:goodsVC animated:YES];
        
}];

TODO

  1. 添加参数验证
  2. 添加参数映射
  3. 通过URL可以打开一个页面
  4. 通过URL可以获取一个对象
  5. 通过URL可以调用相应Target的Action

如果您有好的建议,也可以通过邮件或者 Issues 的方式告诉我。

协议

CRRouter遵循 MIT 协议许可使用。更多信息请查阅 LICENSE 文件。