应用程序因 Firebase Phone 身份验证而崩溃

App crashes with Firebase Phone Auth

我希望这个问题不会重复,因为我找不到任何类似的东西。

我有两个视图控制器:

NEWPhoneAuthViewController

NEWVerifyCodeViewController

NEWPhoneAuthViewController 代码:

        import UIKit
import Firebase

class NEWPhoneAuthViewController: UIViewController {

    @IBOutlet weak var phoneTxtField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func submitPressed(_ sender: Any) {

        let phoneNumber = phoneTxtField.text!

        PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil, completion: { (verificationID, error) in

            if let error = error {
                debugPrint(error.localizedDescription)
                return
            }

            guard let verificationID = verificationID else { return }
            print("Verification ID")
            print(verificationID)
            let verifyScene = NEWVerifyCodeViewController()
            verifyScene.verificationID = verificationID

            self.performSegue(withIdentifier: "toCodefromPhoneAuth", sender: nil)
            //self.navigationController?.pushViewController(verifyScene, animated: true)
        })

    }


}

我的 NEWVerifyCodeViewController 代码是:

    import UIKit
import Firebase

class NEWVerifyCodeViewController: UIViewController {

    @IBOutlet weak var codeTxtField: UITextField!

    var verificationID:String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func verifyPressed(_ sender: Any) {
        if let verificationCode = codeTxtField.text {
            let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID!, verificationCode: verificationCode)
            Auth.auth().signIn(with: credential) { (user, error) in
                if let error = error {
                    debugPrint(error.localizedDescription)
                }else {
                    debugPrint("Verified successfully")

                    print("Navigating to SignUp")

                    //self.performSegue(withIdentifier: "toSignUpfromCode", sender: nil)

                    //let newSignUp = NEWSignUp()
                    //self.navigationController?.pushViewController(newSignUp, animated: true)

                    //self.performSegue(withIdentifier: "toSignUpFromPhone", sender: nil)
                    //Once you have verified your phone number kill the firebase session.
                    //try? Auth.auth().signOut()

                }
            }
        }
    }
}

现在的问题是: 当我点击 NEWVerifyCodeViewController 中的验证按钮时,应用程序崩溃了,

备注:

所以我不确定为什么会这样,我的控制台在点击后没有显示任何错误,除了这些:

所以我做了一个小调整,使我的项目成功。

我在 NEWPhoneAuthViewController 中更改了这部分:

let verifyScene = NEWVerifyCodeViewController()
verifyScene.verificationID = verificationID

至:

首先创建了一个名为:gVerificationID 的全局变量并将其设置为:

gVerificationID = verificationID

就是这样,不确定这是否是最佳实践,也不确定为什么第一个代码不起作用,但这就是我修复它的方法。