URLNavigator应用及源码解析

应用

  • 集成
pod 'URLNavigator' 
  • 创建NavigationMap文件
  • 将视图注册到URL模式
navigator.register("petrel://douban") { url, values, context in
    return DouBanViewController()
}
  • didFinishLaunchingWithOptions方法中初始化
URLNavigationMap.initialize(navigator: navigator)
  • 跳转
navigator.push("petrel://douban")

源码解析

注册过程就是通俗的说就是将制造器工厂(ViewControllerFactoryURLOpenHandlerFactory)以URL为key存到字典(viewControllerFactorieshandlerFactories)中, 跳转过程 先用URLMatcher解析URL,然后映射控制器工厂,生成控制器,跳转并返回控制器

注册

注册方式有两种:register,handle

  • register
func register(_ pattern: URLPattern, _ factory: @escaping ViewControllerFactory)

register 是直接注册ViewController的生产工厂,对应跳转方式:push,present

  • handle
func handle(_ pattern: URLPattern, _ factory: @escaping URLOpenHandlerFactory)

handle可以理解为自定义的操作工厂,比如弹框,或吐丝,对应跳转方式:open

跳转

URLNavigator的跳转方式有三种:push,present,open

  • push
  public func push(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController?

  public func push(_ viewController: UIViewController, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController?

push时可以直接给出要跳转的ViewController,设置from从哪个界面跳转,不设置默认从当前控制器跳转,也可以通过url,去跳转,使用url跳转时可以设置context,

  • present
  public func present(_ url: URLConvertible, context: Any? = nil, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController?

  public func present(_ viewController: UIViewController, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController?

present比push多出两个参数 wrap:可以在要跳转的ViewController上包裹一层,比如在ViewController上包裹一层navigationcontroller,completion结束后可做操作,其余参数和push的作用一致

  • open
func open(_ url: URLConvertible, context: Any? = nil) -> Bool

跳转自定义操作,可以设置context

URLMatcher

还没有理解太透彻,稍后理解透彻了再补充上

应用实例Demo

发布了46 篇原创文章 · 获赞 8 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览