所以点击跳转音乐模块,可以替换成如下操作, 发起请求:
SJRouteRequest *request = [[SJRouteRequest alloc] initWithPath:@"audio/playbackInfo" parameters:@{@"audio_id":@(232)}];
[SJRouter.shared handleRequest:request completionHandler:^(id _Nullable result, NSError * _Nullable error) {
#ifdef DEBUG
NSLog(@"%d - %s", (int)__LINE__, __func__);
#endif
}];
router找到对应的handler, 让其进行处理。
Handler
从开始到现在,可以看出Handler就是最终执行请求的那个家伙。 相信大家都有疑问, 如何成为一个Handler?
很简单,它是自动的(参见Router), 只要某个类遵守了SJRouteHandlerProtocol, 它便成为了一个Handler。再来看一遍协议吧。
NS_ASSUME_NONNULL_BEGIN
typedef id SJParameters;
@protocol SJRouteHandler
+ (NSString *)routePath;
+ (void)handleRequestWithParameters:(nullable SJParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable SJCompletionHandler)completionHandler;
@end
NS_ASSUME_NONNULL_END
- routePath: 即路径, 表示handler能够处理的路径。当发起请求时, Router会通过路径获取到对应的handler, 交给其进行处理。
- handleRequestWithParameters。。。: handler进行的处理。
Router
在整个请求过程中,Router做的事情实质上就是在众多Handler中寻找命中注定的那一个。如何寻找呢?为什么遵守了SJRouteHandlerProtocol便自动成为了Handler呢?










