1.协议
本路由通过协议方式实现
首先定义一个必须实现的方法
typedef void(^JCompletionHandler)(id _Nullable result,NSError *_Nullable error);
@protocol JRouteHandler
@required
+(void)handleRequestWithParameters:(nullable JParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable JCompletionHandler)completionHandler;///< 路由找到路径d后调用该方法
@optional
+(NSString *)routePath; ///< 单路径 可以用这个方法d返回
+(NSArray *)multiRoutePath; ///< 多路径 可以用这个方法返回
@end
2.请求处理
建一个request类处理请求数据
-(instancetype)initWithPath:(NSString *)requestPath parameters:(nullable JParameters)parameters NS_DESIGNATED_INITIALIZER;
@property(nonatomic,strong,readonly)NSString *requestPath;
@property(nonatomic,strong,readonly,nullable)JParameters prts;///<参数
3.最后的路由
.h如下
typedef void(^JRouterUnhandledCallback)(JRouteRequest *request,UIViewController *topViewController);///<无法处理时的回调
@interface JRoute : NSObject
+(instancetype)shared;
-(void)handleRequest:(JRouteRequest*)request completionHandler:(nullable JCompletionHandler)completionHandler;
@property(nonatomic,copy,nullable)JRouterUnhandledCallback unhandledCallBack; /// < 协议方法
-(BOOL)canHandleRoutePath:(NSString *)routePath;/// < 是否可以处理某个路径
@end
topViewController为最初viewcontroller
static UIViewController *_sj_get_top_view_controller(){
UIViewController *vc = UIApplication.sharedApplication.keyWindow.rootViewController;
while ([vc isKindOfClass:[UINavigationController class]] || [vc isKindOfClass:[UITabBarController class]]) {
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = [(UINavigationController *)vc topViewController];
}
if ([vc isKindOfClass:[UITabBarController class]]) {
vc = [(UITabBarController*)vc selectedViewController];
}
if (vc.presentedViewController) {
vc = vc.presentedViewController;
}
}
return vc;
}
找到实现协议的类,并存储下来
-(instancetype)init{
if (self = [super init]) {
_handlersM = [NSMutableDictionary new];
unsigned int img_count = 0;
const char ** imgs = objc_copyImageNames(&img_count);
const char *main = NSBundle.mainBundle.bundlePath.UTF8String;
for (unsigned int i=0; i)cls routePath]] = cls;
}else if ([(id)cls respondsToSelector:@selector(multiRoutePath)]){
for (NSString *rp in [(id)cls multiRoutePath]) {
_handlersM[rp] = cls;
}
}
}
if (classes) {
free(classes);
}
}
if (imgs) {
free(imgs);
}
}
return self;
}
调用路由方法
-(void)handleRequest:(JRouteRequest *)request completionHandler:(JCompletionHandler)completionHandler{
NSParameterAssert(request);
if (!request) return;
Class handler = _handlersM[request.requestPath];
if (handler) {
[handler handleRequestWithParameters:request.prts topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
else {
printf("\n (-_-) Unhandled request:%s",request.description.UTF8String);
if (_unhandledCallBack) {
_unhandledCallBack(request,_sj_get_top_view_controller());
}
}
}
3.使用
首先在路由目标实现路径协议
+(NSString *)routePath{
return @"xx";
}
起始点使用request
JRouteRequest *request = [[JRouteRequest alloc]initWithPath:@"xx" parameters:nil];
[JRoute.shared handleRequest:request completionHandler:nil];
最后在目标实现方法
+ (void)handleRequestWithParameters:(nullable JParameters)parameters topViewController:(nonnull UIViewController *)topViewController completionHandler:(nullable JCompletionHandler)completionHandler {
[topViewController presentViewController:[self new] animated:YES completion:nil];
}