// // RouteManager.m // YouHuiProject // // Created by 小花 on 2018/4/27. // Copyright © 2018年 kuxuan. All rights reserved. // #import "RouteManager.h" #import #import #import @implementation RouteManager + (void)registerRouteWithScheme:(NSString *)scheme{ [self routeManagerAddPushRouteWithScheme:scheme]; } + (void)pushRoutesUrlByDictionary:(NSDictionary *)dict viewControll:(NSString *)viewControll { NSMutableString *paramStr = [NSMutableString string]; if (dict) { for (NSString *key in dict.allKeys) { [paramStr appendFormat:@"%@", [NSString stringWithFormat:@"%@=%@",key,dict[key]]]; [paramStr appendString:@"&"]; } } NSString *routeBase = [NSString stringWithFormat:@"%@://push/%@?",SchemeKey,viewControll]; NSString *url = [NSString stringWithFormat:@"%@%@",routeBase,paramStr]; NSString *newStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newStr]]; } #pragma mark ---- #pragma mark -------------- private ---- /** push Route */ + (void)routeManagerAddPushRouteWithScheme:(NSString *)scheme { //RouteOne://push/FirstNextViewController [[JLRoutes routesForScheme:scheme] addRoute:@"/push/:controller"handler:^BOOL(NSDictionary * _Nonnull parameters) { Class class = NSClassFromString(parameters[@"controller"]); UIViewController *nextVC = [[class alloc] init]; [self paramToVc:nextVC param:parameters]; UIViewController *currentVc = [self currentViewController]; [currentVc.navigationController pushViewController:nextVC animated:YES]; return YES; }]; } #pragma mark ---------- //确定是哪个viewcontroller + (UIViewController *)currentViewController{ UIViewController * currVC = nil; UIWindow *window = [UIApplication sharedApplication].delegate.window; UIViewController * Rootvc = window.rootViewController; do { if ([Rootvc isKindOfClass:[UINavigationController class]]) { UINavigationController * nav = (UINavigationController *)Rootvc; UIViewController * v = [nav.viewControllers lastObject]; currVC = v; Rootvc = v.presentedViewController; continue; }else if([Rootvc isKindOfClass:[UITabBarController class]]){ UITabBarController * tabVC = (UITabBarController *)Rootvc; currVC = tabVC; Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex]; continue; } } while (Rootvc!=nil); return currVC; } //传参数 + (void)paramToVc:(UIViewController *) v param:(NSDictionary *)parameters{ //runtime将参数传递至需要跳转的控制器 unsigned int outCount = 0; objc_property_t * properties = class_copyPropertyList(v.class , &outCount); for (int i = 0; i < outCount; i++) { objc_property_t property = properties[i]; NSString *key = [NSString stringWithUTF8String:property_getName(property)]; NSString *param = parameters[key]; if (param != nil) { [v setValue:param forKey:key]; } } } @end