Using Coordinator With Scene Delegates

Sun Aug 25 2019 | Mark Struzinski

---

I have switched to using the coordinator pattern with any new work I do. I was resistant to break away from Storyboards using segues at first. But over the years I’ve learned any app of medium to large complexity breaks down under the weight of segues after a certain point.

I’ve been playing around with the new iOS 13 APIs a bit, and I was trying to create a new UIKit app using the coordinator pattern. The new UIKit template assumes the use of scenes instead of a single window. The single view app template breaks away from the standard app delegate and uses the new scene APIs. File ➤ New Project now creates a UISceneDelegate and wires everything up for you.

The Xcode template assumes you want to use storyboard from the application start. It creates a scene configuration that wires itself up to Main.storyboard. As you might know, this does not play well with the coordinator pattern. The coordinator pattern works best if your application starts up with a coordinator at the root.

I used to initialize my app coordinator in the app delegate. In the base project template, I would remove the starting storyboard entry. Then I would grab hold of the window property in the app delegate and initialize it with a nav controller. Then I would pass that nav controller into the app coordinator and control then goes to the coordinator for the rest of the work.

I haven’t dug into multiple window support yet, so this article assumes support for a single window and scene configuration. It took me a bit of research to get this working, so I thought I’d post it here in case anyone else has the same problem,

First, go to your app target on the General tab. In the Deployment Info section, remove the entry in the Main Interface field.

This will remove the UIMainStoryboardFile entry from the Info.plist. This prevents the app from launching into the storyboard directly.

Now open SceneDelegate.swift. The scene(_:willConnectTo:options:) method will initially look something like this (minus the boilerplate comments that are normally injected):