로그인이 필요한 앱의 경우 사용자가 로그인을 했는지의 여부에 따라 첫 시작 화면이 달라지곤 한다.
내가 진행하고 있는 프로젝트에도 이러한 기능이 필요하기에 간단히 정리해 보고자 한다.

iOS13 이후 아이패드에서 멀티 윈도우를 지원하게 되면서 하나의 앱에 여러 화면이 동시에 요구되는 상황이 발생했고 이를 위해 여러 scene을 관리하는 SceneDelegate가 필요하게 되었다.

이처럼 iOS12까지는 하나의 앱이 하나의 화면만 관리하였기에 AppDelegate에서 UI LifeCycle과 Process LifeCycle을 함께 관리하였으나,


iOS13 이후, UI LifeCycle을 SceneDelegate에서 관리하게 되었고 AppDelegate에는 Session LifeCycle이 추가되었다.

iOS12 이전을 Development Target으로 삼았다면 SceneDelegate를 삭제한 후 AppDelegate에서 rootViewController를 처리해주면 되지만 이번 프로젝트에서는 iOS14 이상에서만 동작하는 기능이 있기 때문에 그대로 SceneDelegate에서 rootViewController를 처리해주었다.
사실 거창한 기능은 아니고 위젯 기능이 필요해서 그렇다.

방법은 간단하다. AppDelegate.swift의 application(_ application: , didFinishLaunchingWithOptions launchOptions: ) -> Bool 메서드에 작성할 내용을 SceneDelegate에 작성하면 끝이다.

// SceneDelegate.swift

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: scene)

    // 아카이빙된 토큰 값을 불러오는 코드는 생략하였다.

    if UserInfoManager.tokens == nil {
    	startWithLoginView()
    } else {
    	startWithMainView()
    }
    
}
func startWithLoginView() {
    guard let loginVC = UIStoryboard(name: "LoginController", bundle: nil).instantiateViewController(withIdentifier: "LoginNavVC") as? UINavigationController else { return }
    window?.rootViewController = loginVC
    window?.makeKeyAndVisible()
}

func startWithMainView() {
    guard let mainVC = UIStoryboard(name: "MainController", bundle: nil).instantiateViewController(withIdentifier: "MainTabVC") as? UITabBarController else { return }
    window?.rootViewController = mainVC
    window?.makeKeyAndVisible()
}


로그인 화면에서 메인 화면으로 넘어갈 때 Navigation 효과를 주었고, 메인 화면에는 TabBar가 적용 되어있어 모든 기능을 온전히 사용하기 위해 위와 같이 작성하였다. 단순히 뷰 컨트롤러만 불러올 것이라면 아래와 같이 처리해도 무방하다.

guard let mainVC = UIStoryboard(name: "MainController", bundle: nil).instantiateViewController(withIdentifier: "MainTabVC") as? UIViewController else { return }

참고: https://velog.io/@dev-lena/iOS-AppDelegate와-SceneDelegate