组件化—–iOS组件化库JLRoutes

前言

本文将会描述JLRoutes学习理解!!具体运用到了JLRoutes框架来做路由配置和豆瓣的FRDModuleManager来做模块分布!框架适合3-5人开发的中型项目。

本文学习过程

1.JLRoutes的认识和使用示范;

2.FRDModuleManager的认识;

3.项目搭建和使用示范;

1.JLRoutes的认识和使用示范

JLRoutes是一个带有简单的block回调API的URL路由库。用于在你的应用里用少量的代码简单处理复杂URLschemes。

请下载Demo查看并同步学习:

https://github.com/WuChuming/JLRouteDemo

JLRoutes的工作流程和原理:

单一的Scheme注册过程:

1.调用注册方法(用户注册routePattern,默认优先级0)
- (void)addRoute:(NSString *)routePattern handler:(BOOL (^__nullable)(NSDictionary<NSString *, id> *parameters))handlerBlock;
2.路由解析(这些解析跟我们设置路由的规则有直接关系)

1.判断接口URL是否设置可选性URL并将对应的URL封装成JLRRouteDefinition对象

2.将JLRRouteDefinition对象装载进一个可变数组,内存保留了所有的对象!!

(JLRRouteDefinition对象包括有路径,参数解析,block等信息)

单一的Scheme调用过程:

1.调用URL
+ (BOOL)routeURL:(NSURL *)URL

2.解析URL,将参数,路由信息封装成JLRRouteRequest对象

- (instancetype)initWithURL:(NSURL *)URL alwaysTreatsHostAsPathComponent:(BOOL)alwaysTreatsHostAsPathComponent

3.给JLrouteRequest对象和路由数组里的JLRRouteDefinition对象作比对,并且返回JLRRouteResponse 对象抽出参数和URL在数组里

JLRRouteResponse *response = [route routeResponseForRequest:request decodePlusSymbols:shouldDecodePlusSymbols];

4.调用JLRRouteResponse 对象里面的回调方法

[route callHandlerBlockWithParameters:finalParameters];

流程图如下:

注册:

调用:

JLRoutes的URL注册规则:

1.普通注册

JLRoutes *routes = [JLRoutes globalRoutes];
 [routes addRoute:@"/user/view/:userID" handler:^BOOL(NSDictionary *parameters) {
   NSString *userID = parameters[@"userID"]; // defined in the route by specifying ":userID"
   // present UI for viewing user with ID 'userID'
   return YES; // return YES to say we have handled the route
 }];

URL里,分号表示这个是参数

另外一种注册方式,下标注册法

JLRoutes.globalRoutes[@"/route/:param"] = ^BOOL(NSDictionary *parameters) {
  // ...
};

如何按照以上的方式注册,在任何时刻(包括在其它的APP)你都可以调用这个URL。

NSURL *viewUserURL = [NSURL URLWithString:@"myapp://user/view/joeldev"];
[[UIApplication sharedApplication] openURL:viewUserURL];

在这个例子中,在parmameters字典里面的userID会传给block,它是一个键值对。”userID”: “joeldev”。给UI层或者任何需要它的地方用的。

字典参数:

字典参数总包括至少一下3个键:

{
  "JLRouteURL":  "(the NSURL that caused this block to be fired)",
  "JLRoutePattern": "(the actual route pattern string)",
  "JLRouteScheme": "(the route scheme, defaults to JLRoutesGlobalRoutesScheme)"
}

处理Block

你会发现,每个注册的block都会返回一个YES。这个值,如果你返回NO,JLRoutes会跳过这个匹配,然后继续去匹配其它的。

如果你的block设置成nil,它会默认返回YES。

 

2.复杂注册

[[JLRoutes globalRoutes] addRoute:@"/:object/:action/:primaryKey" handler:^BOOL(NSDictionary *parameters) {
  NSString *object = parameters[@"object"];
  NSString *action = parameters[@"action"];
  NSString *primaryKey = parameters[@"primaryKey"];
  // stuff
  return YES;
}];

这个地址会被匹配很多URL,如/user/view/joeldev or /post/edit/123。这些URL上的是参数。

