Coordinator Pattern Swift

Pratheesh Bennet
4 min readOct 30, 2020

If you are an iOS developer you must have come across the term coordinator design pattern. As the name suggests first let’s look at the definition of coordinator,

Coordinator — a person whose job is to organise events or activities and to negotiate with others in order to ensure they work together effectively

So basically coordinators hold the exclusive responsibility of organising the navigations and flows in app instead of view-controllers owning the navigation responsibility. So you can think of coordinators as the Air Traffic Controller in a busy airport who carefully coordinates like who lands and who takes off in the runway.

Here we are going to see with a real world simple example on the implementation of Coordinator pattern using generic Decorators. Not to worry about the term ‘Decorator’ you will end up safely at the end of the story on what they do.

Example: Let us consider a simple app for scanning. Upon launch for the first time the app has to show the onboarding screen which shows the “What’s in the app” details. Upon tapping continue it goes to the scan view controller. On further subsequent launches of the app, it goes to the scan view controller directly.

Without coordinators the navigation logics are present inside the view controller which doesn’t make the code base scalable, maintainable and reusable. We end up in having the tight coupling of each view controller holding the information about the next view controller to be navigated.

To engineer the coordinator pattern further, I have used Generic Decorators to do the actual navigation and presentation behaviours.

Decorator is a structural pattern that allows adding new behaviours to objects dynamically by placing them inside special wrapper objects.

Let’s create a base decorator called NavigateDecorator class that takes up a generic of type UIViewController. NavigateDecorator holds all the default function definitions wrapped from the coordinator protocol and base coordinator protocol and acts on the generic view controller.

ChildCoordinators: This base decorator “NavigateDecorator” is decorated by LandingViewCoordinator and PaymentCardReaderViewCoordinator. LandingViewCoordinator and PaymentCardReaderViewCoordinator are basically the concrete decorators that extends the base decorator and allows to incorporate new behaviour without modifying existing code.

In the below example we can see that PaymentCardReaderViewCoordinator has incorporated new behaviour of showing the camera privacy alert. LandingViewCoordinator and PaymentCardReaderViewCoordinator are responsible for displaying LandingViewController and PaymentsCardCheckViewController respectively.

Maincoordinator: The main coordinator instantiates and establishes between the various child coordinators .i.e our case LandingViewCoordinator and PaymentCardReaderViewCoordinator. The main coordinator has an an array of childCoordinators to prevent the child coordinators from getting deallocated. All the navigation actions in the view controller is delegated to the main coordinator.

The child coordinators manages the navigations and presentations where as main coordinator manages the child coordinators. The Maincoordinator is instantiated in the SceneDelegate(iOS 13 and above) or Appdelegate (iOS 12 and below). The start method in the Maincoordinator sets the root view based on first launch condition. In the case of first launch the LandingViewCoordinator is instantiated whose responsibility is to navigate to LandingViewContoller.

In our example, upon showing up of the LandingViewContoller by the LandingViewCoordinator managed by Maincoordinator in its start method, on tapping the continue button in LandingViewCoordinator the action is delegated to the Maincoordinator via didTapGetStarted method which then instantiates PaymentCardReaderViewCoordinator whose responsibility is to navigate to PaymentsCardCheckViewController

If we want to pass objects to the next view controller the same can be done by passing the objects are argument to the Maincoordinator.

We can see that the navigation logics are totally isolated from view controller resulting in reusability, scalability, testability, low coupling, better testability, reducing the size of view controller and A/B Testing.

In order to create coordinator easily I have developed a framework. Make use of the framework to create coordinators and perform navigations.

Reference:

--

--

Pratheesh Bennet

Product owner Falcon Forms (www.falconforms.app). Digitize your paperwork using Falcon Forms. Build and configure your own forms in an instant.