简体   繁体   English

Swift- Firebase身份验证登录中的signIn函数,如何使用shouldPerformSegue停止执行?

[英]Swift- Firebase authentication signIn function inside a presenting segue, how to use shouldPerformSegue to stop the segue from performing?

I wanted to use a storyboard segue(present modally) to present the new view controller but the verification inside shouldPerform function are Firebase Authorization function signIn but since the trailing closure directly return the compiler back, it returns false before signIn function can make any changes to the flag. 我想使用情节提要segue(以模态形式呈现)来呈现新的视图控制器,但shouldPerform函数内部的验证是Firebase授权函数signIn,但由于尾随闭包直接将编译器返回,因此在signIn函数可以对其进行任何更改之前它返回false。标志。

var shouldPerformSegue = false
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    guard let email = loginemail.text, let pass = loginpassword.text, email != "", pass != "" else {
        return false
    }
    Auth.auth().signIn(withEmail: email, password: pass) { (result, error) in
        if let error = error{
            TWMessageBarManager().showMessage(withTitle: "invalid email or pass", description: error.localizedDescription, type: .error)
            self.shouldPerformSegue = false

        } else {
        self.shouldPerformSegue = true
        }
    }
    return shouldPerformSegue
}

My goal is to use a storyboard segue(particularly a non-custom segue) to solve this, is it possible? 我的目标是使用情节提要segue(尤其是非定制的segue)来解决此问题,这可能吗?

Try using a Completion Handler 尝试使用完成处理程序

func didUserLoggedInSuccessfully(success:@escaping (NSDictionary) -> Void)
    {
        Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
            if error == nil {
                success(true)
            }
            else{
                //Tells the user that there is an error and then gets firebase to tell them the error
                success(false)
            }
        }
    }

Usgae Usgae

self.didUserLoggedInSuccessfully { (success) in
            if success {
                /// Perform Segue
            }
            else{
                /// Do not Perform
            }
        }

Here is more accurate answer. 这是更准确的答案。 Here I have changed @escaping type from NSDictionary to (Bool,Error?) as we need to return the completion clock with result. 在这里,我已经将@escaping类型从NSDictionary更改为(Bool,Error?)因为我们需要返回带有结果的完成时钟。

func didUserLoggedInSuccessfully(success:@escaping (Bool, Error?) -> Void)
{
    Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
        if error == nil {
            success(true,nil)
        }
        else{
            success(false,error)
        }
    }
}


self.didUserLoggedInSuccessfully { (result,error) in
    if result {
        /// Perform Segue
    }
    else{
        /// Do not Perform
        TWMessageBarManager().showMessage(withTitle: "invalid email or pass", description: error.localizedDescription, type: .error)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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