Latest 1.4.1
License MIT
Platforms ios 10.0

Navigate anywhere in just one line.

Codacy Badge
CodeCov Badge
Build Status
Docs Badge


Basic Usage


Define Routes

enum Route: RouteType {
    case newsfeed
    case login
    case signup
    case profile(userID: Int)

Create Router

class Router: XRouter<Route> {

    /// Configure the view controller for the route.
    override func viewController(for route: Route) throws -> UIViewController {
        switch route {
        case .newsfeed: return newsfeedController.rootViewController
        case .login: return LoginFlowCoordinator().start()
        case .signup: return SignupFlowCoordinator().start()
        case .profile(let userID): return UserProfileViewController(withID: userID)


Use Router

// Navigate directly to a route
router.navigate(to: .newsfeed)

// Open a URL

Advanced Usage


XRouter also supports the RxSwift framework out of the box. Bindings exist for navigate(to:), which returns a Completable, and openURL(_:), which returns a Single<Bool>.

router.rx.navigate(to: .loginFlow) // -> Completable
router.rx.openURL(url) // -> Single<Bool>


XRouter supports manually defining transitions.

class Router: XRouter<Route> {

    /// Set the transition for routes.
    override func transition(for route: Route) -> RouteTransition {
        switch route {
        case .newsfeed: return .set     /* Uses UINavigationController(_:).setViewControllers(...) */
        case .profile: return .modal    /* Uses UIViewController(_:).present(...) */
        case default: return .inferred  /* Default. Use appropriate transition based on context. */


See Custom Transitions for details about how you can easily create and use custom transitions.

URL Support

XRouter provides support for deep links and universal links.

You only need to do one thing to add URL support for your routes.
Implement the static method registerURLs:

enum Route: RouteType {

    /// Register URL host and path groups to map to routes.
    static func registerURLs() -> URLMatcherGroup<Route>? {
        return .group("") {
            $"products") { .allProducts }
            $"user/*/logout") { .logout }

            $"products/{category}/view") {
              try .products(category: $0.param("category"))

            $"user/{id}/profile") {
              try .viewProfile(withID: $0.param("id"))


Then you can call the openURL(_:animated:completion:) and/or continue(_ userActivity:) methods, e.g. from in your AppDelegate:

extension AppDelegate {

    /// Handle deep links.
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return router.openURL(url, animated: false)

    /// Handle universal links.
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        return router.continue(userActivity)


Handling errors

If you handle all navigation errors in the same way, you can override the received(unhandledError:) method.

class Router: XRouter<Route> {

    override func received(unhandledError error: Error) {
        log.error("Oh no! An error occured: (error)")


Or you can set a custom completion handler for some individual navigation action:

router.navigate(to: .profilePage(withID: 24)) { (optionalError) in
    if let error = optionalError {
        print("Oh no, we couldn't go here because there was an error!")

Custom Transitions

Here is an example using the popular Hero Transitions library.

Define your custom transitions:

  /// Hero cross fade transition
  let heroCrossFade = RouteTransition { (source, dest, animated, completion) in
      source.hero.isEnabled = true
      dest.hero.isEnabled = true
      dest.hero.modalAnimationType = .fade

      // Present the hero animation
      source.present(dest, animated: animated) {

And set the transition to your custom transition in your Router:

    override func transition(for route: Route) -> RouteTransition {
        if case Route.profile = route {
          return heroCrossFade

        return .inferred


Complete documentation is available here.


To run the example project, clone the repo, and run it in Xcode 10.




XRouter is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'XRouter'


Reece Como, [email protected]


XRouter is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "XRouter",
    "version": "1.4.1",
    "summary": "Navigate anywhere in just one line.",
    "description": "A simple routing library for iOS.nSetup routes and map them to controllers, easy peasy.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Reece Como": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.4.1"
    "platforms": {
        "ios": "10.0"
    "swift_version": "4.2",
    "source_files": "Router/Classes/*.swift",
    "default_subspecs": "Core",
    "subspecs": [
            "name": "Core",
            "source_files": [
            "frameworks": "UIKit"
            "name": "RxSwift",
            "dependencies": {
                "XRouter/Core": [],
                "RxSwift": [
                    "~> 4.0"
            "source_files": "Router/Classes/RxSwift/*.swift"

Pin It on Pinterest

Share This