SwiftUI app life cycle iOS14 where to put AppDelegate code?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In the SwiftUI app lifecycle introduced for iOS 14, most startup code moves into the App type, not into a traditional AppDelegate. But if you still need delegate-based integration for push notifications, SDK setup, or old UIKit lifecycle hooks, SwiftUI lets you attach an AppDelegate explicitly.
Put Simple Startup Code in the App Type
For app-wide object creation and scene setup, the @main app struct is the new default home.
This is the right place for:
- dependency injection
- root environment objects
- scene configuration
- lightweight startup decisions
If the code is not specifically tied to UIApplicationDelegate, it usually belongs here instead of in a recreated old delegate pattern.
Use @UIApplicationDelegateAdaptor When You Need an App Delegate
Some frameworks still expect UIApplicationDelegate callbacks. SwiftUI supports that through @UIApplicationDelegateAdaptor.
This is the standard answer when you still need delegate methods in a SwiftUI lifecycle app.
Use scenePhase for Foreground and Background State
Developers sometimes move every old lifecycle method into the delegate even when SwiftUI already gives a better hook. For foreground and background transitions, scenePhase is often the more natural SwiftUI solution.
This is better than forcing background and foreground logic into AppDelegate just because older UIKit apps did it that way.
How to Decide Where Code Belongs
A practical rule is:
- use the
Appstruct for app composition and SwiftUI-owned state - use
scenePhasefor scene lifecycle observation - use
AppDelegateonly for APIs that still require delegate callbacks
Examples of delegate-worthy code include:
- push notification registration callbacks
- app launch integration with certain SDKs
- URL handling patterns that still depend on delegate methods
The question is not "Where do I put all old AppDelegate code?" The better question is "Which parts still need delegate semantics?"
What Usually Does Not Need AppDelegate Anymore
A lot of older UIKit setup code can disappear or move into more focused SwiftUI structures. View hierarchy setup belongs in the Scene body, shared model construction can live in @StateObject or dependency containers, and scene transitions can often be handled with scenePhase.
That is why many SwiftUI apps still define an AppDelegate, but keep it very small. The delegate should be the exception layer for UIKit integration, not the default home for every startup concern.
Common Pitfalls
- Moving all legacy startup code into
AppDelegateeven when SwiftUI offers a better native hook. - Forgetting
@UIApplicationDelegateAdaptorand expecting a manually defined delegate class to run by itself. - Using
AppDelegatefor scene lifecycle tracking that belongs inscenePhase. - Recreating a full UIKit lifecycle structure when the app is otherwise SwiftUI-first.
- Mixing SwiftUI state management and UIKit delegate logic without clear ownership boundaries.
Summary
- In iOS 14 SwiftUI apps, the
Apptype is the default home for startup and composition code. - Use
@UIApplicationDelegateAdaptoronly when an actualUIApplicationDelegatecallback is still needed. - Use
scenePhasefor active, inactive, and background transitions. - Keep UIKit delegate code narrow and intentional.
- Porting old lifecycle code is usually a refactoring exercise, not a direct copy-paste into a new app structure.
Related reading
- SwiftUI Binding Initialize
- SwiftUI can't tap in Spacer of HStack
- SwiftUI can't tap in Spacer of HStack
- SwiftUI. How to change the placeholder color of the TextField?
- SwiftUI How to implement a custom init with Binding variables
- SwiftUI NavigationLink loads destination view immediately, without clicking
- SwiftUI NavigationView navigationBarTitle LayoutConstraints issue
- SwiftUI State var initialization issue
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.