I
I
Ivan Vasilich2017-08-24 21:59:47
Swift
Ivan Vasilich, 2017-08-24 21:59:47

How to display errors and hide controller in swift?

Good afternoon, a question for professionals, please help. I decided to write an application, it has a storyboard with authorization where everything is folded for me, authorization, registration and recovery.
All authorization goes through Firebase.
In general, the problem itself
. I have two fields: name and password, as well as the login button - the action I have is attached to this code

@IBAction func checkLogin(_ sender: UIButton) {

        let email = userEmail.text!
        let passwd = userPassword.text!
        
        if email == "" || passwd == "" || !email.isValidEMail {
            userEmail.shake()
            userPassword.shake()
            return
        }

        Auth.auth().signIn(withEmail: email, password: passwd) { (user, error) in
            
            if error != nil {
                let alert = UIAlertController(title: "Ошибка!", message: (error?.localizedDescription)!, preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            } else {
                // TODO: Save user data to Locksmith 
                
                if let user = user {
                    do {
                        try Locksmith.saveData(data: ["user_uid": user.uid], forUserAccount: "myUserAccount")
                    } catch (let err) {
                        print(err)
                    }
                }

                let alert = UIAlertController(title: "Вы авторизированны", message: "вы зашли как: \(user?.uid)", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
                
            }
        }
        
        let userUID = Auth.auth().currentUser?.uid
        
        print("\(userUID)")
        
        if userUID != nil {
            self.navigationController?.popViewController(animated: true)
            self.dismiss(animated: true, completion: nil)
        }
    }

the problem is that the clogger itself outputs authorization to a separate thread, and the main thread goes further. and I can't print errors. More precisely, you can display UIAlerts if you file them in the condition itself. The question is how to hide the controller if the user logged in successfully, because the main thread runs faster than the authorization passes??
PS! don't kick me, I'm a noob in swift

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-08-25
@alexkbs

If you have this first view, then you cannot just hide it like that. Before it (below it) what should the user see? You need to show another controller or make a segue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question