iOS 路由组件-HBRouter - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
neverfall
V2EX    iOS

iOS 路由组件-HBRouter

  •  
  •   neverfall 2022 年 2 月 24 日 2239 次点击
    这是一个创建于 1521 天前的主题,其中的信息可能已经有所发展或是发生改变。

    HBRouter

    [https://github.com/flywithbug/HBRouter]

    image

    路由初始化注册管理

     //方法工厂 //自定义跳转方法 public typealias handlerFactory = (_ router: HBRouterAction) -> Any? //注册自定义返回 Controller public typealias viewCOntrollerFactory= (_ router:HBRouterAction) -> UIViewController? //全局单例 public static func shared() -> HBRouter.HBRouter ///默认路由 host public var defaultRouterHost: String { get } /// 默认路由 scheme public var defaultRouterScheme: String { get } //默认导航控制器类(初始化时使用) public var wrapNavgClass: UINavigationController.Type @objc weak public var deleage: HBRouter.HBRouterDelegate? @objc public func setDefault(_ scheme: HBRouter.routerScheme, host: HBRouter.routerHost) @objc public private(set) var routerMapping: [HBRouter.routerURLPattern : HBRouter.HBRouterTarget] { get } /// 路由表注册 /// - Parameters: /// - mapping: 路由表映射关系 /// - bundleClass: 映射对象所在 bundleClass public func registRouter(_ mapping: [HBRouter.routerPath : HBRouter.routerTarget], bundleClass: AnyClass? = nil) public func registRouter(_ mapping: [HBRouter.routerPath : HBRouter.routerTarget], bundle: HBRouter.routerBundle? = nil) public func registeRouter(_ targets: [HBRouter.HBRouterTarget]) public func openController(_ action: HBRouter.HBRouterAction) -> (viewController: UIViewController?, success: Bool)? 自定义路由方法处理(用于处理 jsbridge 和 flutter 路由相关) /// 自行处理 openAction 操作 /// - Parameters: /// - urlPatterns: hb://router.com/path hb://router.com hb:// hb /// - factory: 回调方法 override public func registeHander(_ urlPatterns: [HBRouter.routerURLPattern], factory: @escaping HBRouter.handlerFactory) override public func registerHander(_ urlPattern: HBRouter.routerURLPattern, factory: @escaping HBRouter.handlerFactory) override public func registeViewController(_ urlPatterns: [HBRouter.routerURLPattern], factory: @escaping HBRouter.viewControllerFactory) override public func registerViewController(_ urlPattern: HBRouter.routerURLPattern, factory: @escaping HBRouter.viewControllerFactory) 

    代理协议:HBRouterDelegate

     //router 权限校验及调用生命周期 //登录态判断及状态回调 func loginStatus(_ action:HBRouterAction, completion: ((Bool) -> Void)?) -> Bool //路由权限校验 func shouldOpenRouter(_ action:HBRouterAction) -> Bool //路由打开状态 func willOpenRouter(_ action:HBRouterAction) func didOpenRouter(_ action:HBRouterAction) //外链判断 func shouldOpenExternal(_ action:HBRouterAction) -> Bool //外链打开状态 func willOpenExternal(_ action:HBRouterAction) func openExternal(_ action:HBRouterAction, completion: ((Bool) -> Void)?) func didOpenExternal(_ action:HBRouterAction) //未能匹配到的路由回调 func onMatchUnhandleRouterAction(_ action:HBRouterAction) //路由匹配成功回调 func onMatchRouterAction(_ action:HBRouterAction, any:Any?) 

    路由组件使用

    //打开路由 Action override public func openRouterAction(_ action: HBRouter.HBRouterAction) -> Any? public func open(url: URL) -> Any? public func open(url: URL, completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? public func open(url: URL, params: [String : Any]) -> Any? public func open(url: URL, params: [String : Any], completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? public func open(_ path: HBRouter.routerPath, host: HBRouter.routerHost, scheme: HBRouter.routerScheme, params: [String : Any] = [:], completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? public func open(urlPattern: HBRouter.routerURLPattern, completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? public func open(urlPattern: HBRouter.routerURLPattern, params: [String : Any]) -> Any? public func open(urlPattern: HBRouter.routerURLPattern, params: [String : Any], completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? public func open(action: HBRouter.HBRouterAction) -> Any? public func open(action: HBRouter.HBRouterAction, params: [String : Any]) -> Any? public func open(path: HBRouter.routerPath) -> Any? public func open(path: HBRouter.routerPath, params: [String : Any] = [:]) -> Any? public func open(path: HBRouter.routerPath, params: [String : Any] = [:], completion: ((HBRouter.HBRouterResponse) -> Void)? = nil, callBack: ((Any?) -> Void)? = nil) -> Any? //查询栈内匹配到相关页面 override public func matchPages(_ action: HBRouter.HBRouterAction) -> [UIViewController]? //回退到栈内匹配到的页面 override public func pop2Path(_ path: HBRouter.routerPath, params: [String : Any] = [:], completion: ((Bool) -> Void)? = nil) -> [UIViewController]? override public func pop2Action(_ action: HBRouter.HBRouterAction, completion: ((Bool) -> Void)? = nil) -> [UIViewController]? override public func pop2URL(_ url: URL, params: [String : Any] = [:], completion: ((Bool) -> Void)? = nil) -> [UIViewController]? /// 关闭栈内路由匹配成功的控制器,如果只有一个根控制器,则不返回 /// - Parameter path: 路由 path /// - Returns: 被移除的控制器 override public func closePage(path: HBRouter.routerPath) -> [UIViewController]? /// 关闭导航栈中匹配到的路由 /// - Parameter urlPattern: hb://router.com/path /// - Returns: 返回被关闭的控制器 override public func closePage(urlPattern: HBRouter.routerURLPattern) -> [UIViewController]? 

    路由模型:

    @objc public enum HBRouterOption : Int { case push = 0 //导航栈 push case present // present 到新的导航栈 case wrap_nc // present 页面时是否添加导航控制器 case fullScreen // iOS13 是否强制全屏 } //路由打开成功时返回模型对象 @objcMembers public class HBRouterResponse : NSObject { public private(set) var code: Int { get } public private(set) var msg: String { get } public private(set) var data: Any? { get } public private(set) var action: HBRouter.HBRouterAction { get } @objc public init(_ action: HBRouter.HBRouterAction, data: Any? = nil, msg: String = "", code: Int = 0) } //Target 对象 @objcMembers open class HBRouterTarget : NSObject { public var scheme: String public var host: String public var path: String public var target: String public var targetClass: AnyClass? public var bundle: String public var url: URL? public func routerURLPattern() -> HBRouter.routerURLPattern public var targetType: HBRouter.HBTargetType /// 规则定义 /// - Parameters: /// - scheme: hb:// & hb & https:// & http /// - host: router.com /// - path: /home/page/detail /// - target: 目标类 /// - bundle: 目标类所在库的库名 /// - targetType: 目标类能力类型 public init(scheme: String, host: String, path: String, target: String, bundle: String, targetType: HBRouter.HBTargetType = .undefined) public init(path: String, target: String, bundle: String) public init(path: String, target: String) } //router 类型 @objc public enum HBTargetType : Int { case undefined = 0 case cOntroller= 1 case flutter = 2 case bridge = 3 } 

    UIViewController 扩展

     //router item 数据源 @objc public private(set) var routeAction: HBRouter.HBRouterAction? //路由 path @objc public private(set) var routeURLPattern: String? //手动设置 action @objc public func setRouterAction(_ routeAction: HBRouter.HBRouterAction) /// 控制器内处理 action 回调 /// - Parameter action: action 参数 /// - Returns: 判断是否可以打开页面 false 时,不能打开该页面 @objc open func handleRouterAction(_ action: HBRouter.HBRouterAction) -> Bool @objc open class func needsLogin(_ action: HBRouter.HBRouterAction) -> Bool /// 栈内单例是否唯一: /// - Parameter @objc open class func isSingleton(_ action: HBRouter.HBRouterAction) -> Bool //是否支持侧滑返回 @objc open func canSlideBack() -> Bool 
    1 条回复    2022-02-24 16:08:16 +08:00
    7yST27279vu16H21
        1
    7yST27279vu16H21  
       2022 年 2 月 24 日
    支持一个
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2796 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 79ms UTC 13:21 PVG 21:21 LAX 06:21 JFK 09:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86