『ios』JLRoutes 路由组件化学习

之前看了下源码,现在觉得再看原来的api方法简直无比畅快啊~

第一种 初始化 tabbar

    //Tabbar规则
    [JLRoutes addRoute:@"/Tabbar/:tabVC1/:tabVC2/:tabVC3" handler:^BOOL(NSDictionary * _Nonnull parameters) {

        customTabbarVC.viewControllers = @[
                                           [self viewControllerWithTitle:@"test1" image:IMG(@"icon_home.png") selectImage:IMG(@"icon_home_select.png")  VC:[[NSClassFromString(parameters[@"tabVC1"]) alloc] init]],
                                           [self viewControllerWithTitle:@"test2" image:IMG(@"icon_cheyuan.png") selectImage:IMG(@"icon_cheyuan_select.png")  VC:[[NSClassFromString(parameters[@"tabVC2"]) alloc] init]],
                                           [self viewControllerWithTitle:@"test3" image:IMG(@"icon_search.png") selectImage:IMG(@"icon_search_select.png")  VC:[[NSClassFromString(parameters[@"tabVC3"]) alloc] init]],
                                           ];
        return YES;
    }];

如果要想让包跑起来,只需要调用相关 host就可以了。

     NSString *customURL = @"JLRouteDemo://Tabbar/ViewController/SecondViewController/ThirdViewController";
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];

如果想从外界打开app,只需要在下图配置


image.png

调用JLRouteDemo://就可以打开app。

第二种 页面跳转

    [[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;
    }];
//传参数
-(void)paramToVc:(UIViewController *) v param:(NSDictionary *)parameters{
    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];
        }
    }
}

字典转url url转字典

+ (NSDictionary *)parseParamsWithURL:(NSURL *)URL
{
    NSString *parameterString = URL.query;
    NSMutableDictionary *parameterDic = [NSMutableDictionary new];
    if (parameterString && parameterString.length > 0) {
        
        NSArray *kvPart = [parameterString componentsSeparatedByString:@"&"];
        for (NSString * kv in kvPart) {
            
            NSArray *kvArr = [kv componentsSeparatedByString:@"="];
            if (kvArr.count == 2) {
                
                [parameterDic setObject:[kvArr lastObject] forKey:[kvArr firstObject]];
            }
        }
        
    }
    return [NSDictionary dictionaryWithDictionary:parameterDic];
}

+ (NSString *)mapDictionaryToURLQueryString:(NSDictionary *)dic
{
    if (!dic) {
        return @"";
    }
    NSURLComponents *components = [[NSURLComponents alloc] init];
    NSMutableArray *queryItems = [NSMutableArray array];
    
    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        NSString *queryKey = [NSString stringWithFormat:@"%@", key];
        NSString *queryValue = [NSString stringWithFormat:@"%@", obj];
        NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:queryKey value:queryValue];
        [queryItems addObject:item];
    }];
    components.queryItems = [queryItems copy];
    
    return components.URL.absoluteString;
}

这里面有个必要重要的类 NSURLComponents

@property (nullable, copy) NSString *scheme; // Attempting to set the scheme with an invalid scheme string will cause an exception.
@property (nullable, copy) NSString *user;
@property (nullable, copy) NSString *password;
@property (nullable, copy) NSString *host;
@property (nullable, copy) NSNumber *port; // Attempting to set a negative port number will cause an exception.
@property (nullable, copy) NSString *path;
@property (nullable, copy) NSString *query;
@property (nullable, copy) NSString *fragment;

比如说我们dic传入@{@"test":@"1"}
则 打印 components 为

 {scheme = (null), user = (null), password = (null), host = (null), port = (null), path = , query = test=3, fragment = (null)}

打印 components.URL.absoluteString

?test=3

通过上面的应用,再来分析一波这个url

NSString *url = @"RouteOne://push/FirstNextViewController?titleText=fromFirst&name=中国";

首先 RouteOne是协议
/push/ 在这里相当于host
FirstNextViewController是根文件

不管是 [JLRoutes addRoute:@"/Tabbar/:tabVC1/:tabVC2/:tabVC3" ] 还是 [[JLRoutes routesForScheme:scheme] addRoute:@"/push/:controller"];

我们看到 比如 :tabVC1 还有 :controller.这都是相当于是参数。
:controller对应着 FirstNextViewController

上面的host 是Tabbar 跟 push。说到这里理清了没有? 刚开始自己也是没理解,后面写了一些demo才理顺了。总之多写,多练。写多了自然懂了

你可能感兴趣的:(『ios』JLRoutes 路由组件化学习)