NSURL *editPost = [NSURL URLWithString:@"myapp://post/edit/123?debug=true&foo=bar"];
[[UIApplication sharedApplication] openURL:editPost];

这时,pramater字典就会是以下这样的(传参)

{
  "object": "post",
  "action": "edit",
  "primaryKey": "123",
  "debug": "true",
  "foo": "bar",
  "JLRouteURL": "myapp://post/edit/123?debug=true&foo=bar",
  "JLRoutePattern": "/:object/:action/:primaryKey",
  "JLRouteScheme": "JLRoutesGlobalRoutesScheme"
}

 

3.Scheme(有没有多态的感觉)

JLRoutes支持用指定的URL scheme来创建路由。相同的scheme才能被匹配。默认地,所有的URL会设置进global scheme。

[[JLRoutes globalRoutes] addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
  // This block is called if the scheme is not 'thing' or 'stuff' (see below)
  return YES;
}];

[[JLRoutes routesForScheme:@"thing"] addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
  // This block is called for thing://foo
  return YES;
}];

[[JLRoutes routesForScheme:@"stuff"] addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
  // This block is called for stuff://foo
  return YES;
}];

如果你调用的使用,是这样调用的

[[JLRoutes globalRoutes] addRoute:@"/global" handler:^BOOL(NSDictionary *parameters) {
  return YES;
}];

它只会调用global scheme的对应的URL。不会调用ting scheme里面对应的URL。

当然,你可以设置,如果指定的scheme没有这个URL,去查询global scheme 有没有。你需要设置一个属性。

[JLRoutes routesForScheme:@"thing"].shouldFallbackToGlobalRoutes = YES;

3.通配符的设置URL的方式

通配符为:*

通配符符后面所有的URL上的参数都会以一个数组保存在parameters字典里面的JLRouteWildcardComponentsKey对应的value里。

例如,如果你注册URL如下:

[[JLRoutes globalRoutes] addRoute:@"/wildcard/*" handler:^BOOL(NSDictionary *parameters) {
  NSArray *pathComponents = parameters[JLRouteWildcardComponentsKey];
  if ([pathComponents count] > 0 && [pathComponents[0] isEqualToString:@"joker"]) {
    // the route matched; do stuff
    return YES;
  }

  // not interested unless the joker's in it
  return NO;
}];

如果调用的URL开始是/wildcard,这个路由就可能被触发!!如果第一个参数是joker,就被触发,如果不是,就被拒绝触发。。。

4.选择性路由

如果路由地址设置样式有括号,如:/the(/foo/:a)(/bar/:b),其实它代表的URL有如下:

  • /the/foo/:a/bar/:b
  • /the/foo/:a
  • /the/bar/:b
  • /the

详细请查看Demo。。

查询Routes。

下面的方式,你可以查看Routes里所有注册的URL Routes。

/// All registered routes, keyed by scheme
+ (NSDictionary <NSString *, NSArray <JLRRouteDefinition *> *> *)allRoutes;

/// Return all registered routes in the receiving scheme namespace.
- (NSArray <JLRRouteDefinition *> *)routes;

自定义路由解析

如果你想自己定制一个路由编辑,你可以继承JLRouteDefinition并且用 addRoute:方法去添加你自定义类的对象。

 

FRDModuleManager的认识

FRDModuleManager 是一个简单的 iOS 模块管理工具。

https://github.com/lincode/FRDModuleManager

将生命周期分散给每一个Module。

本文项目主要使用此方式进行 URL的注册。

3.项目搭建和使用示范

项目框架思路:

普通的页面跳转

 

路由跳转

优势:

1.业务页面间(类间)完全脱耦!模块可以按业务修改&删除。不影响其它业务!!

2.框架表现更加规范!!可设置空操作,空模块等功能!!

3.可外部直接调起APP任何个页面。

4.便于页面跳转统计功能。等等。。。。

 

具体Demo请下载并查看…..

https://github.com/WuChuming/APPstructureDemo

 

框架图示如下:

发表评论

邮箱地址不会被公开。 必填项已用*标注