iOS 기초

AppDelegate, SceneDelegate

motosw3600 2021. 12. 10. 08:51

AppDelegate

# ios12 버전 이전

  • ios프로젝트를 생성하면 AppDelegate.swift파일 자동적으로 생성
  • AppDelegate는 이름 그대로 앱과 시스템을 연결하기 위해 필요한 Delegate메소드를 포함하고있다.(UIApplicationDelegate)
  • AppDelegate클래스에는 @UIApplicationMain어노테이션 기재되어있다. (이를 통해 앱은 시스템과 연결하기 위한 파일로 인식)

 

# ios13 버전 이후(AppDelegate의 UILifeCycle역할을 SceneDelegate가 수행)

 

 

13버전 이후 AppDelegate의 역할

 

  • 13이후 Scene Session를 관리하는 Session LifeCycle역할이 추가
  • 앱의 가장 중요한 데이터 구조를 초기화
  • LaunchScreen Loading시 수행할 task 정의
  • Scene Configuration
  • 앱 푸시알람 같이 실행시 요구되는 모든 서비스 등록
  • 앱 밖에서 발생한 알림(배터리부족, 다운로드 완료 등) 대응

SceneDelegate

 

  • ios12이전의 AppDelegate에 있던 window객체를 SceneDelegate에서 관리
  • UIWindowSceneDeleagete 채택
  • 13버전 이후부터는 multiple Window를 지원하기 때문에 Scene마다 window를 관리하기 위해서
  • 앱당 하나의 window만 가졌다면 13이후는 여러개의 scene을 가질 수 있음

UILifeCycle method

scene(_ scene: willConnectTo:, options:)

UISceneSession Lifecycle에서 제일 처음 불리는 메소드로 새로운 UIWindow를 생성하고 window의 rootViewController를 설정

sceneDidDisconnect(_:)

scene의 연결이 해제될 때 호출. 연결은 다시 연결될 수도 있다.

sceneDidBecomeActive(_:)

inactive state -> active state

app switcher에서 선택되는 등 scene과 상호작용이 시작될 때 호출

(app switcher란 홈 버튼을 두 번 누르거나 아이폰 화면의 하단에서 위로 스와이프 했을 때 현재 실행 중인 앱들이 보이는 화면을 칭함.)

sceneWillResignActive(_:)

active state -> inactive state

사용자가 scene과의 상호작용을 중지할 때 호출(ex 다른 화면으로의 전환)

sceneWillEnterForeground(_:)

background -> foreground

scene이 foreground로 진입할 때 호출.

sceneDidEnterBackground(_:)

foreground -> background

scene이 background로 진입할때 호출.

'iOS 기초' 카테고리의 다른 글

Frame, Bounds  (0) 2021.12.13
iOS 화면전환  (0) 2021.12.10
init(frame:), required init?(coder: NSCoder), awakeFromNIb()  (0) 2021.12.07
Nib, Xib  (0) 2021.12.07
App LifeCycle  (0) 2021.12.07