简体   繁体   中英

Keep User Logged In in swift

I developed app where I open firstly UIViewController with login system, here in viewDidLoad I chech if user loggined in (with firebase) like that

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .systemBackground

    if Auth.auth().currentUser == nil{
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
        view.addGestureRecognizer(tap)
            
        view.addSubview(logoImageView)
        logoImageView.center = CGPoint(x: horizontalCenter, y: verticalCenter/2.5)
            
        view.addSubview(emailTextField)
        emailTextField.center = CGPoint(x: horizontalCenter, y: verticalCenter)
            
        view.addSubview(passwordTextField)
        passwordTextField.center = CGPoint(x: horizontalCenter, y: verticalCenter+50)
            
        view.addSubview(loginButton)
        loginButton.center = CGPoint(x: horizontalCenter, y: verticalCenter+100)
            
        view.addSubview(dontHaveAccountButton)
        dontHaveAccountButton.center = CGPoint(x: horizontalCenter, y: verticalCenter*1.8)
            
        view.addSubview(showPasswordButton)
        showPasswordButton.center = CGPoint(x: horizontalCenter+100, y: verticalCenter+50)
    }
    else {
        view.addSubview(LlogindButton)
        LlogindButton.center = CGPoint(x: horizontalCenter, y: verticalCenter+100)
    }
}

My config funk looks like:

@objc func config(){
    let tabBarVC = UITabBarController()
    
    let vc1 = UINavigationController(rootViewController: feedViewController())
    let vc2 = LibraryViewController()
    let vc3 = UINavigationController(rootViewController: CreatorViewController())
    let vc4 = ProfileViewController()
    let vc5 = UINavigationController(rootViewController: SettingsViewController())
    
    vc1.title = "Feed"
    vc2.title = "Library"
    vc3.title = "Creator Studio"
    vc4.title = "Profile"
    vc5.title = "Settings"
    
    tabBarVC.setViewControllers([vc1, vc2, vc3, vc4, vc5], animated: false)
    
    guard let items = tabBarVC.tabBar.items else{
        return
    }
    
    let images = ["house", "book.fill", "paintpalette.fill", "person.circle", "gear"]
    
    for x in 0..<items.count{
        items[x].image = UIImage(systemName:  images[x])
    }
    
    tabBarVC.modalPresentationStyle = .fullScreen
    present(tabBarVC, animated: true)
}

So, here is my problem. In config function I am showing UITabBarController() and if user logged in I want to show one of my bars from UITabBarController instead of button that call my config func. The same logic provided in instagram and I need something like that.

Firebase Authentication automatically persists the user's authentication state and restores it when the app restarts. This requires it to make a call to the server (for example to check if the account has been suspended), which means that it is an asynchronous operation that may not be completed by the time your if Auth.auth().currentUser == nil runs.

To ensure your app responds to the authentication state when it changes, use an auth state listener as shown in the first snippet of the documentation on getting the current user :

handle = Auth.auth().addStateDidChangeListener { auth, user in
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM