Day 33 - TIL
App delegate와 Scene delegate의 심화 버전.
📘 Day 33 - Today I Learned
🔍 Today’s Error or Issue
오늘은 매니저님께서 “이번엔 AppDelegate와 SceneDelegate에 대해 언제, 어떻게 사용하는 건지 알아보면 좋겠어요!” 라는 말을 듣고, 단순히 이름과 역할만 알고 있었던 것에서 벗어나 구체적인 동작 방식과 실무 적용 포인트까지 살펴보게 되었다.
App Delegate: 앱 전체 생명 주기의 시작과 끝
📌 주요 역할
메서드 | 설명 |
---|---|
pplication(_:didFinishLaunchingWithOptions:) | 앱이 시작되고 최초 실행될 때 호출됨 |
applicationWillResignActive(_:) | 앱이 비활성 상태가 되기 직전 (전화 수신 등) |
applicationDidEnterBackground(_:) | 앱이 백그라운드로 진입 |
applicationWillEnterForeground(_:) | 백그라운드에서 다시 돌아올 때 |
applicationDidBecomeActive(_:) | 앱이 활성화될 때 |
applicationWillTerminate(_:) | 앱이 종료되기 직전 |
AppDelegate는 앱 자체의 상태 변화, 알림, 백그랑누드 동작, 푸시토큰 처리 등 앱 전역 설정 및 상태 관리를 담당합니다.
🧪 예제 코드 - AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("앱이 시작됨")
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("앱이 백그라운드로 진입함")
}
func applicationWillTerminate(_ application: UIApplication) {
print("앱이 종료되기 직전")
}
}
Scene Delegate: 화면(윈도우) 관리
iOS 13 부터 iPad에서 멀티 윈도우(Split View 등) 기능이 도입되며 등장 즉, Scene은 하나의 화면(또는 윈도우)을 의미하고, 여러 Scene이 동시에 존재할 수 있다. | 메서드 | 설명 | |—|—| | scene(:willConnectTo:options:) | 앱이 실행되어 화면을 연결할 때 (필수 구현) | | sceneDidDisconnect(:) | Scene이 종료될 때 | | sceneDidBecomeActive(:) | Scene이 활성화될 때 | | sceneWillResignActive(:) | Scene이 비활성화될 때 | | sceneWillEnterForeground(:) | 백그라운드에서 포그라운드로 진입할 때 | | sceneDidEnterBackground(:) | 포그라운드에서 백그라운드로 나갈 때 |
SeneDelegate는 앱의 “화면 단위 생명 주기”를 담당하며, rootViewController 설정, 딥링크 핸들링, 화면 상태 전환을 여기서 처리한다.
🧪 예제 코드 - SceneDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
print("Scene 연결됨")
}
func sceneDidEnterBackground(_ scene: UIScene) {
print("Scene이 백그라운드로 이동함")
}
}
추가적으로
- SwiftUI 앱에서는 @main과 App 프로토콜을 사용하며, AppDelegate/SceneDelegate는 사라지거나 대체됩니다.
- Scene을 사용하지 않고 싶다면 Info.plist에서 UIApplicationSceneManifest를 삭제하면 iOS 13 이상에서도 SceneDelegate를 사용하지 않게 설정할 수 있습니다.
- 앱의 상태를 전역적으로 감지해야 한다면 여전히 AppDelegate이 유용합니다 (예: 푸시 알림, 백그라운드 Task 등록 등).
📘 Lesson Learned
단순히 개념적으로 알고 있던 AppDelegate와 SceneDelegate의 차이점을 “앱 전역 관리 vs 화면 단위 관리” 라는 관점에서 명확하게 정리할 수 있었다. 앞으로 앱 설계 시 어떤 처리를 어디에 위치시켜야 할지 기준이 생겼고, 실무에서 멀티 윈도우 지원 여부에 따라 SceneDelegate의 유무를 결정하는 판단 기준도 이해하게 되었다.