WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How to add an AppDelegate to a SwiftUI app

Paul Hudson    @twostraws   

Updated for Xcode 14.0 beta 1

New in iOS 14

SwiftUI apps launch using a custom struct that conforms to the App protocol, but sometimes you might want to get back the old UIApplicationDelegate functionality we used to have – perhaps to handle registration for push notifications, to respond to memory warnings, to detect time changes, and so on.

To do this, first create a custom class that inherits from NSObject and conforms to the UIApplicationDelegate protocol, like this:

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        return true

I’ve added didFinishLaunchingWithOptions in there, but you only need to implement the methods you care about.

And now in your App scene, use the UIApplicationDelegateAdaptor property wrapper to tell SwiftUI it should use your AppDelegate class for the application delegate.

struct NewIn14App: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {

SwiftUI is responsible for creating that delegate and looking after its lifetime, so you can go ahead and add any other app delegate functionality to that class to have it called.

Tip: For push notifications, you should probably adjust the delegate property of UNUserNotificationCenter.current() so that it points to a custom class you own.

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.9/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.