HHRouter解析

关于App内部的路由设计,主要需要解决2个问题:
【HHRouter解析】1.各个页面和组件之间的跳转问题。
2.各个组件之间相互调用。
HHRouter从GitHub上获取源码
这是布丁动画的一个Router,灵感来自于ABRouter和Routable iOS。
先来看看HHRouter的Api。它提供的方法非常清晰。
ViewController提供了2个方法。map是用来设置路由规则,matchController是用来匹配路由规则的,匹配争取之后返回对应的UIViewController。

-(void)map:(NSString*)route toControllerClass:(Class)controllerClass; -(UIViewController*)matchController:(NSString*)route;

block闭包提供了三个方法,map也是设置路由规则,matchBlock:是用来匹配路由,找到指定的block,但是不会调用该block。callBlock:是找到指定的block,找到以后就立即调用。
-(void)map:(NSString*)route toBlock:(HHRouterBlock)block; -(HHRouterBlock)matchBlock:(NSString*)route; -(id)callBlock:(NSString*)route;

matchBlock:和callBlock:的区别就在于前者不会自动调用闭包。所以matchBlock:方法找到对应的block之后,如果想调用,需要手动调用一次。
除去上面这些方法,HHRouter还为我们提供了一个特殊的方法。
-(HHRouteType)canRoute:(NSString*)route;

这个方法就是用来找到执行路由规则对应的RouteType,RouteType总共就3种:
typedefNS_ENUM(NSInteger,HHRouteType){ HHRouteTypeNone=0, HHRouteTypeViewController=1, HHRouteTypeBlock=2 };

再来看看HHRouter是如何管理路由规则的。整个HHRouter就是由一个NSMutableDictionary *routes控制的。
@interfaceHHRouter()@property(strong,nonatomic)NSMutableDictionary*routes; @end

HHRouter解析
文章图片
image 别看只有这一个看似“简单”的字典数据结构,但是HHRouter路由设计的还是很精妙的。
-(void)map:(NSString*)route toBlock:(HHRouterBlock)block{NSMutableDictionary*subRoutes=[selfsubRoutesToRoute:route]; subRoutes[@"_"]=[block copy]; }-(void)map:(NSString*)route toControllerClass:(Class)controllerClass{NSMutableDictionary*subRoutes=[selfsubRoutesToRoute:route]; subRoutes[@"_"]=controllerClass; }

上面两个方法分别是block闭包和ViewController设置路由规则调用的方法实体。不管是ViewController还是block闭包,设置规则的时候都会调用subRoutesToRoute:方法。
-(NSMutableDictionary*)subRoutesToRoute:(NSString*)route{NSArray*pathComponents=[selfpathComponentsFromRoute:route]; NSInteger index=0; NSMutableDictionary*subRoutes=self.routes; while(index

上面这段函数就是来构造路由匹配规则的字典。
举个例子:
[[HHRouter shared]map:@"/user/:userId/"toControllerClass:[UserViewController class]]; [[HHRouter shared]map:@"/story/:storyId/"toControllerClass:[StoryViewController class]]; [[HHRouter shared]map:@"/user/:userId/story/?a=0"toControllerClass:[StoryListViewController class]];

设置3条规则以后,按照上面构造路由匹配规则的字典的方法,该路由规则字典就会变成这个样子:
{story={":storyId"={"_"=StoryViewController; }; }; user={":userId"={"_"=UserViewController; story={"_"=StoryListViewController; }; }; }; }

路由规则字典生成之后,等到匹配的时候就会遍历这个字典。
假设这时候有一条路由过来:
[[[HHRouter shared]matchController:@"hhrouter20://user/1/"]class];

HHRouter对这条路由的处理方式是先匹配前面的scheme,如果连scheme都不正确的话,会直接导致后面匹配失败。
然后再进行路由匹配,最后生成的参数字典如下:
{"controller_class"=UserViewController; route="/user/1/"; userId=1; }

具体的路由参数匹配的函数在
-(NSDictionary*)paramsInRoute:(NSString*)route

这个方法里面实现的。这个方法就是按照路由匹配规则,把传进来的URL的参数都一一解析出来,带?号的也都会解析成字典。这个方法没什么难度,就不在赘述了。
ViewController 的字典里面默认还会加上2项:
"controller_class"=
route=
route里面都会保存传过来的完整的URL。
如果传进来的路由后面带访问字符串呢?那我们再来看看:
[[HHRouter shared]matchController:@"/user/1/?a=b&c=d"];

那么解析出所有的参数字典会是下面的样子:
{a=b; c=d; "controller_class"=UserViewController; route="/user/1/?a=b&c=d"; userId=1; }

同理,如果是一个block闭包的情况呢?
还是先添加一条block闭包的路由规则:
[[HHRouter shared]map:@"/user/add/"toBlock:^id(NSDictionary*params){}];

这条规则对应的会生成一个路由规则的字典。
{story={":storyId"={ "_"=StoryViewController; }; }; user={":userId"={"_"=UserViewController; story={"_"=StoryListViewController; }; }; add={"_"="<__NSMallocBlock__: 0x600000240480>"; }; }; }

注意”_”后面跟着是一个block。
匹配block闭包的方式有两种。
// 1.第一种方式匹配到对应的block之后,还需要手动调用一次闭包。HHRouterBlock block=[[HHRouter shared]matchBlock:@"/user/add/?a=1&b=2"]; block(nil); // 2.第二种方式匹配block之后自动会调用改闭包。[[HHRouter shared]callBlock:@"/user/add/?a=1&b=2"];
匹配出来的参数字典是如下:
{a=1; b=2; block="<__NSMallocBlock__: 0x600000056b90>"; route="/user/add/?a=1&b=2"; }

block的字典里面会默认加上下面这2项:
block=
route=
route里面都会保存传过来的完整的URL。
生成的参数字典最终会被绑定到ViewController的Associated Object关联对象上。
-(void)setParams:(NSDictionary*)paramsDictionary{objc_setAssociatedObject(self,&kAssociatedParamsObjectKey,paramsDictionary,OBJC_ASSOCIATION_RETAIN_NONATOMIC); }-(NSDictionary*)params{returnobjc_getAssociatedObject(self,&kAssociatedParamsObjectKey); }

这个绑定的过程是在match匹配完成的时候进行的。
-(UIViewController*)matchController:(NSString*)route{NSDictionary*params=[selfparamsInRoute:route]; Class controllerClass=params[@"controller_class"]; UIViewController*viewController=[[controllerClass alloc]init]; if([viewController respondsToSelector:@selector(setParams:)]){[viewController performSelector:@selector(setParams:)withObject:[params copy]]; }returnviewController; }

最终得到的ViewController也是我们想要的。相应的参数都在它绑定的params属性的字典里面。
将上述过程图解出来,如下:
HHRouter解析
文章图片
image 参考:https://halfrost.com/ios_router/

    推荐阅读