0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Welcome to SO. The question is a bit unclear - can you clarify what part of that code isn't working as expected and explain what you expect it to do? Doing some troubleshooting and indicating the line of code that's failing would help us to narrow the issue. – Jay Sep 08 '22 at 17:41
  • In viewDidLoad() I check if the user is logged in or not. This function is a method of the UIViewController class. If the user is not logged in, I just add input bar and button elements to the subview. If the user is logged in, I need to display the UITabBarController. The problem is that these are two different classes and I can’t just add the elements to the subview, as in the case of the first check, and I need to understand how to show the UITabBarController elements if the user is logged in – Dany Rudenko Sep 09 '22 at 08:15
  • I think you're creating a lot of extra work adding in all of those subviews. IMO, if the user is logged in segue to the main view that has all of those components. If they user is not logged in, segue to a controller that lets them log in. That may be a more efficient process. Use @frankvanpuffelen answer to determine connected status before segue-ing to either of the views I just mentioned. – Jay Sep 09 '22 at 18:25
  • Thanks, I understood how to deal with authentication state, probably you know any ways to redirect to another view if user logged in? – Dany Rudenko Sep 10 '22 at 09:42
  • Sure. One option is called a Segue. That allows one view to transition to another view. Here's a great answer on [How to segue](https://stackoverflow.com/questions/27604192/how-to-segue-programmatically-in-ios-using-swift). Its pretty well documented in the developer documentation as well as a number of Q&A's here on SO. There are some videos on Youtube as well explaining the process. – Jay Sep 10 '22 at 14:04

1 Answers1

0

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
  // ...
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807