Bye Bye AppDelegate! SwiftUI App Life Cycle

Sanzeev Gautam
Nov 20, 2020 · 4 min read
Image for post
Image for post
Photo by Maxwell Nelson on Unsplash

Now XCode 12 introduces two options for app life cycle. When you create new project by xcode then you can see Life Cycle dropdown to choose.

UIKit AppDelegate or SwiftUI

Image for post
Image for post
XCode 12 — Choose project template

Get Started

Image for post
Image for post
Default SwiftUI life cycle

You can see that there are two swift files — one SwiftUILifeCycleAppApp.swift & ContentView.swift

ContentView.swift is the first view file which renders on the window where SwiftUILifeCycleAppApp.swift is your main entry point in the app just like AppDelegate.

Let’s dive into depth of SwiftUILifeCycleAppApp.swift

There is a struct ‘SwiftUILifeCycleAppApp’ which confirms to App protocol and in the body part, ContentView is added on the app Window. That’s it, nothing else in the file. You can see, it is so light weighted. One more thing, you can see @main just above the struct ‘SwiftUILifeCycleAppApp’. It tells to the system that it is main entry point of the app.

Now, you have lots of questions that where we’ll do lots of stuff which we do in AppDelegate’s didFinishLaunchWithOptions callback and app life cycle callback events methods etc. Don’t worry 😉 we’ll discuss all the these here.

App States Callbacks

Image for post
Image for post
SwiftUI life cycle with App States

For more details, you should read apple document.

Deeplink URLs

But in the SwiftUI, we can handle incoming urls by attaching the onOpenURL modifier to the top-most scene in your app. It returns the incoming url in closure (refer line numbers — 17 to 20 in below screenshot).

Image for post
Image for post
SwiftUI life cycle with deeplink url

App Init

Image for post
Image for post
SwiftUI life cycle with App Init

AppDelegate in SwiftUI

The @UIApplicationDelegateAdaptor gives the option of using UIApplicationDelegate methods that are traditionally used in UIKit applications.

First add a file to create AppDelegate class e.g. ‘MyAppDelegate.swift’ then create a class of ‘MyAppDelegate’ which inherited from ‘UIResponder’ and confirms to ‘UIApplicationDelegate’ and then add didFinishLaunchWithOptions method.

Image for post
Image for post
AppDelegate in SwiftUI

After creating AppDelegate, now attach it with struct ‘SwiftUILifeCycleAppApp’ main entry point by @UIApplicationDelegateAdaptor property wrapper.

Image for post
Image for post
Attach AppDelegate with SwiftUI life cycle

First scene init method calls and then AppDelegate’s didFinishLaunchWithOptions method calls.

Summary

Here is link of full source code: https://github.com/sanjeevworkstation/SwiftUILifeCycleApp

Thank You. Happy coding! 👍

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Sanzeev Gautam

Written by

A programmer, mobile photographer. https://www.linkedin.com/in/sanjeevkumargautam/ , https://www.instagram.com/the_lens_on/

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Sanzeev Gautam

Written by

A programmer, mobile photographer. https://www.linkedin.com/in/sanjeevkumargautam/ , https://www.instagram.com/the_lens_on/

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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