간단한 선수지식

Scene 은 하나의 독립적인 UI 인스턴스를 가리킵니다.

Untitled

오직 한 개만 존재하는 앱 인스턴스는 연결되어 있는 씬들의 참조를 유지합니다.

UIApplication.shared.connectedScens

중요한 것은, AppDelegate 시절의 Life Cycle 과 달리

다수의 씬이 동시에 존재할 수 있기 때문에, 현재 화면을 표시하는 씬을 찾아주는 로직이 필요하다는 것입니다.

활용처

코드 조각

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references this current scene (itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Save the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// Here is a convenient view controller extension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

참고자료

iOS) Scene-basedScene-support 의 차이점