'UIKit automatically switch to another VC after launch screen appear

I'm trying to automatically switch from lauch screen to home screen after 3 sec. This is code in entry point view controller

        override func viewDidAppear(_ animated: Bool) {
                DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
                     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                     let viewController = enter code heremainStoryboard.instantiateViewController(withIdentifier: "GenresNC") 
                     UIApplication.shared.windows.first?.rootViewController = screen
                     self.view.window?.makeKeyAndVisible()
                }
            }
 

It brakes at line with mainStoryboard... Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f5c7de8)

Can somebody help ?



Solution 1:[1]

AppDelegate will automatically change from LaunchScreen to window rootViewController - just place your code inside application(application: didFinishLaunchingWithOptions) method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "GenresNC")

    window?.makeKeyAndVisible()

    return true;
}

Solution 2:[2]

I've implement the same thing in SceneDelegate.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    
    guard let _ = (scene as? UIWindowScene) else { return }
    guard let windowScene = (scene as? UIWindowScene) else { return }


    self.window = UIWindow(windowScene: windowScene)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC: UIViewController = storyboard.instantiateViewController(identifier: "tapBarViewController")

    let rootNC = UINavigationController(rootViewController: rootVC!)
    self.window?.rootViewController = rootNC
    self.window?.makeKeyAndVisible()
    
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gustavo Tagliari
Solution 2