본문 바로가기

iOS/Tip

[iOS 13] AppDelegate, SceneDelegate에서 RootViewController 설정하기

안녕하세요.

요즘 오늘의지출 앱을 만드느라 정신이 없네요 ㅎㅎ;

저는 항상 RootViewController를 소스로 지정해서 사용하고 있는데요
오늘의지출 앱을 만들면서 겪었던 난해한 부분과 해결법을 공유하고자 합니다.

Xcode 11.0 부터 새로운 프로젝트를 생성시 AppDelegate와 SceneDelegate가 생성되는데요
기존처럼 AppDelegate에서 RootViewController를 설정했더니 iOS13 기기에서 테스트하니 Crash가 나더군요..

열심히 삽질한 결과 iOS13 이전과 이후의 방식이 다르다는걸 찾았습니다.


iOS13 이전의 버전은 기존과 같이 AppDelegate에 있는 didFinishLaunching에서 처리해주시면 됩니다.

//
//  AppDelegate.swift
//

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 13, *) {
            print("set in SceneDelegate")
        } else {
            let window = UIWindow(frame: UIScreen.main.bounds)
            window.rootViewController = RootTabBarViewController()
            self.window = window
            window.makeKeyAndVisible()
        }
        
        return true
    }

 


iOS13 이후에서는 새롭게 추가된 SceneDelegate에서 관리하게 되더군요
아래와 같이 willConnectTo에서 처리해주시면 됩니다.

//
//  SceneDelegate.swift
//

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = RootTabBarViewController()
            self.window = window
            window.makeKeyAndVisible()
        }
        
        guard let _ = (scene as? UIWindowScene) else { return }
    }

UIWindowSceneDelegate가 새롭게 생겼는데 하루 빨리 익숙해져야 겠습니다 ㅠ

더 좋은 방법이 있으면 댓글이나 메일로 공유해주시면 감사하겠습니다.


추가 : 2019년 11월 19일

SceneDelegate를 사용하지 않을 분들은 SceneDelegate 파일을 프로젝트에서 삭제하고
AppDelegate에서 SceneDelegate관련된 소스를 지우시면 기존과 같이 사용할 수 있습니다.


해피코딩 :)