tezvyn:

The AppDelegate: Your App's Central Command

AI-drafted, machine-checkedSource: developer.apple.combeginner
The AppDelegate: Your App's Central Command

Think of the AppDelegate as your app's command center, receiving critical notifications from the OS. It handles core events like launch, termination, or receiving a push notification. The footgun is bloating it with logic that belongs elsewhere.

WHY IT EXISTS: iOS apps need a single, reliable entry point to respond to system-level events that happen outside their direct control. Events like the user pressing the Home button, the device rotating, or the system warning of low memory require a central coordinator. The AppDelegate provides this essential hub for the entire application lifecycle.

THE MENTAL MODEL: The AppDelegate is your application's most trusted deputy. The iOS operating system, represented by the UIApplication instance, doesn't talk to your individual view controllers or data models directly. Instead, it sends notifications about crucial events to your AppDelegate. The AppDelegate then interprets these events and orchestrates the response across the rest of your app. It's the main point of contact between your code and the OS.

HOW IT WORKS: At launch, iOS creates a singleton UIApplication object and your AppDelegate object. It then sets your AppDelegate as the delegate of the UIApplication instance. This establishes a communication channel. When a significant system event occurs, the UIApplication object calls a corresponding method on its delegate. For example, when the app finishes launching, the system calls application(_:didFinishLaunchingWithOptions:). You implement these methods in your AppDelegate.swift file to inject your custom logic at key moments in the app's life.

WHEN TO USE IT: The AppDelegate is the correct place for logic that is truly global and tied to the application's lifecycle. This includes one-time setup tasks at launch, like initializing core services (logging, analytics, crash reporting). It's also where you handle app state transitions, such as saving user data when the app is about to enter the background, and processing incoming remote notifications.

WHEN NOT TO USE IT: Avoid putting business logic, UI setup (beyond creating the initial window and root controller), or specific network calls directly in the AppDelegate. A bloated AppDelegate is a classic architectural anti-pattern. It makes code hard to test, reuse, and reason about. This logic should be delegated to specialized objects like view controllers or data managers that are managed by the AppDelegate, but not part of it.

ONE CANONICAL EXAMPLE: The most fundamental job of the AppDelegate is to set up the app's initial user interface. In the application(_:didFinishLaunchingWithOptions:) method, you typically create a UIWindow object, instantiate your initial view controller, set it as the window's rootViewController, and then make the window the key window and visible. This single sequence of actions is what puts your app's first screen in front of the user.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.