URL Routing with Compass
Tags: url, routing, compass, swift
Published: 2017-12-06 15:13:02

---
title: URL Routing with Compass
published: true
description: 
tags: url, routing, compass, swift
---

Original post https://github.com/onmyway133/blog/issues/110

Apps often have many screens, and `UIViewController` works well as the basis for a screen, together with presentation and navigation APIs. Things are fine until you get lost in the forest of flows, and code becomes hard to maintain. 

One way to avoid this is the `central URL routing` approach. Think of it as a network router that handles and resolves all routing requests. This way, the code becomes declarative and decoupled, so that the list component does not need to know what it's presenting. URL routing also makes logging and tracking easy along with ease of handling external requests such as deep linking. 

There are various frameworks that perform URL routing. In this tutorial you’ll use [Compass](https://github.com/hyperoslo/Compass) for its simplicity. You’ll refactor an existing app, which is a simplified Instagram app named `PhotoFeed`. When you’ve finished this tutorial, you’ll know how to declare and use routers with Compass and handle deep linking. 

# Getting Started 

Download the [starter project](https://github.com/onmyway133/PhotoFeed/tree/start) and unzip it. Go to the `PhotoFeed` folder and run `pod install` to install the particular dependencies for this project. Open `PhotoFeed.xcworkspace` and run the project. Tap `Login` to go to the Instagram login page and enter your Instagram credentials, then have a look around the app. 

![artboard](https://user-images.githubusercontent.com/2284279/33277373-a808bf8a-d398-11e7-9b27-9ddc4549ffdc.png)

The main app is made of a `UITabBarController` that shows the feed, the currently logged-in user profile and a menu. This is a typical `Model View Controller` project where `UIViewController` handles `Cell` delegates and takes responsibility for the navigation. For simplicity, all view controllers inherit from `TableController` and `CollectionController` that know how to manage list of a particular model and cell. All models conform to the new Swift 4 `Codable` protocol.

## Registering Your App on Instagram

In order to use the Instagram API, you’ll need to register your app at [Instagram Developer](https://www.instagram.com/developer/). After obtaining your _client id_, switch back to the project. Go to `APIClient.swift` and modify your `clientId`. 

![artboard 2](https://user-images.githubusercontent.com/2284279/33277396-b40a3bec-d398-11e7-9f1d-3033870e0738.png)

**Note**: The project comes with a default app with limited permissions. The app can't access following or follower APIs, and you can only see your own posts and comments

# Compass 101

The concept of Compass is very simple: you have a set of routes and central place for handling these routes. Think of a route as a navigation request to a specific screen within the app. The idea behind URL routing is borrowed from the modern web server. When user enters a URL into the browser, such as `https://www.raywenderlich.com/category/ios`, that request is sent from the browser to the web server. The server parses the URL and returns the requested content, such as HTML or JSON. Most web server frameworks have URL routing support, including [ASP.NET](https://expressjs.com/en/guide/routing.html), [Express.js](https://expressjs.com/en/guide/routing.html), and others. For example, here is how you handle a URL route in express.js:

```js
app.get('/api/category/:categoryTag', function (req, res) {
  const page = getCategoryPageFor(req.params.categoryTag)
  res.send(page)
})
```

Users or apps request a specific URL that express an intent about what should be returned or displayed. But instead of returning web pages, Compass constructs screens in terms of `UIViewController` and presents them.

## Route Patterns

This is how you declare a routing schema in Compass:

```swift
Navigator.routes = ["profile:{userId}", "post:{postId}", "logout"]
```

This is simply as array of patterns you register on the `Navigator`. This is the central place where you define all your routes. Since they are in one place, all your navigations are kept in one place and can easily be understood. Looking at the example above, `{userId}, {postId}` are placeholders that will be resolved to actual parameters. For example with `post:BYOkwgXnwr3`, you get `userId` of `BYOkwgXnwr3`. Compass also performs pattern matching, in that `post:BYOkwgXnwr3` matches `post:{postId}`, not `comment:{postId}`, `blogpost:{postId}`, ...This will become to make sense in following sections. 

## The Navigator

The `Navigator` is a the central place for routes registration, navigating and handling.

![artboard 3](https://user-images.githubusercontent.com/2284279/33277861-1855fa40-d39a-11e7-8449-0d10608d2501.png)

 The next step is to trigger a routing request. You can do that via the `Navigator`. For example, this is how you do  in the feed to request opening a specific post:

```swift
Navigator.navigate(urn: "post:BYOkwgXnwr3")
```

Compass uses the user-friendly `urn`, short for [Uniform Resource Name](https://no.wikipedia.org/wiki/Uniform_Resource_Name) to make itwork seamlessly with [Deep Linking](https://en.wikipedia.org/wiki/Mobile_deep_linking). This `urn` matches the routing schema `post:{postId}`. Compass uses `{param}` as the special token to identifier the parameter and `:` as the delimiter. You can change the delimiter to something else by configuring `Navigator.delimiter`. You have learned how to register routes and navigate in Compass. Next, you will learn how to customize the handling code to your need.

## Location

`Navigator` parses and works with `Location` under the hood. Given the `URN` of `post:BYOkwgXnwr3`, you get a `Location` where `path` is the route pattern, and `arguments` contain the resolved parameters.

```swift
path = "post:{postId}"
arguments = [
  "postId": "BYOkwgXnwr3"
]
```

To actually perform the navigation, you assign a closure that takes a `Location` to `Navigator.handle`.

```swift
Navigator.handle = { [weak self] location in
  guard let `self` = self else {
    return
  }

  let arguments = location.arguments

  switch location.path {
    case "post:{postId}":
      let postController = PostController(postId: postID)
      self.rootController.pushViewController(postController, animated: true)
    default: 
      break
  }
}
```

The `let `self` = self` dance is to ensure `self` isn't released by the time this closure is executed. If it is released, the routing it's about to perform is likely invalid, and you return without doing anything instead. You should typically do the above  in the components that own the root controller, such as `AppDelegate` as seen above.  That's the basic of Compass. Astute readers may have noticed that it does not scale, as the number of `switch` statements will grow as the number of routes and endpoints increase in your app. This is where the `Routable` protocol comes in.  Anything conforming to `Routable` knows how to handle a specific route. Apps may have many modular sections, and each section may have a set of routes. Compass handles these scenario by using a composite `Routable` named `Router` that groups them . You can have a router for a pre-login module, a post-login module, premium features module, and so on. 

![untitled 2 2017-08-30 09-53-58](https://user-images.githubusercontent.com/2284279/33277894-2eff78de-d39a-11e7-8554-bf064b621921.png)

In the next section, you'll change PhotoFeed to use `Router` and `Routable`.

# Router to the Rescue

The first step is to include Compass in your project. Using `CocoaPods`, this is an easy task. Edit the `Podfile` with the project and type `pod 'Compass', '~> 5.0'` just before the `end` statement. Then open Terminal and execute the following:

```sh
pod install
```

The version of Compass used in this tutorial is `5.1.0`.

## Registering a Router

![untitled 2 2017-08-30 10-04-50](https://user-images.githubusercontent.com/2284279/33277916-3da628f6-d39a-11e7-9652-b45ea4a935fd.png)

To start, you’ll create a simple router to handle all post-login routes. Open _AppDelegate.swift_, and import Compass at the top of the file:

```swift
import Compass
```

Next, add the following router declaration under the `var mainController: MainController?` declaration:

```swift
var postLoginRouter = Router()
```

Then declare a function called `setupRouting`, you 'll do this in an extension to separate the routing setup from the main code in `AppDelegate`. 

```swift
extension AppDelegate {
  func setupRouting() {
    // [1] Register scheme
    Navigator.scheme = "photofeed"

    // [2] Configure routes for Router
    postLoginRouter.routes = [:]

    // [3] Register routes you 'd like to support
    Navigator.routes = Array(postLoginRouter.routes.keys)

    // [4] Do the handling
    Navigator.handle = { [weak self] location in
      guard let selectedController = self?.mainController?.selectedViewController else {
        return
      }

      // [5] Choose the current visible controller
      let currentController = (selectedController as? UINavigationController)?.topViewController
        ?? selectedController

      // [6] Navigate
      self?.postLoginRouter.navigate(to: location, from: currentController)
    }
  }
}
```

Here's what you do in the above method:

1.  Declare a scheme for Compass to work. This is your application URL scheme. This shines when you wish to support deep linking .
2.  Register all the routes in your app. `Router` accepts a mapping of route and `Routable` conformers. This is empty for now, but you will add several routes in a moment.
3.  A `Navigator` can manage multiple routers. In this case, you only register one router.
4.  This is where you supply the handling closure. `Navigator` uses this to handle a resolved location request.
5.  Screens in one modular section originate from one root or parent view controller. In order to show something from the route, you should try to push or present it from the selected most-visible view controller. In this project, the root is a `UITabBarController`, so you try to get the top controller from the current selected navigation. The selection of `current controller` depends on the module and your app use cases, so Compass let you decide it.  If you use the side menu drawer, then you can just change the selected view controller.
6.  Finally, since `Router` is a composite `Routable`, you dispatch to it the `Location`.

![main storyboard 2017-08-30 10-37-16](https://user-images.githubusercontent.com/2284279/33277961-531e6342-d39a-11e7-9199-158a469a3709.png)

Finally, you need to call this newly added function. Add the following line right above `window?.makeKeyAndVisible()`:

```swift
setupRouting() 
```

Build and run. Nothing seems to work yet! To make things happen, you’ll need to add all the route handlers. You’ll do this in the next section.

## Implementing the Route Handlers

First, create a new file and name it `Routers.swift`. This is where you’ll declare all of your route handlers. At the beginning of the file, add `import Compass`. Compass declares a simple protocol — `Routable` — that decides what to do with a given `Location` request from a `Current Controller`. If a request can't be handled, it will throw with `RouteError`. Its implementation looks like this: 

```swift
public protocol Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws
}
```

It’s an incredibly simple protocol. Any routes you create only need to implement that single method. Now create your first handler to deal with user info request. 

```swift
struct UserRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    // [1] Examine arguments
    guard let userId = location.arguments["userId"] else {
      return
    }

    // [2] Create the controller
    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "UserController") as! UserController
    controller.userId = userId
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}
```

This is called when you touch the post author on the feed. Here’s what's happening:

1.  `UserRoute` deals with `user:{userId} urn`, so `location.arguments["userId"]` should contain the correct `userId` to inject into `UserController`.
2.  This app uses storyboards to make the UI, so get the correct view controller based on its identifier. Remember tha `currentController` is the current visible controller in the navigation stack. So you ask for its `UINavigationController` to push a new view controller.

Right below this router, add one more route for the screen shown when the user wants to see who likes a particular post:

```swift
struct LikesRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    guard let mediaId = location.arguments["mediaId"] else {
      return
    }

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LikesController") as! LikesController
    controller.mediaId = mediaId
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}
```

### The remaining Route

Now it's your turn to write the other route handlers: `CommentsRoute, FollowingRoute, FollowerRoute`. See if you can figure it out first, you can find the solution below. Here's what you should have:

```swift
struct CommentsRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    guard let mediaId = location.arguments["mediaId"] else {
      return
    }

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CommentsController") as! CommentsController
    controller.mediaId = mediaId
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}

struct FollowingRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    guard let userId = location.arguments["userId"] else {
      return
    }

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FollowingController") as! FollowingController
    controller.userId = userId
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}

struct FollowerRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    guard let userId = location.arguments["userId"] else {
      return
    }

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FollowerController") as! FollowerController
    controller.userId = userId
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}
```

### The LogoutRoute

There is one more route to add: the one you'll use for logout. `LogoutRoute` is quite tricky, as it usually involves changing the current root view controller. Who knows this better than the app delegate? Open _AppDelegate.swift_ and add the following code at the very bottom:

```swift
struct LogoutRoute: Routable {
  func navigate(to location: Location, from currentController: CurrentController) throws {
    APIClient.shared.accessToken = nil
    (UIApplication.shared.delegate as! AppDelegate).showLogin()
  }
}
```

Now that you’ve implemented all of the route handlers, you will have to tell `Navigator` which route is used for which URN. Still in _AppDelegate.swift_, find `postLoginRouter.routes = [:]` and replace it with the following:

```swift
postLoginRouter.routes = [
  "user:{userId}": UserRoute(),
  "comments:{mediaId}": CommentsRoute(),
  "likes:{mediaId}": LikesRoute(),
  "following:{userId}": FollowingRoute(),
  "follower:{userId}": FollowerRoute(),
  "logout": LogoutRoute()
]
```

Build the app and everything should compile. Now all that’s left is to actually _all_ all of the code you’ve written!

## Refactoring Time

It’s time to refactor all the code in `UIViewController` by replacing all the navigation code with your new routing instructions. Start by freeing the `FeedController` from the unnecessary tasks of navigation. Open _FeedController.swift_ and add the following import to the top of the file:

```swift 
import Compass
```

Next, look for `// MARK: - MediaCellDelegate` and replace the three `MediaCell` delegate methods with the following:

```swift
func mediaCell(_ cell: MediaCell, didViewLikes mediaId: String) {
  try? Navigator.navigate(urn: "likes:\(mediaId)")
}

func mediaCell(_ cell: MediaCell, didViewComments mediaId: String) {
  try? Navigator.navigate(urn: "comments:\(mediaId)")
}

func mediaCell(_ cell: MediaCell, didSelectUserName userId: String) {
  try? Navigator.navigate(urn: "user:\(userId)")
}
```

For these three cases, you simply want to navigate to another screen. Therefore, all you need to do is tell the `Navigator` where you want to go. For simplicity, you use `try?` to deal with any code that throws. Build and run the app. Search for your favorite post in the feed, and tap on the author, the post comments or likes to go to the target screen. The app behaves the same as it did before, but the code is now clean and declarative. Now do the same with _UserController.swift_. Add the following import to the top of the file:

```swift 
import Compass
```

Replace the code after `// MARK: - UserViewDelegate` with the following:

```swift
func userView(_ view: UserView, didViewFollower userId: String) {
  try? Navigator.navigate(urn: "follower:\(userId)")
}

func userView(_ view: UserView, didViewFollowing userId: String) {
  try? Navigator.navigate(urn: "following:\(userId)")
}
```

Your task now is to refactor with the last route `LogoutRoute`. Open _MenuController.swift_ and add the following to the top:

```swift
import Compass
```

Remove the `logout` method altogether. Find the following:

```swiftif indexPath.section == Section.account.rawValue, indexPath.row == 0 {
  logout()
}
```

...and replace it with:

```swift
if indexPath.section == Section.account.rawValue, indexPath.row == 0 {
  try? Navigator.navigate(urn: "logout")
}
```

Build and run the app, navigate to the menu and tap _Logout_. You should be taken to the login screen.

# Handling Deep Linking

[Deep linking](https://en.wikipedia.org/wiki/Mobile_deep_linking) allows your apps to be opened via a predefined URN. The system identifies each app via its [URL scheme](https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html). For web pages, the scheme is usually `http`, `https`. For _Instagram_ it is, quite handily, `instagram`. Use cases for this are inter-app navigation and app advertisements. For examples, the Messenger app uses this to open the user profile in the Facebook app, and Twitter uses this to open the App Store to install another app from an advertisement. In order for user to be redirected back to PhotoFeed, you need to specify a custom URL scheme for your app. Remember where you declared `Navigator.scheme = "photofeed"`? PhotoFeed just so happens to conform to this URL scheme, so deep links already worked — and you didn't even know it! Build and run the app, then switch to Safari. Type `photofeed://` in the address bar, then tap Go. That will trigger your app to open. The app opens, but PhotoFeed doesn't parse any parameters in the URL to go anywhere useful. Time to change that! Your app responds to the URL scheme opening by implementing a `UIApplicationDelegate` method. Add the following after `setupRouting` in `AppDelegate.swift`:

```swift
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  try? Navigator.navigate(url: url)
  return true
}
```

`Navigator` parses and handles this for you. Build and run again. Go to Safari app, type `photofeed://user:self` and tap Go. Photofeed will open and show the currently logged in users’ profile. Because you already had `UserRoute`, the requested URL was handled gracefully. Your app may already be presenting a particular screen when a routing request comes, but you’ve anticipated this by resetting the navigation controller or presentation stack to show the requested screen. This simple solution works for most cases. Again, it's recommended you pick the topmost visible view controller as the current controller in `Navigator.handle`.

![artboard 5](https://user-images.githubusercontent.com/2284279/33277990-709ae7c4-d39a-11e7-99b2-27acb7a1c5d0.png)

Deep linking is usually considered external navigation, in that the routing requests come from outside your app. Thanks to the central routing system that you developed, the code to handle external and internal routing requests is very much the same and involves no code duplication at all.

# Routing with Push Notifications

Push notifications help engage users with your app. You may have received messages like "Hey, checkout today 's most popular stories" on _Medium_, "Your friend has a birthday today" on Facebook, ... and when you tap those banners, you are taken straight to that particular screen. How cool is that? This is achievable with your URL routing approach. Imagine users tapping a push notification banner saying "You’re a celebrity on PhotoFeed — check out your profile now!" and being sent directly to their profile screen. To accomplish this, you simply have to embed the URN info into the push payload and handle that in your app.

## Setting up

To start, you’ll need to specify your bundle ID. Go to `Target Settings\General` to change your bundle ID as push notification requires a unique bundle ID to work. Your project uses `com.fantageek.PhotoFeed` by default.

![step1_bundleid](https://user-images.githubusercontent.com/2284279/33278026-8e1fe01a-d39a-11e7-982f-854ab7056aca.png) 

Next, you’ll need to register your _App ID_. Go to [Member Center](https://developer.apple.com/account/ios/certificate) and register your App ID. Remember your _Team ID_, as you will need it in the final step. Also tick the _Push Notification_ checkbox under _Application Services_. 

![step1_appid](https://user-images.githubusercontent.com/2284279/33278038-94947866-d39a-11e7-9386-8fffcd5bf9cf.png)


Now you’ll need to generate your _Authentication Key_. Apple provides [Token Authentication](https://developer.apple.com/news/?id=09222016a) as a new authentication mechanism for push notifications. The token is easy to generate, works for all your apps, and mostly, it never expires. Still in [Member Center](https://developer.apple.com/account/ios/authkey/), create a new _Key_ and download it as a `.p8` file. Remember your _Key ID_ as you will need it in the final step. 

![step2_key](https://user-images.githubusercontent.com/2284279/33278050-9bb3ccc8-d39a-11e7-99b8-076138e5b559.png)

Next up: enabling push notification capability. Back in Xcode, go to `Target Settings\Capabilities` and enable _Push Notifications_, which will add `PhotoFeed.entitlements` to your project. 

![step3_capability](https://user-images.githubusercontent.com/2284279/33278054-a21dde8c-d39a-11e7-907b-b1ec620050b4.png)


The next step is to register for push notifications. Open `MainController.swift` and add the following import to the top of _MainController.swift_:

```swift  
import UserNotifications
```

You want to enable push notification only after login, so `MainController` is the perfect place. [UserNotifications](https://developer.apple.com/documentation/usernotifications) is recommended for app targeting iOS 10 and above.

```swift
override func viewDidLoad() {
  super.viewDidLoad()

  // [1] Register to get device token for remote notifications
  UIApplication.shared.registerForRemoteNotifications()

  // [2] Register to handle push notification UI
  let options: UNAuthorizationOptions = [.alert, .sound, .badge]
  UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
    print(error as Any)
  }
}

```

The permission dialog is shown once, so make sure you accept it. It’s time to handle the device token. Open `AppDelegate.swift`, and add the following to the end of `extension AppDelegate`:

```swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  // [1] Parse to token string
  let token = deviceToken.map {
    return String(format: "%02.2hhx", $0)
  }.joined()

  // [2] Log it
  print("Your device token is \(token)")
}
```

This is where you get device token if your app successfully connects to APNs. Normally, you would send this device token to the backend so they can organize , but in this tutorial we just log it. It is required in the tool to be able to target a particular device.

## Handling payload

Open `AppDelegate.swift` and add the following to th end of `extension AppDelegate`:

```swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  // [1] Always call the completion handler
  defer {
    completionHandler(.newData)
  }

  // [2] Convert into JSON dictionary
  guard let json = userInfo as? [String: Any] else {
    return
  }

  // [3] Parse to aps
  guard let aps = json["aps"] as? [String: Any] else {
    return
  }

  // [4] Parse to urn
  guard let urn = aps["urn"] as? String else {
    return
  }

  try? Navigator.navigate(urn: urn)
}
```

This method is called when your app receives push notification payload and is running. The above code is relatively straightforward: it first tries to parse the `urn` information from the payload, then tells `Navigator` to do the job . Build and run the app on the device, since push notifications won’t work on the simulator. Log in to the app if prompted. Once on the main screen, grant push notification permissions to the app in order to receive alerts. You should see the `device token` logged to your Xcode console. 

<div align="center">
<img src="https://user-images.githubusercontent.com/2284279/33278064-abbe6592-d39a-11e7-81cd-b3affc3ee4b6.PNG" height="500" />
</div>

## Testing Push Notifications

In this tutorial, you’ll use a tool called [PushNotifications](https://github.com/onmyway133/PushNotifications) to help you easily create push notifications for your app. Download the tool `PushNotifications` from [here](https://github.com/onmyway133/PushNotifications/releases). This tool sends payloads directly to APNs.

![step4_test](https://user-images.githubusercontent.com/2284279/33278070-b3094632-d39a-11e7-81f6-62f1ed04130b.png)


Choose `iOS\Token` to use `Token Authentication`, you get that by creating and downloading your `Key` from [Certificates, Identifiers & Profiles](https://developer.apple.com/account/ios/authkey/). Browse for the `.p8` auth key file that you downloaded earlier. Enter `Team ID`, you can check it by going to [Membership Details](https://developer.apple.com/account/#/membership) Enter `Key ID`, this is the ID associated with the `Key` from the first step. Enter `Bundle ID` and `device token`. Paste the following into as. It is a traditional payload associated with the URN.

```json
{
   "aps":{
      "alert":"You become a celebrity on PhotoFeed, checkout your profile now",
      "urn": "user:self"
   }
}
```

Since you're debugging with Xcode, select `Sandbox` as environment.

<div align="center">
<img src="https://user-images.githubusercontent.com/2284279/33278079-baef3ece-d39a-11e7-8818-56b54ed01a1d.PNG" height="500" />
</div>


Tap `Send` now. If your app is in the background, an alert will appear. Tapping it will take you to your app and show you your user profile. Bravo! You just implemented deep linking in push notification, thanks again to the URL routing.

# Read more

Here is the [final project](https://github.com/onmyway133/PhotoFeed/tree/finish) with all the code from this tutorial. You now understand central routing patterns, have mastered Compass and even refactored a real-world app. However, there is no silver bullet that works well for all apps. You need to understand your requirements and adjust accordingly. If you want to learn more about other navigation patterns, here are a few suggestions: 

*   [Coordinator](http://khanlou.com/2015/10/coordinators-redux/)
*   [Flow controller](http://albertodebortoli.com/blog/2014/09/03/flow-controllers-on-ios-for-a-better-navigation-control/)
*   [Routing in VIPER](https://www.objc.io/issues/13-architecture/viper/)

Remember, it's not only about the code, but also about the user experience that your app provides. So please make sure you conform to the guidelines [Navigation in Human Interface Guidelines iOS](https://developer.apple.com/ios/human-interface-guidelines/interaction/navigation).