FWRouter


@interface FWRouter : NSObject

URL路由器

  • 路由类加载器,访问未注册路由时会尝试调用并注册,block返回值为register方法class参数

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) FWLoader<NSString *, id> *_Nonnull sharedLoader;

Class

  • 注册路由类或对象,批量注册路由规则

    Declaration

    Objective-C

    + (BOOL)registerClass:(nonnull id)clazz
               withMapper:(nullable NSDictionary<NSString *, NSString *> *_Nonnull (
                              ^)(NSArray<NSString *> *_Nonnull))mapper;

    Parameters

    clazz

    路由类或对象,不遍历父类

    mapper

    自定义映射,默认nil时查找规则:xxxUrl => xxxRouter: > xxxDefaultRouter:

  • 预置路由类或对象,批量注册路由规则,仅当路由未被注册时生效

    Declaration

    Objective-C

    + (BOOL)presetClass:(nonnull id)clazz
             withMapper:(nullable NSDictionary<NSString *, NSString *> *_Nonnull (
                            ^)(NSArray<NSString *> *_Nonnull))mapper;

    Parameters

    clazz

    路由类或对象,不遍历父类

    mapper

    自定义映射,默认nil时查找规则:xxxUrl => xxxRouter: > xxxDefaultRouter:

  • 取消注册某个路由类或对象

    Declaration

    Objective-C

    + (void)unregisterClass:(nonnull id)clazz
                 withMapper:
                     (nullable NSDictionary<NSString *, NSString *> *_Nonnull (^)(
                         NSArray<NSString *> *_Nonnull))mapper;

    Parameters

    clazz

    路由类或对象,不遍历父类

    mapper

    自定义映射,默认nil时查找规则:xxxUrl => xxxRouter: > xxxDefaultRouter:

URL

  • 注册 pattern 对应的 Handler,可返回一个 object 给调用方,也可直接触发事件返回nil

    Declaration

    Objective-C

    + (BOOL)registerURL:(nonnull id)pattern
            withHandler:(nonnull FWRouterHandler)handler;

    Parameters

    pattern

    字符串或字符串数组(批量),带上 scheme,如 app://beauty/:id

    handler

    该 block 会传一个字典,包含了注册的 URL 中对应的变量。 假如注册的 URL 为 app://beauty/:id 那么,就会传一个 @{@“id”: 4} 这样的字典过来

  • 预置 pattern 对应的 Handler,可返回一个 object 给调用方,也可直接触发事件返回nil,仅当路由未被注册时生效

    Declaration

    Objective-C

    + (BOOL)presetURL:(nonnull id)pattern
          withHandler:(nonnull FWRouterHandler)handler;

    Parameters

    pattern

    字符串或字符串数组(批量),带上 scheme,如 app://beauty/:id

    handler

    该 block 会传一个字典,包含了注册的 URL 中对应的变量。 假如注册的 URL 为 app://beauty/:id 那么,就会传一个 @{@“id”: 4} 这样的字典过来

  • 取消注册某个 pattern

    Declaration

    Objective-C

    + (void)unregisterURL:(nonnull id)pattern;

    Parameters

    pattern

    字符串或字符串数组(批量)

  • 取消注册所有 pattern

    Declaration

    Objective-C

    + (void)unregisterAllURLs;

Handler

  • 设置全局路由过滤器,URL 被访问时优先触发。如果返回YES,继续解析pattern,否则停止解析

    Declaration

    Objective-C

    + (void)setRouteFilter:(nullable BOOL (^)(FWRouterContext *_Nonnull))filter;
  • 设置全局路由处理器,URL 被访问且有返回值时触发,可用于打开VC、附加设置等

    Declaration

    Objective-C

    + (void)setRouteHandler:(nullable id _Nullable (^)(FWRouterContext *_Nonnull,
                                                       id _Nonnull))handler;
  • 预置全局路由处理器,仅当未设置routeHandler时生效,值为nil时默认打开VC

    Declaration

    Objective-C

    + (void)presetRouteHandler:(nullable id _Nullable (^)(FWRouterContext *_Nonnull,
                                                          id _Nonnull))handler;
  • 设置全局错误句柄,URL 未注册时触发,可用于错误提示、更新提示等

    Declaration

    Objective-C

    + (void)setErrorHandler:(nullable void (^)(FWRouterContext *_Nonnull))handler;

