首页 > 解决方案 > Firebase iOS 电话身份验证:无法验证电话号码

问题描述

我正在尝试使用 Firebase 和电话号码在 iOS 上进行身份验证。

我通过打印语句发现的问题是 SubmitPhoneNumber()。它命中“我们发现错误”打印语句,并且报告的错误是

FIRAuthErrorUserInfoNameKey=ERROR_INVALID_PHONE_NUMBER 以及

FIRAuthErrorUserInfoNameKey=ERROR_WEB_CONTEXT_ALREADY_PRESENTED

我不知道该怎么做或如何解决这个问题。我传递的电话号码格式是“+[国家代码]1234567890”

import SwiftUI
import Firebase

struct VerifyPhoneView: View {

    private var phoneNumber: String = ""
    @State private var verificationID: String = ""
    @State private var verificationCode: String = ""
    @State private var loginSuccesful: Bool = false
    @EnvironmentObject var ls: LoginStatus

    init(phoneNumber: String) {
        self.phoneNumber = phoneNumber
        submitPhoneNumber()
    }

    var body: some View {

        ZStack {

            Text("My code is")

            TextField("Enter code", text: $verificationCode)

            Button(action: {
                self.submitVerificationCode()
            }) {
                Text("Continue")
            }

        }

    }

    func submitPhoneNumber() {
        print("the submitPhoneNumber method was called")
        PhoneAuthProvider.provider().verifyPhoneNumber("+1" + phoneNumber, uiDelegate: nil) { (verificationID, error) in
            if error != nil {
                print("we found an error")
                print(error?.localizedDescription)
                return
            }
            else {
                print("we did not find an error")
                print(verificationID!)
                self.verificationID = verificationID!
            }
        }
    }

    func submitVerificationCode() {
        print ("222 verificationID is " + self.verificationID)

        let credential = PhoneAuthProvider.provider().credential(
            withVerificationID: self.verificationID,
            verificationCode: self.verificationCode)

        Auth.auth().signIn(with: credential) { (user, error) in
            if let error = error {
                print(error.debugDescription)
                return
            }
            // User is signed in
            print("User signed in succesfully")
        }
    }



}

标签: iosfirebasefirebase-authentication

解决方案


推荐阅读