Flow coordinator pattern on steroids

Pavle Pesic
May 16 · 6 min read

The goal

The goal is to modify flow coordinator so we can use it both with custom and native back action, as well as swipe back. However, it’s a necessity to maintain clean code which is easy to read, reuse and upgrade.

The problem

The major problem with UINavigationController is that clicking on the default back button, or navigating back with a swipe, pops the view controller and the coordinator is not aware of it.

The solution concept

As you already know, controllers don’t know anything about the coordinator. They only expose an interface that informs the coordinator when navigation should happen. That means only view controllers should notify coordinator. Default back action on UINavigationController isn’t informing anyone, so that needs to change.

Implementing the solution

Handling default back action

If we want to change default back action, we can do it by changing the controller’s navigationItem. Every controller needs to implement this. We could either do this in BaseViewController (base class for every controller in the app) or custom UINavigationController. I think a better solution is to do this in custom UINavigationController because we can also change the design of a back button and controller title.

Example

As you can see AuthCoordinator knows about four view controllers ChooseLoginRegisterViewController, LoginViewController, RegisterViewController and TermsAndConditionsViewController. WalktroughCoordinator knows only about WalktroughViewController. In real the app example, WalktroughCoordinator wouldn’t be a child of AuthCoordinator. We need to implement this flow.

App Structure

Handling swipe back

First of all, we have to make a public function for enabling back swipe. We are going to do it in CoordinatorNavigationController.

Example

We will use the same flow from above. RegisterViewContoller doesn’t need to override transitionBackFinished method. Back swipe will pop RegisterViewContoller, and we don’t need to inform the coordinator about it. There is no action needed for that use case.

Conclusion

We’ve removed some of the flow coordinators most significant flaws. On the other hand, we’ve made the pattern even more complicated. However, I think the tradeoff is good.

Resources & Reading list

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Pavle Pesic

Written by

Senior iOS Developer & Mobile Team Lead

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing