Adding AppDelegate to your SwiftUI App

Moussa Hellal
2 min readNov 15, 2023

--

Hello Swift developers around the globe. In Today’s topic we will tackle Adding the old friend AppDelegate to your shiny new project in SwiftUI.

Once you create a new project in Xcode with SwiftUI you will notice something that there is no AppDelegate file. It must be a heartbreaking for some of you, and for that reason this article will help you out linking an AppDelegate class to your SwiftUI app.

First Step:

Create an Xcode project. -> Choose App -> Make sure to select SwiftUI in Interface option.

Click Next and you will get this:

No App Delegate so far, and that brings us to step two.

Second Step:

Create a new file in your Project folder and name it AppDelegate

Now Copy -> Paste this code in your newly created file “AppDelegate”

import Foundation
import SwiftUI

class AppDelegate: NSObject, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

return true
}

//YOU CAN ADD OTHER UIApplicationDelegate here

}

That brings us to our last step

Third and Final Step:

Open you main app SwiftUI file in our example here it’s “AppDelegateSampleApp”:

Copy this line of code and paste as same as in the screenshot in your Main SwiftUI App file:

@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate

That’t it! Easy right?

I appreciate you choosing to spend some of your day with me. I hope that my piece was informative and helpful to you.

Farewell, keep growing and learning. See you in the next one. ✌️

Make sure to follow me on Twitter to keep receiving the newest articles:

https://twitter.com/mosesCodeOs

--

--