Language/Swift

iOS - AppDelegate, SceneDelegate

비코딩 2022. 11. 5. 02:53

AppDelegate.swift의 역할

 

1. AppDelegate클래스를 정의

AppDelegate.swift의 존재로 -> AppDelegate 클래스가 생성됨 -> 이 클래스의 인스턴스인 app delegate(우리 앱의 객체)

-> 앱의 상태에 따라 응답하는 콘텐츠가 그려지는 창(window)을 만듬

 

2. entry point와, 앱의 입력 이벤트를 전달하는 run loop를 생성

 

Apple이 기본적으로 제공하는 AppDelegate 메소드

 

func application(_: didFinishLaunchingWithOptions: ) -> Bool

: 메서드 안에서 application setup 진행

 

func application(_: configurationForConnecting:options: ) -> UISceneConfiguration

: application 새로운 scene/window 제공하려고 불리는 메서드

 

func application(_: didDiscardSceneSessions: )

: 사용자가 scene 버릴 불리는 메서드 (swipe multitasking windwo 없앤다던지, 코드로서 없애는 경우 등등)

 

 

SceneDelegate

화면에 무엇(scene/window) 보여줄지 관리하는 역할을 한다.

 

Apple이 기본적으로 제공하는 SceneDelegate 메소드

 

scene(_: willConnectTo: options: )

: UISceneSession lifecycle에서 제일 처음 불리는 메소드로  content view, 새로운 UIWindow 생성하고 window rootViewController 설정한다. 과거에 disconnected UI 되돌릴 때도 쓰기도 한다

 

sceneWillEnterForeground(_ :)

: scene foreground 전환될 불리는 메소드 (background → foreground 되었을 or 처음 active 상태가 되었을 )

 

sceneDidBecomeActive(_ :)

: scene setup 되고 화면에 보여지면서 준비가 완료되면 불리는 메소드 (active 상태)

 

sceneWillResignActive(_ :)

: active 상태에서 inactive 상태로 전환될 불리는 메소드 (전화가 왔을 )

 

sceneDidEnterBackground(_ :)

: scene foreground에서 background 전환 불리는 메소드. 다음에 다시 foreground 돌아 복원할 있도록 state 정보를 저장하거나, 데이터를 저장, 공유 자원 돌려주는 등의 일을 하도록 해야한다.

 

sceneDidDisconnect(_ :)

: scene background 들어가서 disconnect(App 종료 X, session에서 끊어짐O) 시스템이 자원을 확보하기 위해 불리는 메소드. 생성이 쉬운 데이터들은 돌려주고(필요 없는 자원은 돌려주고), 사용자의 input 같이 재생성이 어려운 데이터는 갖고 있게끔 하는 작업 해야한다.

 

[참고 : https://www.sueaty.com/134]

[참고 : https://zeddios.tistory.com/218]