skip to Main Content

I have an app where users can login / sign out and the functionality stopped working recently when I added a new SceneDelegate view and moved over the code from the AppDelegate. I’m not sure why it is not working but I suspect it has to do with using the shared delegate in my signOut function.

Something strange is happening, when I tap the sign out button nothing happens. However, when I close the app and open it again, I will be signed out.

Here is the code on my home screen for the sign out button:

@IBAction func signOutButtonTapped(_ sender: Any) {
    KeychainWrapper.standard.removeObject(forKey: "accessToken")
    KeychainWrapper.standard.removeObject(forKey: "userID")
    
    // send user to splash page
    let signInPage = self.storyboard?.instantiateViewController(withIdentifier: "splashController") as! splashViewController
    let appDelegate = UIApplication.shared.delegate
    appDelegate?.window??.rootViewController = signInPage
    
}

This is the code from my SceneDelegate.swift file:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

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

        let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
        
        // If access token exists, skip login page
        if accessToken != nil {            
            if let windowScene = scene as? UIWindowScene {
                self.window = UIWindow(windowScene: windowScene)
                let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = mainStoryboard.instantiateViewController(withIdentifier: "homeTabController") as! TabBarController
                self.window!.rootViewController = vc
            }        
        }    
    
    }
  }

2

Answers


  1. Chosen as BEST ANSWER

    Figured it out - add a new var in SceneDelegate

    static weak var shared: SceneDelegate?
    

    Then replace the appDelegate.window line with

    let appDelegate = SceneDelegate.shared
    

  2. You’ve answered your own question. With a scene delegate, the window belongs to the scene delegate. The app delegate window is nil. So this line does nothing:

    appDelegate?.window??.rootViewController = signInPage
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search