RouterMan 1.0

RouterMan 1.0

gumpwang 维护。



RouterMan GitHub license Cocoapods

RouterMan 是一个基于正则表达式的协议导向 URL 路由器,简单且可扩展。

要求

  • iOS 10 或更高版本
  • Swift 4

##特性

  • 正则表达式支持
  • Storyboard 控制器支持,您可以轻松打开基于 Storyboard 的控制器
  • 可变源 URL 到目标行为的转换
  • 可变过渡支持

CocoaPods 安装

pod 'RouterMan'

Carthage 安装

github "wanggang316/RouterMan"

使用方法

使用 RouterMan 很简单,只需基于 RoutableType 实现您的控制器或其他类型的协议,然后将它注册到 RouteMan 即可。

RoutableType 是可路由的基本协议,它衍生了三个协议

  • RoutableControllerType
  • RoutableStoryboardControllerType
  • RoutableActionType

配置 RoutableType

  • RoutableControllerType
class CityViewController: UIViewController: RoutableControllerType {
    
	static var patterns: [String] {
        return ["abc://page/cities/\\d+\\?name=\\w+"]
    }
    
    required convenience init(_ url: URLConvertible) {
        self.init()
        let params = url.urlValue?.queryParameters
        let title = params?["name"]
        self.title = title
    }
}
  • RoutableStoryboardControllerType
// MARK: - RoutableStoryboardControllerType

extension StoryViewController: RoutableStoryboardControllerType {
    
    static var patterns: [String] {
        return ["abc://page/stories/\\d+\\?name=\\S+",
                "http://www.xxx.com/stories/\\d+\\?name=\\w+"]
    }
    
    static var storyboardName: String {
        return "Main"
    }
    
    static var identifier: String {
        return "StoryViewController"
    }
    
    func initViewController(_ url: URLConvertible) {
        print("story parameters: \(String(describing: url.urlStringValue))")
        self.storyId = url.urlValue?.pathComponents.last
        self.storyName = url.urlValue?.queryParameters["name"]
    }
}
  • RoutableActionType
class AlertActionRouter: RoutableActionType {
    static func handle(_ url: URLConvertible) -> Bool {
        let title = url.urlValue?.queryParameters["title"]
        let message = url.urlValue?.queryParameters["message"]

        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Yes", style: .cancel, handler: nil))

        let appdelegate = UIApplication.shared.delegate as? AppDelegate
        appdelegate?.window?.rootViewController?.present(alert, animated: true, completion: nil)

        return true
    }
    
    static var patterns: [String] {
        return ["abc://alert\\?title=\\w+&message=\\w+"]
    }
}

注册

Router.default.registe(CityViewController.self)
Router.default.registe(StoryViewController.self)
Router.default.registe(AlertActionRouter.self)

处理

try? Router.default.handle(url)

许可证

RouterMan 依据 MIT 许可证发布。