iOS 模块化之JLRoute路由示例

2020-01-21 05:09:46王振洲

 

2.路由解析(这些解析跟我们设置路由的规则有直接关系)

(1)判断接口URL是否设置可选性URL并将对应的URL封装成JLRRouteDefinition对象

(2)将JLRRouteDefinition对象装载进一个可变数组,内存保留了所有的对象!!

(JLRRouteDefinition对象包括有路径,参数解析,block等信息)

单一的Scheme调用过程:

1.调用URL


+ (BOOL)routeURL:(NSURL *)URL

2.解析URL,将参数,路由信息封装成JLRRouteRequest对象

 

复制代码
- (instancetype)initWithURL:(NSURL *)URL alwaysTreatsHostAsPathComponent:(BOOL)alwaysTreatsHostAsPathComponent

 

3.给JLrouteRequest对象和路由数组里的JLRRouteDefinition对象作比对,并且返回JLRRouteResponse 对象抽出参数和URL在数组里

 

复制代码
JLRRouteResponse *response = [route routeResponseForRequest:request decodePlusSymbols:shouldDecodePlusSymbols];

 

4.调用JLRRouteResponse 对象里面的回调方法


[route callHandlerBlockWithParameters:finalParameters];

JLRoutes的URL注册规则:

iOS,JLRoute路由,JLRoute

1.普通注册


JLRoutes *routes = [JLRoutes globalRoutes];
[routes addRoute:@"/user/view/:userID" handler:^BOOL(NSDictionary *parameters) {
NSString *userID = parameters[@"userID"]; // defined in the route by specifying ":userID"
// present UI for viewing user with ID 'userID'
return YES; // return YES to say we have handled the route
}];

URL里,分号表示这个是参数

另外一种注册方式,下标注册法


JLRoutes.globalRoutes[@"/route/:param"] = ^BOOL(NSDictionary *parameters) {
// ...
};

如何按照以上的方式注册,在任何时刻(包括在其它的APP)你都可以调用这个URL。


NSURL *viewUserURL = [NSURL URLWithString:@"myapp://user/view/joeldev"];
[[UIApplication sharedApplication] openURL:viewUserURL];

在这个例子中,在parmameters字典里面的userID会传给block,它是一个键值对。”userID”: “joeldev”。给UI层或者任何需要它的地方用的。

字典参数:

字典参数总包括至少一下3个键:


{
"JLRouteURL": "(the NSURL that caused this block to be fired)",
"JLRoutePattern": "(the actual route pattern string)",
"JLRouteScheme": "(the route scheme, defaults to JLRoutesGlobalRoutesScheme)"
}