您的当前位置:首页正文

iOS-组件化之路由方案

来源:图艺博知识网

组件化

- 下图为我设想中的组件化流程图:

  • 业务模块层
    • 组件化的目标是为了实现不同业务模块之间的独立性,从而使业务开发更专注于单个模块.
    我倾向于利用cocoapods私有库管理多个模块
    - 优势:
      - 可以使用pod方便集成
      - 可以使用tag进行版本切换
    一些阻碍:
     - cocoapods私有库只能依赖公有库,所以必须将基础组件层暴露出来(既要暴露出来!当然要大家一起用啦!!)
     - 一些共用自定义UI组件,这部分经过划分后归到业务模块调用
     - 基础的项目环境文件(定制的网络接口/数据解析之类的),只能通过建立模板私有库的方式解决(一旦更新就很绝望,幸好这部分不常变...)
    

路由方案 - SPRoutable

gif.gif
  • 示例

    • 建立目标类用于接受Routable调用

      • 目标类为swift,且存在于Frameworks中,则需要在头部添加@objc(类名)
      • 目标类只会init一次,除非手动清除
      • 类名与函数名具有默认前缀(可自行配置)
      • swift示例:
    @objc(Router_swift)
    class Router_swift: NSObject {
      var flag = true
    
      func router_a(params:[String: Any]) -> UIViewController {
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.blue
        return vc
      }
    
      func router_b() -> UIView {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 300, width: 300, height: 300)
        view.backgroundColor = flag ? UIColor.red : UIColor.blue
        flag = !flag
        return view
      }
    
      func router_c(params: [String: Any] = [:]) {
        let alert = UIAlertController()
        alert.title = #function
        alert.message = params.description
        let action = UIAlertAction(title: "确定",
                                   style: UIAlertActionStyle.cancel,
                                   handler: nil)
        alert.addAction(action)
        UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
      }
    }
    
    • 可以这样使用:
     get viewController:
     guard let vc = Routable.viewController(url: "http://swift/a") else { return }
      
     get view:
     guard let v = Routable.view(url: "http://swift/b") else { return }
    
     get object:
     guard let v: UIView = Routable.object(url: "http://swift/b") else { return }
    
     get function:
     Routable.executing(url: str)
    
  • 路由配置参数配置:

    Routable.classPrefix = "Router_"  // defalut: "Router_"
    Routable.funcPrefix  = "router_"  // defalut: "router_"
    Routable.paramName   = "Params"   // defalut: "Params"
    Routable.scheme      = "scheme"   // defalut: ""
    
Top