Beeline is a very small library that aims to provide a lean, automatic implementation of the classic iOS router pattern. It extends UIViewController
to retain a Router
object that serves as the source of truth for controlling navigation flows for all of the view controller's children. When a child view controller wishes to transition to a new screen, it can call show()
on itself and this request is passed up the view controller chain to the routing view controller.
A very basic custom implementation looks as the following. First, we create a Swift enum conforming to Route
where we can define the types of destinations with which we want to move:
enum AppRoute: Route {
case viewController(number: Int)
}
Thanks to Swift associated enums, we can also include any custom parameters the new destination may need.
We then also make a new class which subclasses Router
, which serves as our single point of truth for controlling the app flow based off the destinations we defined above:
public class AppRouter: Router {
override func show(_ route: Route,
from sourceViewController: UIViewController?) -> Bool {
// Optionally, filter out routes we don't support
guard let appRoute = route as? AppRoute else { return false }
// Check the requested enum, and perform the transition
switch appRoute {
case .viewController(let number):
let newViewController = ViewController(number: number)
sourceViewController?
.navigationController?
.pushViewController(newViewController, animated: true)
}
return true
}
}
Using Objective-C associated objects, we can assign this router to any parent view controller that contains all of the view controllers that might want to perform these transitions:
let navigationController = UINavigationController(rootViewController: ViewController())
navigationController.router = AppRouter()
Finally, without any further modification to any of the child view controllers, they can start a transition by simply calling show()
with the desired destination:
class ViewController: UIViewController {
func moveToNewViewController() {
show(AppRoute.viewController(number: 2))
}
}
And that's the entire library! 🎉
- Swift 5
- UIKit-compatible platforms (iOS, tvOS, Mac Catalyst)
Beeline is a very small framework, with all of its code contained in Router.swift
. You can install it in the following ways:
Drag the Beeline/Router.swift
file into your Xcode project.
pod 'Beeline'
You can add Beeline to an Xcode project by adding it as a package dependency.
https://github.com/TimOliver/Beeline
or, add the following to the dependencies in package.swift.
dependencies: [
.package(url: "https://github.com/TimOliver/Beeline", from: "1.0.2")
]
No plans to support Carthage at the moment, but please consider filing a PR if you would like it!
Beeline was built as a component of iComics 2 by Tim Oliver
Beeline is available under the MIT License. Please check the LICENSE file for more information.