应用程式因Firebase电话验证而当机 [英] App crashes with Firebase Phone Auth
问题描述
我希望这个问题不会重复,因为我找不到类似的东西.
I hope that this question is not duplicate because I couldn't find any thing similar.
我有两个视图控制器:
NEWPhoneAuthViewController
NEWVerifyCodeViewController
NEWPhoneAuthViewController代码:
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)
})
}
}
和我的NEWVerifyCodeViewController代码是:
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()
}
}
}
}
}
现在问题是:当我在NEWVerifyCodeViewController中点击验证按钮时,
Now the problem is: when I tap on verify button in NEWVerifyCodeViewController the App crashes,
注意:
- 我打印了验证ID,但不为NULL.
- 我打印了用户收到的代码,它不是NULL.
因此,我不确定为什么会发生这种情况,并且在点击之后,我的控制台没有显示任何错误,除了以下内容:
So I'm not sure why that happens, and my console doesn't show any error after the tap except these:
推荐答案
所以我做了一个小调整,使我的项目正常工作.
So I made a small tweak that made my project work.
我在 NEWPhoneAuthViewController
中更改了此部分:
let verifyScene = NEWVerifyCodeViewController()
verifyScene.verificationID = verificationID
收件人:
首先创建一个名为: gVerificationID
的全局变量,并将其设置为:
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.
这篇关于应用程式因Firebase电话验证而当机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!