省钱达人

RouteManager.m 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // RouteManager.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/4/27.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "RouteManager.h"
  9. #import <UIKit/UIKit.h>
  10. #import <JLRoutes.h>
  11. #import <objc/runtime.h>
  12. @implementation RouteManager
  13. + (void)registerRouteWithScheme:(NSString *)scheme{
  14. [self routeManagerAddPushRouteWithScheme:scheme];
  15. }
  16. + (void)pushRoutesUrlByDictionary:(NSDictionary *)dict viewControll:(NSString *)viewControll {
  17. NSMutableString *paramStr = [NSMutableString string];
  18. if (dict) {
  19. for (NSString *key in dict.allKeys) {
  20. [paramStr appendFormat:@"%@", [NSString stringWithFormat:@"%@=%@",key,dict[key]]];
  21. [paramStr appendString:@"&"];
  22. }
  23. }
  24. NSString *routeBase = [NSString stringWithFormat:@"%@://push/%@?",SchemeKey,viewControll];
  25. NSString *url = [NSString stringWithFormat:@"%@%@",routeBase,paramStr];
  26. NSString *newStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  27. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newStr]];
  28. }
  29. #pragma mark ----
  30. #pragma mark -------------- private ----
  31. /**
  32. push Route
  33. */
  34. + (void)routeManagerAddPushRouteWithScheme:(NSString *)scheme {
  35. //RouteOne://push/FirstNextViewController
  36. [[JLRoutes routesForScheme:scheme] addRoute:@"/push/:controller"handler:^BOOL(NSDictionary<NSString *,id> * _Nonnull parameters) {
  37. Class class = NSClassFromString(parameters[@"controller"]);
  38. UIViewController *nextVC = [[class alloc] init];
  39. [self paramToVc:nextVC param:parameters];
  40. UIViewController *currentVc = [self currentViewController];
  41. [currentVc.navigationController pushViewController:nextVC animated:YES];
  42. return YES;
  43. }];
  44. }
  45. #pragma mark ----------
  46. //确定是哪个viewcontroller
  47. + (UIViewController *)currentViewController{
  48. UIViewController * currVC = nil;
  49. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  50. UIViewController * Rootvc = window.rootViewController;
  51. do {
  52. if ([Rootvc isKindOfClass:[UINavigationController class]]) {
  53. UINavigationController * nav = (UINavigationController *)Rootvc;
  54. UIViewController * v = [nav.viewControllers lastObject];
  55. currVC = v;
  56. Rootvc = v.presentedViewController;
  57. continue;
  58. }else if([Rootvc isKindOfClass:[UITabBarController class]]){
  59. UITabBarController * tabVC = (UITabBarController *)Rootvc;
  60. currVC = tabVC;
  61. Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
  62. continue;
  63. }
  64. } while (Rootvc!=nil);
  65. return currVC;
  66. }
  67. //传参数
  68. + (void)paramToVc:(UIViewController *) v param:(NSDictionary<NSString *,NSString *> *)parameters{
  69. //runtime将参数传递至需要跳转的控制器
  70. unsigned int outCount = 0;
  71. objc_property_t * properties = class_copyPropertyList(v.class , &outCount);
  72. for (int i = 0; i < outCount; i++) {
  73. objc_property_t property = properties[i];
  74. NSString *key = [NSString stringWithUTF8String:property_getName(property)];
  75. NSString *param = parameters[key];
  76. if (param != nil) {
  77. [v setValue:param forKey:key];
  78. }
  79. }
  80. }
  81. @end