In a SwiftUI app, how do you receive the push-notification device token through @UIApplicationDelegateAdaptor, and what should stay out of that delegate?
answer
- SwiftUI owns the delegate instance
- declare it once, only in App
- observable delegate joins the environment
- life cycle belongs to scenePhase
basics
~10 sWrite an NSObject class conforming to UIApplicationDelegate that implements application(_:didRegisterForRemoteNotificationsWithDeviceToken:), and declare it once in the App with @UIApplicationDelegateAdaptor. SwiftUI creates it and forwards callbacks; foreground and background handling stays with scenePhase.
solid answer
~40 sThe SwiftUI life cycle has no app delegate unless you ask for one. I write a class that inherits from `NSObject` and conforms to `UIApplicationDelegate`, implement `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)` to record the token, and declare `@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate` in the `App` struct. SwiftUI instantiates the delegate and calls it on life cycle events; declaring the adaptor more than once is a runtime error. If the delegate also conforms to `ObservableObject`, SwiftUI puts it in the environment, so a settings screen can read the token with `@EnvironmentObject`. What stays out is what SwiftUI already models: Apple's documentation says to prefer `ScenePhase` changes over callbacks like `application(_:didFinishLaunchingWithOptions:)`, and windows come from scenes, not from setting a root view controller.
code
swift · 25 linesimport Combine
import SwiftUI
import UIKit
final class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
@Published var deviceToken: Data?
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
self.deviceToken = deviceToken
}
}
@main
struct EditorApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
var body: some Scene {
WindowGroup {
Text("Editor")
}
}
}go deeper
Recall that SwiftUI apps add an app delegate with @UIApplicationDelegateAdaptor in the App struct when a UIKit callback is needed.
Explain who creates the delegate, how an ObservableObject delegate reaches views through the environment, and the declare-once rule.
Show where each concern belongs: token in the delegate, transitions in scenePhase, scene events in a scene delegate from the configuration.
Set a team rule for what may live in the delegate, so it stays a thin bridge to system callbacks and never becomes a second life cycle.
## Why an adaptor exists An app that declares an `App` type has no UIKit app delegate of its own: SwiftUI drives launch, scenes and windows. Some system services still talk only to a `UIApplicationDelegate`, and remote-notification registration is the textbook case, because the device token arrives through a delegate callback. **`UIApplicationDelegateAdaptor`**, available since iOS 14, is the property wrapper that bridges the gap: you describe a delegate type, and SwiftUI creates it and routes UIKit's callbacks to it. ## Wiring the push token 1. Define a class that inherits from `NSObject` and conforms to `UIApplicationDelegate`. 2. Implement `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)` and store or upload the token there. The request to register for remote notifications is still made through UIKit's application object; the adaptor only gives you somewhere to receive the answer. 3. In the `App` struct, declare `@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate`. 4. Do not create the delegate yourself. SwiftUI instantiates it and calls its methods in response to life cycle events. Apple's documentation adds a hard rule: define the adaptor **only in the `App` declaration, and only once** for a given app. Declaring it more than once makes SwiftUI generate a runtime error. ## Reaching the delegate from views If the delegate class also conforms to `ObservableObject`, SwiftUI puts the instance it creates into the environment. Any scene or view can then read it with `@EnvironmentObject private var appDelegate: AppDelegate`, and `@Published` properties on it, such as the latest device token, can drive the interface or produce bindings. Since iOS 17 the SDK also declares an adaptor initializer for delegates that use Observation's `@Observable`. Apple's page documents the automatic environment placement for `ObservableObject` delegates, so with the Observation variant, handing the delegate's data to views explicitly is the safe route. The wrapper is main-actor isolated, like `App` itself, so delegate callbacks and the views reading them meet on the main actor. ## Keeping the delegate thin Because SwiftUI creates the delegate, the `App` cannot pass dependencies into its initializer. Two patterns keep it small anyway: - The delegate exposes the little it receives, such as the latest token, as published or observable state, and the rest of the app reacts to that. - The `App` reads the delegate through the wrapper's `wrappedValue` (the `appDelegate` property) and hands what it needs to the services it owns, so upload logic, retries and account binding live in ordinary types that tests can create directly. Either way the delegate stays a translator from UIKit callbacks to app state, not a home for business logic. ## Scene delegates for scene-level events Some events are delivered to a scene delegate rather than the app delegate, such as an app shortcut chosen from the Home Screen. SwiftUI supports this through the app delegate: - Implement `application(_:configurationForConnecting:options:)` in the adaptor's delegate and return a `UISceneConfiguration`. - Set only its `delegateClass` to a type conforming to `UIWindowSceneDelegate`; no scene class or storyboard is needed. - SwiftUI creates and manages that scene delegate, sends it the relevant callbacks, and, if it is an `ObservableObject`, places it in the environment as well. ## What stays out of the delegate | Concern | Where it belongs in a SwiftUI app | |---|---| | Device token for remote notifications | App delegate, through the adaptor | | Foreground, inactive and background transitions | `scenePhase` read from the environment | | Creating windows and root views | Scenes such as `WindowGroup` | | App-wide models | Owned by the `App` and injected through the environment | | Scene-level system events such as app shortcuts | A scene delegate returned from the app delegate's configuration | Apple's guidance is to manage life cycle events **without an app delegate whenever possible**, and to prefer `ScenePhase` changes over delegate callbacks like `application(_:didFinishLaunchingWithOptions:)`. A delegate that grows into a second life cycle, with background saves and UI state, fights the scene-based model and hides per-window behaviour behind app-wide callbacks. ## Pitfalls - Declaring the adaptor in a view or in two places, which triggers SwiftUI's runtime error. - Constructing the delegate by hand and expecting UIKit to call that instance. - Creating a window or root view controller in the launch callback; scenes own the windows now. - Putting per-window saving in the delegate instead of reacting to each scene's phase.
- How does a SwiftUI app handle an app shortcut that UIKit delivers to a scene delegate?Return a UISceneConfiguration from the app delegate's application(_:configurationForConnecting:options:), setting only its delegateClass to a type conforming to UIWindowSceneDelegate. SwiftUI creates and manages that scene delegate and forwards callbacks such as windowScene(_:performActionFor:). If it is an ObservableObject, it lands in the environment too.
- What goes wrong if a view also declares @UIApplicationDelegateAdaptor for the same delegate type?Apple's documentation says to define the adaptor only in the App declaration and only once; declaring it more than once makes SwiftUI generate a runtime error. Views that need the delegate read it from the environment with @EnvironmentObject when it is an ObservableObject, or receive its data explicitly.
- A SwiftUI app cannot get a push token without going back to a UIKit @main app delegate.
- You create the delegate with AppDelegate() yourself and hand it to UIKit.
- Declaring @UIApplicationDelegateAdaptor in several views is fine because SwiftUI shares one instance.
- App delegate background callbacks are the preferred place for SwiftUI apps to save state.
- The launch callback should still build the window and root view controller.