Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- Animation
- SWIFTUI
- singleton
- error
- Refresh
- SWIFT
- class
- Git
- protocol
- 화면전환
- self
- 글또
- 고차함수
- mvvm
- IOS
- escaping
- http
- viewlifecycle
- Switch
- ScrollView
- array
- uikit
- Observer
- PushNotification
- segue
- struct
- apns
- calendar
- NotificationCenter
- list
Archives
- Today
- Total
seong_hye, the developer
UIKit) AppDelegate & SceneDelegate 알아보기 본문
UIKit을 사용하기 위해 프로젝트를 만들게 되면
만들어져있는 기능들에 대해 알아보려 한다
📘 AppDelegate
iOS 앱의 생명 주기(lifeCycle)와 시스템 이벤트를 관리하는 핵심 클래스
앱이 실행될 때부터 종료되기까지의 흐름을 제어하며,
앱 수준의 이벤트(ex_푸시 알림, 백그라운드 진입, 외부 URL 열기 등)를 처리하는 데 사용
AppDelegate = iOS 앱 전체의 실행 상태와 시스템 이벤트를 처리하는 앱 생명 주기 관리자 클래스
🔹기본 구조
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any) -> Bool {
print("앱이 시작됨")
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("앱이 백그라운드로 이동함")
}
func applicationWillENterForeground(_ application: UIApplication) {
print("앱이 포그라운드로 돌아옴")
}
...
}
🔹주요 메서드
메서드 | 설명 |
didFinishLaunchingWithOptions | 앱이 실행될 떄 최초로 호출됨 (초기화 작업) |
applicationDidBecomeActive | 앱이 활성화되었을 때 (포그라운드 전환 후) |
applicationWillResignActive | 앱이 비활성화되기 직전 (전화 수신 등) |
applicationDidEnterbackground | 앱이 백그라운드로 전환됨 |
applicationWillEnterForeground | 다시 포그라운드로 돌아올 때 |
applicationWillTerminate | 앱이 종료되기 직전(iOS에서는 드뭄) |
didReceiveRemoteNotification | 푸시 알림 수신 처리 |
🔹하는 역할
역할 | 설명 |
앱 초기 설정 | SDK 초기화, 로깅, Firebase 등 설정 |
알림 처리 | 푸시 알림 등록 및 수신 |
백그라운드 관리 | 작업 저장, 정지, 종료 처리 |
외부 URL 핸들링 | ex) 카카오 로그인, 딥링크, 결제 콜백 등 |
상태 전환 대응 | 포그라운드/백그라운드 진입 처리 |
🔹사용 위치
- UIKit 앱에서 기본적으로 사용
- SwiftUI 앱에서도 @UIApplicationDelegateAdaptor를 사용하면 연동 가능
class AppDelegate: NSObject, UiApplicationDelegate {
func applicationDidEnterBackground(_ application: UIApplication) {
print("hi")
}
}
@main
struct TestApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
📘 SceneDelegate
iOS 13부터 도입된 개념으로, 멀티 윈도우(멀티 Scene) 환경을 지원하기 위해 UI상태를 관리하는 역할
하지만 iOS 14 ~ 15 이후 SwiftUI와 함께 App 프로토콜 구조가 등장하면서 점점 덜 사용됨
여전히 UIKit 기반 프로젝트에선 중요한 개념
SceneDelegate = 앱의 UI씬(윈도우)을 관리하고, 각 씬의 생명 주기를 제어하는 클래스
🔹등장 배경
iOS 12까지는 AppDelegate 하나가 앱의 UI 상태와 생명주기를 담당했음iOS 13부터는 앱이 멀티 윈도우 (iPad 멀티 작업 등)를 지원할 수 있도록 SceneDelegate가 UI와 화면 흐름을 분리해서 관리하게 되었음
🔹기본 구조
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}
let window = UIWindow(windowScene: windowScene)
let rootVC = ViewController() // 첫 화면 뷰 컨트롤러
window rootViewController = rootVC
self.window = window
window.makeKeyAndVisible()
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("Scene 활성화됨")
}
func sceneWillResignActive(_ scene: UIScene) {
print("Scene 비활성화됨")
}
...
}
➡️ scene 함수에서 Navigation도 설정할 수 있음
🔹SceneDelegate가 필요한 경우
- UIKit 앱 (스토리보드 X)에서 직접 루트 뷰 설정할 때
- 멀티 윈도우 지원 (iPad 등)
🔹AppDelegate vs SceneDelegate
역할 | AppDelegate | SceneDelegate |
앱 전체 생명주기 | O | X |
UI 윈도우 관리 | X | O |
멀티 윈도우 지원 | X | O |
네트워크, 알림, 백그라운드 처리 등 | O | X |
초기 뷰 설정 (iOS 13+) | - | 보통 여기서 진행됨 |
'IOS > UIKit' 카테고리의 다른 글
UIKit) View Life Cycle (0) | 2022.10.26 |
---|---|
Swift)SnapKit에 대해 알아보기 (0) | 2022.10.24 |
Swift) 문법 정리 - 생성자 (0) | 2022.08.02 |
Swift) 코드 스니펫(Code Snippet) (0) | 2022.07.26 |
UIKit) 화면 전환 방식 (0) | 2022.07.19 |
Comments