Swift:我无法将Firebase FIRAuth函数中的变量传递回来自父函数

时间:2016-07-10 16:01:07

标签: ios iphone swift firebase firebase-authentication

我是一个快速的新手,我一直在努力学习自己。我遇到了一个问题,我不确定如何解决它。代码如下:

import Foundation
import Firebase

class UserLogin {

var email:String?
var password:String?

init(email:String, password:String){
    self.email = email
    self.password = password
}

func userLogin() -> String{
    var errorMsg:String = ""
    //Email & Password Integrity check
    if (email == ""){
        errorMsg = "Please enter your email"
    } else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){
        errorMsg = "Email is invalid"
    }else if (password == ""){
        errorMsg = "Please enter your password"
    }  else if (password?.characters.count < 8){
        errorMsg = "Password is invalid"
    }else{
        print("Logging In... with Email:\(email!) and Password:\(password!)")
        //Firebase Authentication Process"

        FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
            // ...
            if (error != nil){
                let errorCode = error!.code
                if (errorCode == 17009){
                    errorMsg = "You have entered the wrong password"
                } else if (errorCode == 17011){
                    errorMsg = "Your email does not exist"
                } else if (errorCode == 17010) {
                    errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."
                } else {
                    print(error)
                }
            } else {
                print("User is Logged In")
                errorMsg = "You have successfully Logged In"
            }
        }
    }
    return errorMsg
}

}

基本上在我的ViewController中,我有一个单独的代码,就像这样

let alert = UIAlertController(title: "Error", message: errorMsg, preferredStyle: .Alert)
        let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alert.addAction(action)
        self.presentViewController(alert, animated: true, completion: nil)

这适用于我的userLogin函数中的所有errorMsg,但对于基于FIRAuth提供的error.code生成的少数errorMsgs,它不会出现。

我读过并认为可能是因为FIRAuth是一个异步调用,但我不知道如何解决它。

很抱歉,如果这听起来真的很愚蠢,但我一整天都在搞清楚,但无济于事,从你们那里得到一些帮助真是太棒了。

增加: 我按照建议实现了CompletionHandler,但我不明白为什么它不起作用虽然它应该......以下是我的代码。

UserLogin1.swift

import Foundation
import Firebase

class UserLogin1 {

var email:String?
var password:String?

init(email:String, password:String){

    self.email = email
    self.password = password
}

func userLogin(completion:(message:String)->()) {

    var errorMsg:String = ""
    //Email & Password Integrity check
    if (email == ""){

        errorMsg = "Please enter your email"

    } else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){

        errorMsg = "Email is invalid"

    }else if (password == ""){

        errorMsg = "Please enter your password"

    }  else if (password?.characters.count < 8){

        errorMsg = "Password is invalid"

    }else if (errorMsg != ""){

        completion(message: errorMsg)

    }else{

        print("Logging In... with Email:\(email!) and Password:\(password!)")
        //Firebase Authentication Process"

        FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
            // ...
            if (error != nil){

                let errorCode = error!.code

                if (errorCode == 17009){

                    errorMsg = "You have entered the wrong password"

                } else if (errorCode == 17011){

                    errorMsg = "Your email does not exist"

                } else if (errorCode == 17010) {

                    errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."

                } else {

                    print(error)

                }

            } else {
                print("User is Logged In")
                errorMsg = "You have successfully Logged In"
            }


        }
        completion(message: errorMsg)

    }

}

}

LoginViewController

import UIKit

class LoginViewController: UIViewController {

//Properties
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!

//Actions
@IBAction func loginButton(sender: AnyObject) {

    let email = self.emailTextField.text!
    let password = self.passwordTextField.text!

    let user = UserLogin1(email: email, password: password)

    user.userLogin(){ (message:String) in
        print(message)
    }


    }

1 个答案:

答案 0 :(得分:1)

从我的评论中,我提到你将有两种可能的解决方案。最好的选择取决于你想要达到的目的。但我相信你可以让它与任何人合作。

使用Completion Handler

func userLogin(completion:(message:String)->()){
    var errorMsg:String = ""
    if (email == ""){
    ...
    //check if found any errors yet
    }else if (errorMsg != ""){
        completion(errorMsg)
    } else {
        FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
            if (error != nil){
            ...
            } else {
                errorMsg = "You have successfully Logged In"
            }
            completion(errorMsg)
        }
    }
}

userLogin(){ (message:String) in
    // this will only be called when userLogin trigger completion(errorMsg)...
    print(message)
}

自我

func userLogin() -> Void{
    var errorMsg:String = ""
    if (email == ""){
    ...
    //check if found any error yet
    }else if (errorMsg != ""){
        self.errorMsg = errorMsg
    } else {
        FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
            if (error != nil){
            ...
            } else {
                errorMsg = "You have successfully Logged In"
            }
            self.errorMsg = errorMsg
        }
    }
}