How to programmatically setup your app with Scene Delegate in Swift

Up to iOS 12 the AppDelegate would deal with process level events informing when the application launched and terminated and letting you know the state of its UI with UI lifecycle methods like did enter background or foreground events. This made the AppDelegate the perfect place for developers to configure the UIWindow container to programmatically launch the entry point of the application. This was fine in a world of single window applications…

From iOS 13 and onwards though we now have the concept of multi-window session capabilities allowing you to have multiple app instances running at the same time with varying content. Windows are managed by a scene or a UISceneSession class. Your app delegate will still handle process events but will no longer be responsible to configure the window anymore. UI lifecycle events are now managed by a UISceneDelegate in a different place in the SceneDelegate.swift file. This makes the App Delegate more lean and gives your programmatic entry point a more appropriate home where it can receive UI lifecycle methods on a per session basis.

Here’s how to migrate the programmatic setup of your app from the App Delegate over to your Scene Delegate and how to break away from the Main.storyboard in iOS 13 and onwards.

You’ll want to remove the Main.storyboard and get rid of the ViewController.swift as you’ll most likely want to create your own initial view controller. At the very least you can just rename that class to something more relevant to your app.

You’ll want to go ahead and delete the following key value pairs in your info.plist file:
1. Storyboard Name
2. Main storyboard file base name
and in AppleScript terminology they’re namedUISceneStoryboardFile and UIMainStoryboardFile respectively

If you run the app now you’ll see a black screen. This is correct behaviour as you haven’t created a window to be presented in which you’ll also need to create an initial view controller to be displayed in. Which brings us to the next 2 steps. Creating the view controller and creating the window.

We now have to create the initial view controller that we want to present to the user when they your application. You can have as a complex hierarchy as you like but for this example we will stick to a simple UIViewController named ArticleListViewController.

You can create your view controller however you like, in our case it’s as simple as File -> New -> File …, selecting a Cocoa Touch Class write the name of your class, click next, and hit Create.

Your programmatic UIViewController class will appear in the project navigator in the file list.

Note: The view in view controllers in storyboards have a default white background color. This is in comparison to programmatic view controllers whose views do not have a default background color! If you don’t set the background color of your view you’ll end up with a black window at the end of this tutorial. So set the color of your view of your view controller like so view.backgroundColor = .white in the viewDidLoad method.

As developers previously would have written in the app delegate, you now just specify the window setup in the scene delegate.

Run the app and you’ll now have your first scene session managing your the entry point of your application that too programmatically!

Next time we’ll focus on how to amend your app to create a multi-window application!

Find out how to unwrap optionals the quick way in another article I wrote!

Consider following me here on Medium if you liked my content today as I would love for you to be part of my iOS writing journey.

You can check me out on Github and Twitter too.

Engineer Pavan, First of His name, the Unburnt, Lord of the iOS Realm, Bug Breaker, Space Indenter, and New writer — otherwise known as PK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store