[iOS] AppDelegate와 SceneDelegate, App의 생명주기
iOS

[iOS] AppDelegate와 SceneDelegate, App의 생명주기

728x90

AppDelegate와 SceneDelegate

iOS 13부터 바뀐점

  • 바뀌기 이전(~iOS12)
  • 바뀐 후 (iOS13)

1. iOS12까지는 대부분 하나의 앱에 하나의 window였지만 iOS 13부터는 window의 개념이 scene으로 대체되고 하나의 앱에서 여러개의 scene을 가질 수 있음

2. AppDelegate의 역할 중 UI의 상태를 알 수 있는 UILifeCycle에 대한 부분을 SceneDelegate가 하게 됨

3. 그리고 AppDelegate에 Session Lifecycle에 대한 역할이 추가됨

  • Session LifeCycle에는 두 메소드가 존재한다.
  • 각각의 메소드는 Scene Session이 생성되거나 삭제될 때 AppDelegate에 알리는 역할을 한다.
  • Scene Session은 앱에서 생성한 모든 scene의 정보를 관리한다.

 

iOS13부터 AppDelegate가 하는 일?

  1. 앱의 가장 중요한 데이터 구조를 초기화하는 것
  2. 앱의 scene을 환경설정(Configuration)하는 것
  3. 앱 밖에서 발생한 알림(배터리 부족, 다운로드 완료 등)에 대응하는 것
  4. 특정한 scenes, views, view controllers에 한정되지 않고 앱 자체를 타겟하는 이벤트에 대응하는 것
  5. 애플 푸쉬 알림 서비스, 위치서비스, 앱종료와 같이 실행시 요구되는 모든 서비스를 등록하는것

 

iOS13부터 SceneDelegate가 하는 일?

앱 Scene기반 생명 주기 이벤트를 관리

 

🌟 정리

앱 실행 및 종료와 관련된 Process Life Cycle은 AppDelegate에서,

앱이 Foreground와 Background 상태에 있을 때 상태 전환과 관련된 UI Life Cycle은 SceneDelegate에서 관리한다.

 

App의 생명주기

App State

 

1. Not Running

  • 앱이 아직 실행되지 않았거나, 완전히 종료되어 동작하지 않는 상태.

2. Inactive - Foreground

  • Inactive는 app이 실행중이지만 사용자로부터 event를 받을 수 없는 상태.
  • multitasking window로 진입하거나 app 실행중 전화, 알림 등에 의해 app을 사용할 수 없게 되는 경우 이 상태로 진입.

3. Active - Foreground

  • Active는 app이 실제 실행중이고 사용자 event를 받아서 상호작용할 수 있는 상태.
  • (바로 Active가 되지 않고 Inactive 상태를 거쳐 Active상태가 된다.)

4. Running - Backgound

  • Background는 홈화면으로 나가거나 다른 app으로 전환되어 현재 app이 실질적인 동작을 하지 않는 상태.
  • 예를 들어 Music app을 닫아도 재생한 음악이 계속 실행되는 경우.
  • 사용자가 app을 사용하지 않는 동안 서버와 데이터를 동기화하거나 타이머가 실행되어야 하는 등의 작업을 이 상태에서 할 수 있습니다.

5. Suspended - Backgound

  • Suspended는 app을 다시 실행했을 때 최근 작업을 빠르게 로드하기 위해 메모리에 관련 데이터만 저장되어있는 상태.
  • app이 background 상태에 진입했을 때 다른 작업을 하지 않으면 Suspended 상태로 진입하게 됩니다.(보통 2~3초 이내)
  • Suspended 상태의 app들은 iOS의 메모리가 부족해지면 가장 먼저 메모리에서 해제됨. 따라서 app을 종료시킨 적이 없더라도 app을 다시 실행하려고 하면 처음부터 다시 실행됩니다.

App Life Cycle

iOS13 이전 AppDelegate의 App Life Cycle

1. 앱 실행

: Not Running - Inactive - Active

앱 클릭하여 실행

  • application(_:didFinishLaunchingWithOptions:)
  • applicationDidBecomeActive(_:)

2. To Background

: Active - Inactive - Background

앱에서 다른 앱으로 이동할 때 or 홈화면으로 이동

  • applicationWillResignActive(_:)
  • applicationDidEnterBackground(_:)

3. To Foreground

: Background - Inactive - Active

  • applicationWillEnterForeground(_:)
  • applicationDidBecomeActive(_:)

4. 앱 종료

: (some States) - Background or Suspended - Not Running

앱 사용하다가 종료

  • applicationWillTerminate(_:)

 

iOS13 이후 SceneDelegate 생성 후 App Life Cycle

 

1. 앱 실행 + Scene 연결

: Not Running - Inactive - Active

  • application(_:didFinishLaunchingWithOptions:)
  • application(_:configurationForConnecting:options:)
  • scene(_:willConnectTo:options:)
  • sceneDidBecomeActive(_:)

2. To Background

: Active - Inactive - Background

  • sceneWillResignActive(_:)
  • sceneDidEnterBackground(_:)

3. To Foreground

: Background - Inactive - Active

  • sceneWillEnterForeground(_:)
  • sceneDidBecomeActive(_:)

4. Scene 연결 해제 + 앱 종료

: (some States) - Background or Suspended - Not Running

  • sceneDidDisconnected(_:)
  • application(_:didDiscardSceneSessions:)
  • applicationWillTerminate(_:)

 

[출처]

https://velog.io/@rnfxl92/앱-생명주기-Application-Life-Cycle

https://velog.io/@dev-lena/iOS-AppDelegate와-SceneDelegate#appdelegate와-scenedelegate

728x90

'iOS' 카테고리의 다른 글

[iOS] UIStackView 정리  (0) 2022.07.12
[iOS] Xcode error 대처 방법  (0) 2022.06.06
[iOS] ViewController 생명 주기  (0) 2022.01.20
[iOS] Frame & Bounds  (0) 2022.01.20
[iOS] Snapkit 라이브러리로 AutoLayout 잡기  (0) 2021.12.30