본문 바로가기

IT/iOs

SceneDelegate란

SceneDelegate란

 

 

SceneDelegate을 살펴보겠습니다. Xcode 11부터 SceneDelegate가 기본 iOS 앱 프로젝트 템플릿으로 자동 추가됩니다. 

SceneDelegate은 swiftUI에만 속해있는 것은 아닙니다.

 

iOS 13 이상에서는 씬 델리게이트(scene delegate)가 앱 델리게이트의 일부 역할을 담당합니다. 중요한 것은 window의 개념이 scene 개념으로 대체된다는 것입니다. 앱에는 둘 이상의 scene이 있을 수 있으며 이제 scene은 앱의 사용자 인터페이스 및 콘텐츠의 배경으로 사용됩니다. 특히 재미있는 점은 scene이 있는 하나의 앱을 갖는 개념은 iOS 및 iPadOS에서 다중 창 앱을 빌드할 수 있습니다. 

 

 

자동으로 생성되는 SceneDelegate 에는 active, resign and disconnect와 같은 친숙한 라이프사이클 이벤트가 있습니다. SceneDelegate은 delegate을 사용하며 일반적으로 모든 장면에 응답한다는 점에 유의해야합니다. 하나의 델리게이트를 정의하여 앱의 모든 scene에서 사용합니다.

AppDelegate 에는  scene sessions에 관련된 application(_:configurationForConnecting:options:) 와 application(_:didDiscardSceneSessions:) 함수가 추가됐습니다.

 

SceneDelegate에 있는 함수들을 살펴보겠습니다.

 

 

 

 

 

scene (_ : willConnectTo : options :)

가장 중요한 기능은 입니다. iOS 12의 application (_ : didFinishLaunchingWithOptions :) 함수와 유사합니다. scene이 앱에 추가될 때 호출됩니다.

 

sceneDidDisconnect(_:) 

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

 

sceneDidBecomeActive(_:) 

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

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

 

sceneWillResignActive(_:) 

사용자가 scene과의 상호작용을 중지할 때 호출됩니다. (다른 화면으로의 전환이 예입니다.)

 

sceneWillEnterForeground(_:) 

scene이 포그라운드로 진입할 때 호출됩니다.

 

sceneDidEnterBackground(_:)

scene이 백그라운드로 진입할때 호출됩니다.

 

 

disconnect와 active, forground와 background 이 대칭성을 보았을 때 우리가 계속 경험해왔던 라이프사이클과 다를 것이 없다는 것이 보입니다.

 

 

 

위에서 언급했던 AppDelegate에 추가된 두 개의 메서드에서는 scene sessions을 관리합니다.

scene sessions은 앱에서 생성한 모든 scene의 정보를 관리합니다.

 

AppDelegate의 새로 추가된 함수를 살펴보겠습니다.

 

application (_ : configurationForConnecting : options :)

scene을 만들 때 구성 객체를 반환해야 합니다.


application (_ : didDiscardSceneSessions :)

사용자가  app switcher를 통해 scene을 닫을 때 호출됩니다.

 

 

 

 

 

SceneDelegate 사용 안하는법 (How to not use SceneDelegate)

 

1. info.plist의 Application Scene Manifest 항목을 삭제한다. (Delete Application Scene Manifest in info.plist )

 

 

2. SceneDelegate.swift 파일을 삭제한다 (Delete SceneDelegate file)

 

 

 

3. AppDelegate에 아래 두개의 메소드를 삭제한다. (Delete the appDelegate method below)

   

// MARK: UISceneSession Lifecycle

 

    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)

    }

 

    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.

    }

 

 

 

 

#ios13 scenedelegate

#ios13 appdelegate

#ios13 SceneDelegate 와 AppDelegate 비교

 

 

 

https://jercy.tistory.com/11

https://stackoverflow.com/questions/56498099/difference-between-scenedelegate-and-appdelegate

https://learnappmaking.com/scene-delegate-app-delegate-xcode-11-ios-13/

'IT > iOs' 카테고리의 다른 글

UIBackgroundTaskIdentifier vs BGTaskScheduler  (0) 2019.11.05
Can't end BackgroundTask error  (7) 2019.10.31
iOS13 touchid issue  (0) 2019.10.22
맥 카탈리나 코코아팟 오류  (0) 2019.10.19
Realm 사용법  (0) 2019.10.12