[iOS] Scene Delegate

RudinP·2024년 2월 3일
0

Study

목록 보기
131/227

Scene은 iOS 13부터 도입됨

  • 이전에는 한 화면에 한 화면만 표시 가능

Multitasking Split view

  • 앱 실행 시 상단에 점 3개를 누르면 멀티태스킹 가능
  • 동일한 앱을 한 화면에 동시에 띄우는 경우 앱 자체는 하나만 실행된 것임.
    • 앱이 제공하는 화면을 개별 인스턴스로 표시
    • 화면 인스턴스를 scene 이라고 함

Scene-based life-cycle

Unattached

  • 앱이 실행되었고 scene도 만들어졌으나 아직 연결되지 않은 상태

Inactive

  • 앱과 scene이 연결되고 초기화를 거친 상태

Active

  • 화면 표시된 상태
  • Background상태에서 앱을 다시 켜면 Inactive-Active 의 절차를 밟음

Background

  • 다른 앱을 열거나 홈 화면으로 가면 Inactive-Background의 절차를 밟음

Suspended

  • Background에서 작업할 것이 없으면 되는 상태

모든 scene은 독립적이다.

13 이전 버전이거나 scene을 지원하지 않는다면

  • 어플리케이션 이벤트와 화면 이벤트를 AppDelegate 가 모두 처리

13 이후 버전, scene 지원

  • 어플리케이션 이벤트는 AppDelegate에서 처리
    • 하나씩만 만들어짐
  • 화면 이벤트는 SceneDelegate에서 처리
    • 각 scene 인스턴스 별로
    • DidBecomeActive, WillResignActive, DidEnterBackground, WillEnterForeground

AppDelegate에서 화면 이벤트를 처리하지 않는 이유

  • scene은 여러개가 되지만, AppDelegate는 앱 하나당 하나씩만 있기 때문.

scene 생성

// MARK: UISceneSession Lifecycle

// 새로운 scene을 만들때 마다 호출. scene Customizing 부분
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene ConnectionOptions) -> UISceneConfiguration{
  // Called when a new scene session is being created.
  // Use this method to select a configuration to create the new scene with.
  return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
  • 해당 함수의 name: "Default Configuration"은 Info.plist 파일의 Application Scene Manifest 하단 분류의 Configuration Name 부분의 값과 일치해야 한다.
  • scene이 생성되고 위의 Configuration을 토대로 sceneDelegate를 연결해 준 뒤, Storyboard가 로딩된다.
  • 매번 실행되는 것이 아닌, Configuration 파일이 존재하지 않는 경우에만 실행된다. 즉, 이미 이전에 실행했었다면 해당 파일이 존재되기 때문에 이 함수는 호출되지 않는다.(재활용)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>){
  // Called when the user discards a scene session.
  // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
  • scene을 닫은 뒤에 호출
  • scene에서 사용하는 리소스 중 자동으로 정리되지 않는 것을 제거할 때 사용
    • 보통 자동으로 정리되지 않는 일은 없기 때문에 별도로 구현하는 경우는 드뭄.

SceneDelegate

  • scene에서 발생하는 이벤트를 처리
  • storyboardUI의 단위는 viewController. 화면 하나에 viewController 하나인 셈.
  • storyboard를 사용한다면 window 객체는 자동 생성 및 연결.
// scene이 생성되고 나서 앱과 연결되기 직전 호출.
// scene마다 별도의 초기화가 필요하다면 여기에서 구현
func scene(_ :willConnectTo:)

// release(앱과 연결이 끊어지기) 직전에 호출
// scene이 background에 들어간 직후, 혹은 session이 버려질 때 호출
// scene에 대한 id, scene에 대한 상세정보가 들어가므로 나중에 데이터를 복구할 때 필요한 데이터나 리소스가 필요하다면 여기서 구현
func sceneDidDisconnect(_ scene:)

//inactive->active 직후 호출
func sceneDidBecomeAcitve(_ scene:)

//active -> inactive 직전 호출
func sceneWillResignActive(_ scene:)

//background->foreground 직전, 즉 UI 표시 직전호출
func sceneWillEnterForeground(_ scene:)

//foreground->background 직후 호출
func sceneDidEnterBackground(_ scene:)

앱 실행 후 시퀀스

application(_:willFinishLaunchingWithOptions:)
application(_:didFinishLaunchingWithOptions:)
application(_:configurationForConnecting:options:)
scene(_:willConnectTo:options:)
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
//이후 홈 화면으로 갈 경우
sceneWillResignActive(_:)
sceneDidEnterBackground(_:)
//다시 앱을 실행
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
//앱스위처로 종료
sceneWillResignActive(_:)
sceneDidEnterBackground(_:)
sceneDidDisconnect(_:)
application(_:didDiscardSceneSessions:)
applicationWillTerminate(_:)

Split-view 지원 방법

기본적으로 scene을 지원하고 있지만, 다중 윈도우는 기본적으로 설정되어있지 않다.
이를 설정하려면 Info파일에서 Enable Multiple Windows를 Yes로 바꾸어주면 된다.
스플릿 뷰를 위해 앱의 화면이 잠깐 옆으로 빠질때에는 inactive 상태가 되고, 이후 옆에 열 앱을 선택해주면 바로 active 상태가 된다.

호출 순서 디버깅 방법

print(self, #function)

해당 함수를 확인하고자 하는 메소드 안에 작성해두고 콘솔창을 확인하면 된다.

profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글