简体   繁体   English

Firebase 认证:Swift 中的电话号码认证

[英]Firebase Authentication : Phone number authentication in Swift

A localised description is being printed in my output console saying: "unable to get verification: Optional("If app delegate swizzling is disabled, remote notifications received by UIApplicationDelegate need to be forwarded to FIRAuth\'s canHandleNotificaton: method.")"在我的 output 控制台中打印了本地化描述:“无法获得验证:可选(“如果禁用了应用程序委托 swizzling,则需要将 UIApplicationDelegate 接收到的远程通知转发到 FIRAuth 的 canHandleNotificaton:方法。”)“

Even though, I am receiving OTP in my phone as well as recaptcha for verification it is not taking me to next page which where we need to verify the otp received.即使,我在手机中收到 OTP 以及用于验证的 recaptcha,它并没有将我带到我们需要验证收到的 otp 的下一页。

My code is till receiving the OTP is working fine, but after that its showing the same page where I need to enter my number.我的代码在收到 OTP 之前工作正常,但在那之后它显示了我需要输入我的号码的同一页面。

My viewcontroller.swift has the following code:我的 viewcontroller.swift 有以下代码:

    @IBAction func mobileNumberSubmitButton(_ sender: Any) {

     guard  let phonenumber = mobileNumberText.text else {return}

     PhoneAuthProvider.provider().verifyPhoneNumber(phonenumber, uiDelegate: nil) {(verificationid, error) in
                if error == nil{
                    print(verificationid)
                    guard let verify = verificationid else {return}
                    self.userdefault.set("verify", forKey: "verifyID")
                    self.userdefault.synchronize()
                    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "OTPVerificationID") as! OTPVerificationViewController
                    viewController.verifyid = verificationid! 
                    self.navigationController?.pushViewController(viewController, animated: true)

                }else{
                    print("unable to get verification:", error?.localizedDescription)
                }
            }



}

and my otpverificationviewcontroller.swift has the following code:我的 otpverificationviewcontroller.swift 具有以下代码:

     class OTPVerificationViewController: UIViewController { 

       var verifyid : String = ""
       let userdefault = UserDefaults()

       @IBOutlet weak var OTPVerifyCode: UITextField!

       override func viewDidLoad() {
           super.viewDidLoad()

           let valust = self.userdefault.dictionary(forKey: "verifyID")
           print(verifyid) 

       }

       @IBAction func verificationSubmitButton(_ sender: Any) {

            guard let verifycode = OTPVerifyCode.text else {return}
            let credentials = PhoneAuthProvider.provider().credential(withVerificationID: verifyid, verificationCode: verifycode)
            Auth.auth().signInAndRetrieveData(with: credentials) { (result, error) in
            if error == nil {
                 if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ServiceViewControllerID") as? ServiceViewController {

                 UIApplication.shared.keyWindow?.rootViewController = viewController
                 self.dismiss(animated: true, completion: nil)
             }
           } else{
               print(error?.localizedDescription)
        } 
    }
} 

} }

I expect the mobileNumberSubmitButton action push the otpverification page inorder to verify the OTPcode.我希望 mobileNumberSubmitButton 操作会推送 otpverification 页面以验证 OTPcode。

Can someone give me some solutions?有人可以给我一些解决方案吗?

Do it on the main thread as:在主线程上执行以下操作:

DispatchQueue.main.async{
    let viewController =
     self.storyboard?.instantiateViewController(withIdentifier: "OTPVerificationID") as! OTPVerificationViewController
     viewController.verifyid = verificationid!
     self.navigationController?.pushViewController(viewController, animated: true) 
    }

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

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