【问题标题】:Resend Code using Firebase Authentication in Swift在 Swift 中使用 Firebase 身份验证重新发送代码
【发布时间】:2020-05-22 11:46:26
【问题描述】:

由于没有直接的方法可以在发送代码后重新发送代码,因此我尝试使用以下方法再次重新发送代码:

 func sendOTPGetVerfID(phoneNum: String, completion: @escaping((_ verificationID: String?, _ error: Error?) -> Void)) {

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

        if let error = error {
            QL1(error.localizedDescription)
            completion(nil, error)
            return
        }
        // Sign in using the verificationID and the code sent to the user
        // ...
        QL1("VerificationID : \(verificationID ?? "")")
        completion(verificationID,nil)
    }
}

这里的问题是,我第二次接收不到verificationID,调用上面的方法 任何帮助表示赞赏

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    在重新发送 OTP 按钮点击时调用 requestOtp() 方法。

    import FirebaseAuth
    
    func requestOtp() {
    let phNo = "Your phone number"
    PhoneAuthProvider.provider().verifyPhoneNumber(phNo, uiDelegate: nil) { (verificationID, error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            // Sign in using the verificationid and the code sent to the user
            // ...
          UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
        }
    }
    

    使用 Auth 验证代码:

    func verifyNumberWith(verificationCode: String) {
      let verificationID=UserDefaults.standard.string(forKey:"authVerificationID")
        let credential = PhoneAuthProvider.provider().credential(
            withVerificationID: verificationID ?? "",
            verificationCode: verificationCode)
    
        Auth.auth().signIn(with: credential) { (authResult, error) in
            //Do your actions. 
        }
    }
    

    【讨论】:

    • 我做了同样的事情,但问题是我没有第二次获得验证 ID。
    猜你喜欢
    • 2020-07-22
    • 2015-02-06
    • 2021-06-27
    • 2020-01-28
    • 1970-01-01
    • 2011-10-09
    • 2022-12-15
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多