{"id":16012442,"url":"https://github.com/sun6boys/crrouter","last_synced_at":"2025-03-17T16:30:27.455Z","repository":{"id":56905117,"uuid":"92585784","full_name":"sun6boys/CRRouter","owner":"sun6boys","description":"A simple and powerful router","archived":false,"fork":false,"pushed_at":"2018-12-13T14:56:18.000Z","size":47,"stargazers_count":53,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T12:42:25.064Z","etag":null,"topics":["coupling","ios","objective-c","router","routing","url-router"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sun6boys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-27T08:52:11.000Z","updated_at":"2024-10-17T16:54:54.000Z","dependencies_parsed_at":"2022-08-20T19:20:29.412Z","dependency_job_id":null,"html_url":"https://github.com/sun6boys/CRRouter","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sun6boys%2FCRRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sun6boys%2FCRRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sun6boys%2FCRRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sun6boys%2FCRRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sun6boys","download_url":"https://codeload.github.com/sun6boys/CRRouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244068708,"owners_count":20392879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coupling","ios","objective-c","router","routing","url-router"],"created_at":"2024-10-08T14:03:24.694Z","updated_at":"2025-03-17T16:30:26.895Z","avatar_url":"https://github.com/sun6boys.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"CRRouter\n====\n\n![License MIT](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)\n![](https://img.shields.io/badge/language-objective-orange.svg)\n![](https://img.shields.io/cocoapods/v/CRRouter.svg?style=flat)\n\n\n## CRRouter是什么\nCRRouter是一个利用URL注册，通过URL获取对象或者打开一个页面的模块解耦框架。\r\n## CRRouter提供了哪些功能\n\n - 通过一个URL获取一个对象\n - 通过一个URL打开相应页面\n - 参数验证\n - 参数映射\n\n## 安装\n### CocoaPods\n\n1. 在Podfile中添加 pod \"CRRouter\".\n2. 输入命令 `pod install` 或者 `pod update`.\n3. Import \\\u003cCRRouter/CRRouter.h\\\u003e.\n\n## 如何使用\n### 注册\n**CRRouter**提供了2种注册方式\n\n```\n//第一种注册方式\nCRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@\"cr\" URLHost:@\"goods\" URLPath:@\"/goodsDetail\"];\n[CRRouter registRouteNode:routeNode];\n```\n\n```\n//第二种注册方式\n[CRRouter registURLPattern:@\"cr://goods/goodsDetail\"]\n```\n### 参数映射\n有时候我们需要从第三方app中打开我们app，并且进入某个页面。我们需要给他们提供scheme、host、path以及所需的参数，比如`cr://goods/goodsDetail?goodsId=12389`，但是我们不想告诉第三方页面所需要的真正参数名，所以我们告诉他们通过`cr://goods/goodsDetail?p1=12389`来打开商品详情页面，而我们真正需要的参数名是`goodsId`,**CRRouter**提供了下面的方式来映射参数。\n\n```\nCRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@\"cr\" URLHost:@\"goods\" URLPath:@\"/goodsDetail\"];\n\n[routeNode paramsMap:^NSDictionary *(NSDictionary *originParams) {\n    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];\n    if(originParams[@\"p1\"]){\n        //说明是第三方调用\n        temp[@\"goodsId\"] = originParams[@\"p1\"];\n     }\n    [temp addEntriesFromDictionary:originParams];\n    return temp;\n }];\n```\n\nSo,对于我们内部仍然可以通过`cr://goods/goodsDetail?goodsId=12389`来调用，而第三方通过`cr://goods/goodsDetail?p1=12389`也可以调用到相应的模块\n\n### 参数验证\n以上面为例，打开一个商品详情页，一个goodsId是必须的，**CRRouter**提供相应的参数验证\n\n```\nCRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@\"cr\" URLHost:@\"goods\" URLPath:@\"/goodsDetail\"];\n\n[routeNode paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {\n     NSString *goodsId = routeParams[@\"goodsId\"];\n     return goodsId.length \u003e 0;\n }];\n```\n如果参数验证失败，将不会继续执行下去。**如果实现了参数映射的block，那验证的是映射后的参数**\n\n### 实现注册的URL相应的获取实例操作\n```\nCRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@\"cr\" URLHost:@\"goods\" URLPath:@\"/goodsDetail\"];\n\n[routeNode objectHandler:^id(NSDictionary *routeParams) {\n     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];\n     goodsVC.goodsId = routeParams[@\"goodsId\"];\n     return goodsVC;\n }];\n```\n如果实现了参数验证，如果这个block被调用，那肯定是参数验证通过了。\n\n### 实现注册的URL打开页面的操作\n```\nCRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@\"cr\" URLHost:@\"goods\" URLPath:@\"/goodsDetail\"];\n\n[routeNode openHandler:^(NSDictionary *routeParams) {\n     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];\n     goodsVC.goodsId = routeParams[@\"goodsId\"];\n     UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;\n     [navigationVC pushViewController:goodsVC animated:YES];\n }];\n```\n如果实现了参数验证，如果这个block被调用，那肯定是参数验证通过了。\n\n### 如何调用\n**通过URL获取一个实例**\n\n```\nUIViewController *vc = [CRRouter objectForURL:@\"cr://goods/goodsDetail?p1=1\"];\n```\n如果一些参数不方便通过URL query传递（比如block）可以用下面的方式获取实例\n\n```\ndispatch_block_t completionHandler = ^{\n        \n};\nUIViewController *vc = [CRRouter objectForURL:@\"cr://goods/goodsDetail?goodsId=1\" withParams:@{@\"block\" : completionHandler}];\n```\n**通过URL打开一个页面**\n\n```\n[CRRouter openURL:@\"cr://goods/goodsDetail?p1=1\"];\n```\n或\n\n```\ndispatch_block_t completionHandler = ^{\n        \n};\n[CRRouter openURL:@\"cr://goods/goodsDetail?goodsId=1\" withParams:@{@\"block\" : completionHandler}];\n```\n\n### 支持链式语法\n如果你不喜欢上面的调用方式，**CRRouter**也提供类似[ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa)那样的调用方式\n\n```\n[[[[[CRRouter registURLPattern:@\"cr://goods/goodsDetail\"] paramsMap:^NSDictionary *(NSDictionary *originParams) {\n        \n    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];\n    temp[@\"goodsId\"] = originParams[@\"p1\"];\n    [temp addEntriesFromDictionary:originParams];\n    return temp;\n        \n}] paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {\n        \n    NSString *goodsId = routeParams[@\"goodsId\"];\n    return goodsId.length \u003e 0;\n        \n}] objectHandler:^id(NSDictionary *routeParams) {\n        \n    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];\n    goodsVC.goodsId = routeParams[@\"goodsId\"];\n    return goodsVC;\n        \n}] openHandler:^(NSDictionary *routeParams) {\n        \n    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];\n    goodsVC.goodsId = routeParams[@\"goodsId\"];\n    UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;\n    [navigationVC pushViewController:goodsVC animated:YES];\n        \n}];\n\n```\n\n## TODO\n 1. [x] 添加参数验证\n 2. [x] 添加参数映射\n 3. [x] 通过URL可以打开一个页面\n 4. [x] 通过URL可以获取一个对象\n 5. [ ] 通过URL可以调用相应Target的Action\n\n\n 如果大家有好的建议也可以通过Issues的方式或者[邮件](mailto:sun6boys@126.com)的方式告诉我。\n \n  \n## 协议\nCRRouter被许可在 MIT 协议下使用。查阅 LICENSE 文件来获得更多信息。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsun6boys%2Fcrrouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsun6boys%2Fcrrouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsun6boys%2Fcrrouter/lists"}