Open

  • 是否可以打开URL,不含object

    Declaration

    Objective-C

    + (BOOL)canOpenURL:(nonnull id)URL;

    Parameters

    URL

    带 Scheme,如 app://beauty/3,支持NSURL和NSString

    Return Value

    返回BOOL值

  • 打开此 URL 会在已注册的 URL -> Handler 中寻找,如果找到,则执行 Handler

    Declaration

    Objective-C

    + (void)openURL:(nonnull id)URL;

    Parameters

    URL

    带 Scheme,如 app://beauty/3,支持NSURL和NSString

  • 打开此 URL,同时当操作完成时,执行额外的代码

    Declaration

    Objective-C

    + (void)openURL:(nonnull id)URL userInfo:(nullable NSDictionary *)userInfo;

    Parameters

    URL

    带 Scheme 的 URL,如 app://beauty/4,支持NSURL和NSString

    userInfo

    附加参数

  • 打开此 URL,同时当操作完成时,执行额外的代码

    Declaration

    Objective-C

    + (void)openURL:(nonnull id)URL
         completion:(nullable FWRouterCompletion)completion;

    Parameters

    URL

    带 Scheme 的 URL,如 app://beauty/4,支持NSURL和NSString

    completion

    URL 处理完成后的 callback,完成的判定跟具体的业务相关

  • 打开此 URL,带上附加信息,同时当操作完成时,执行额外的代码

    Declaration

    Objective-C

    + (void)openURL:(nonnull id)URL
           userInfo:(nullable NSDictionary *)userInfo
         completion:(nullable FWRouterCompletion)completion;

    Parameters

    URL

    带 Scheme 的 URL,如 app://beauty/4,支持NSURL和NSString

    userInfo

    附加参数

    completion

    URL 处理完成后的 callback,完成的判定跟具体的业务相关

  • 快速调用FWRouterHandler参数中的回调句柄,指定回调结果

    Declaration

    Objective-C

    + (void)completeURL:(nonnull FWRouterContext *)context
                 result:(nullable id)result;

    Parameters

    context

    FWRouterHandler中的模型参数

    result

    URL处理完成后的回调结果

Object

  • 查找谁对某个 URL 感兴趣,如果有的话,返回一个 object;如果没有,返回nil

    Declaration

    Objective-C

    + (nullable id)objectForURL:(nonnull id)URL;

    Parameters

    URL

    带 Scheme,如 app://beauty/3,支持NSURL和NSString

  • 查找谁对某个 URL 感兴趣,如果有的话,返回一个 object;如果没有,返回nil

    Declaration

    Objective-C

    + (nullable id)objectForURL:(nonnull id)URL
                       userInfo:(nullable NSDictionary *)userInfo;

    Parameters

    URL

    带 Scheme,如 app://beauty/3,支持NSURL和NSString

    userInfo

    附加参数

Generator

  • 调用此方法来拼接 pattern 和 parameters

    #define APP_ROUTE_BEAUTY @“beauty/:id” [FWRouter generateURL:APP_ROUTE_BEAUTY, @[@13]]; [FWRouter generateURL:APP_ROUTE_BEAUTY, @{@“id”:@13}];

    Declaration

    Objective-C

    + (nonnull NSString *)generateURL:(nonnull NSString *)pattern
                           parameters:(nullable id)parameters;

    Parameters

    pattern

    url pattern 比如 @“beauty/:id”

    parameters

    一个数组(数量和变量一致)或一个字典(key为变量名称)或单个值(替换所有参数)

    Return Value

    返回生成的URL String

Rewrite

  • According to the set of Rules, go to rewrite URL.

    Declaration

    Objective-C

    + (nullable NSString *)rewriteURL:(nonnull id)URL;

    Parameters

    URL

    URL to be rewritten

    Return Value

    URL after being rewritten

  • Set custom rewrite filter block

    Declaration

    Objective-C

    + (void)setRewriteFilter:
        (nullable NSString *_Nonnull (^)(NSString *_Nonnull))filter;

    Parameters

    filter

    Custom filter block

  • Add a RewriteRule

    Declaration

    Objective-C

    + (void)addRewriteRule:(nonnull NSString *)matchRule
                targetRule:(nonnull NSString *)targetRule;

    Parameters

    matchRule

    Regular matching rule

    targetRule

    Conversion rules

  • Add multiple RewriteRule at the same time, the format must be:@[@{@“matchRule”:@“YourMatchRule”,@“targetRule”:@“YourTargetRule”},…]

    Declaration

    Objective-C

    + (void)addRewriteRules:(nonnull NSArray<NSDictionary *> *)rules;

    Parameters

    rules

    RewriteRules

  • Remove a RewriteRule

    Declaration

    Objective-C

    + (void)removeRewriteRule:(nonnull NSString *)matchRule;

    Parameters

    matchRule

    MatchRule to be removed

  • Remove all RewriteRule

    Declaration

    Objective-C

    + (void)removeAllRewriteRules;

Navigation

  • 使用最顶部的导航栏控制器打开控制器

    Declaration

    Objective-C

    + (void)pushViewController:(nonnull UIViewController *)viewController
                      animated:(BOOL)animated;
  • 使用最顶部的导航栏控制器打开控制器,同时pop指定数量控制器

    Declaration

    Objective-C

    + (void)pushViewController:(nonnull UIViewController *)viewController
                           pop:(NSUInteger)count
                      animated:(BOOL)animated;
  • 使用最顶部的显示控制器弹出控制器,建议present导航栏控制器(可用来push)

    Declaration

    Objective-C

    + (void)presentViewController:(nonnull UIViewController *)viewController
                         animated:(BOOL)animated
                       completion:(nullable void (^)(void))completion;
  • 使用最顶部的视图控制器打开控制器,自动判断push|present,完成时回调

    Declaration

    Objective-C

    + (void)openViewController:(nonnull UIViewController *)viewController
                      animated:(BOOL)animated
                       options:(FWNavigationOptions)options
                    completion:(nullable void (^)(void))completion;
  • 关闭最顶部的视图控制器,自动判断pop|dismiss,返回是否成功,完成时回调

    Declaration

    Objective-C

    + (BOOL)closeViewControllerAnimated:(BOOL)animated
                                options:(FWNavigationOptions)options
                             completion:(nullable void (^)(void))completion;