0

i have an app where the user sets a username in the WelcomeViewController the first time he launches the app. This Username gets stored in Firestore. I want to change the ViewController that gets displayed when the app is opened after the username is set! I made a function that checks if there is a username stored in FireStore and sets a boolean to true or false depending on the result. I want to change the ViewController that gets displayed as the RootViewController based on the boolean value, if true set the MainVC as RootVC and if it's false the WelcomeVC should be the RootVC.

If tried to set up a func inside the SceneDelegate func scene() but somehow i either get a crash or a black screen when the app doesnt crash. I dont know what im doing wrong, i have tried every tutorial but nothing is working.

heres my code:

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).
        
        self.window = UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        
        let welcomeVCAsRoot = storyboard.instantiateViewController(withIdentifier: "WelcomeViewController")
        let mainVCAsRoot = storyboard.instantiateViewController(withIdentifier: "MainViewController")
        
        if User.shared.userNameOccupied == true {
            self.window?.rootViewController = welcomeVCAsRoot
            self.window?.makeKeyAndVisible()
        } else {
            self.window?.rootViewController = mainVCAsRoot
            self.window?.makeKeyAndVisible()
        }
        
        guard let _ = (scene as? UIWindowScene) else { return }
        
    }
flolopi
  • 43
  • 6

1 Answers1

0

Try to use scene

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

    self.window = .init(frame: windowScene.coordinateSpace.bounds)
    self.window?.windowScene = windowScene
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    
    let welcomeVCAsRoot = storyboard.instantiateViewController(withIdentifier: "WelcomeViewController")
    let mainVCAsRoot = storyboard.instantiateViewController(withIdentifier: "MainViewController")
    
    if User.shared.userNameOccupied == true {
        self.window?.rootViewController = welcomeVCAsRoot
    } else {
        self.window?.rootViewController = mainVCAsRoot
    }
    self.window?.makeKeyAndVisible()
}

If this will not help - try to add:

class TestViewController: UIViewController {}

and call it from the method scene like:

let testController = TestViewController()
testController.backgroundColor = .blue
self.window?.rootViewController = testController

If you will receive blue screen - something wrong on the controller side, if no - something wrong with my advice, and let me know exactly type of error. Good luck

  • Hey Man thanks, gonna try it later! I will let you know if it works – flolopi Jan 25 '23 at 13:39
  • Hey man, i have implemented it but it somehow doesnt work. The var name gets initialized when starting the app with name in firestore but even when user.shared.usernameOccupied is true it doesnt set the mainVC as initial wievcontroller but the welcomeVC. I guess the porblem is that the userNameOccupied is true AFTER the sceneDelegate func scene() is executed. I made a print statement inside scene() with a 2 second delay and then userNameOccupied is true, but thats after the initialVC is loaded – flolopi Jan 28 '23 at 14:57