Search code examples
iosswiftparsingparse-platform

IOS Parse Homescreen when currentUser() != nil


I am struggling with this (seems pretty straightforward), but I think it might be because my login/signup VCs are in a navigation controller, and the rest of my app (Homescreen etc) is in a separate TabBarController.

My "Is Initial VC" is set to the navigation controller that holds my Signup and Login VCs, and this launches and works perfectly, I am able to log in and I go to my HomeVC like so:

func transitionToHome() {
    let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
    view.window?.rootViewController = homeViewController
    view.window?.makeKeyAndVisible()
}

Inside of my AppDelegate I have the following code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    setupParse()

    if  PFUser.current() != nil {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let homeVC = storyboard.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController?.present(homeVC, animated: true, completion: nil)
    }

    return true
}

I have tried it backwards as well(Home TabbarVC is Initial, and in app delegate if Pf.current() == nil, launch with login), and made sure PFUser.current() was set to nil after logging out and it was, but it still was not working for me. I have read through other similar questions but I think my problem might be the tab bar vs navigation - or something with the use of windows. Thank you in advanced.


Solution

  • You need to move your code inside of SceneDelegate and you need to change it a bit:

    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).
        guard let windowScene = (scene as? UIWindowScene) else { return }
    
        if  PFUser.current() != nil {
            window = UIWindow(windowScene: windowScene)
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let homeVC = storyboard.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
            window?.rootViewController = homeVC
            self.window?.makeKeyAndVisible()
        }
    
    }