0

I hope that this question is not duplicate because I couldn't find any thing similar.

I have two view controllers:

NEWPhoneAuthViewController

NEWVerifyCodeViewController

NEWPhoneAuthViewController Code:

        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)
        })

    }


}

and my NEWVerifyCodeViewController code is:

    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()

                }
            }
        }
    }
}

Now the problem is: when I tap on verify button in NEWVerifyCodeViewController the App crashes,

NOTES:

  • I printed Verification ID and its not NULL.
  • I printed code that the user receives and its not NULL.

So I'm not sure why that happens, and my console doesn't show any error after the tap except these:

enter image description here

Hussein
  • 487
  • 6
  • 22

1 Answers1

0

So I made a small tweak that made my project work.

I changed this part in NEWPhoneAuthViewController :

let verifyScene = NEWVerifyCodeViewController()
verifyScene.verificationID = verificationID

to:

first created a global variable called: gVerificationID and set it to:

gVerificationID = verificationID

Thats it, not sure if thats the best practice and not sure why the first code didn't work but this is how I fixed it.

Hussein
  • 487
  • 6
  • 22