我正在 IOS 17+ 上开发一个应用程序,我的 firebase-ios-sdk 10.18.0,Xcode 15.4。我需要使用电话号码在应用程序中实现登录和注册。但由于多种错误,我无法发送短信。
第一个错误是
电话号码验证期间出错:发生内部错误,打印并检查错误详细信息以获取更多信息。 Firebase 错误代码:FIRAuthErrorCode(原始值:17999)
最常出现并伴有错误
https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode,响应代码:503
并且出现错误的频率也较低
电话号码验证期间出错:由于异常活动,我们已阻止来自此设备的所有请求。稍后再试。 Firebase 错误代码:FIRAuthErrorCode(原始值:17010)
并且伴随着错误
https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode,响应代码:400
我尝试了很多解决方案,这就是我能说的
我当然已经在 FireBase 和 Google Cloud Platform 中启用了使用电话号码注册
如果您添加测试电话号码,注册和身份验证将正常工作
该错误与 reCaptcha 无关,因为它成功通过,并且我已经重新检查了 URL 方案
在 Google Cloud Platform 中,我启用了“Cloud Identity”和“Identity Toolkit API”
该错误也与 APNs 身份验证密钥无关,因为我可以通过云消息发送通知,没有任何问题。
我还关联了我的信用卡。
这是我的 AppDelegate 代码以及与发送消息相关的函数
func startAuth(phoneNumber: String, completion: @escaping (Bool) -> Void) {
print("Starting phone number verification for: \(phoneNumber)")
// Установить isAppVerificationDisabledForTesting в true для тестирования
// Auth.auth().settings?.isAppVerificationDisabledForTesting = true
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
if let error = error {
print("Error during phone number verification: \(error.localizedDescription)")
if let errorCode = AuthErrorCode.Code(rawValue: (error as NSError).code) {
print("Firebase Error Code: \(errorCode)")
}
completion(false)
return
}
UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
completion(true)
}
}
func verifyCode(smsCode: String, completion: @escaping (Bool) -> Void) {
guard let verificationID = UserDefaults.standard.string(forKey: "authVerificationID") else {
completion(false)
return
}
let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID, verificationCode: smsCode)
Auth.auth().signIn(with: credential) { authResult, error in
if let error = error {
print("Error verifying code: \(error)")
completion(false)
return
}
completion(true)
}
}
func resendCode(completion: @escaping (Bool) -> Void) {
guard let phoneNumber = tempUserData?["phoneNumber"] as? String else {
completion(false)
return
}
startAuth(phoneNumber: phoneNumber) { success in
completion(success)
}
}
func registerUser(userData: [String: Any], completion: @escaping (Bool) -> Void) {
let db = Firestore.firestore()
guard let uid = Auth.auth().currentUser?.uid else {
completion(false)
return
}
db.collection("users").document(uid).setData(userData) { error in
if let error = error {
print("Error registering user: \(error)")
completion(false)
return
}
completion(true)
}
}
和
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// let providerFactory = AppCheckDebugProviderFactory()
// AppCheck.setAppCheckProviderFactory(providerFactory)
let providerFactory = YourAppCheckProviderFactory()
AppCheck.setAppCheckProviderFactory(providerFactory)
FirebaseApp.configure()
print("App Check provider factory set")
// Запрос разрешения на отправку уведомлений
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
print("Permission granted: \(granted)")
}
application.registerForRemoteNotifications()
print("Remote notifications registered")
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Обработка регистрации удаленных уведомлений
Auth.auth().setAPNSToken(deviceToken, type: .unknown)
print("Remote notifications token registered")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Обработка ошибки регистрации удаленных уведомлений
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Обработка полученных уведомлений
if Auth.auth().canHandleNotification(userInfo) {
completionHandler(.noData)
return
}
// This notification is not handled by Firebase Authentication.
// Handle the notification yourself, as appropriate.
}
}
class YourAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
if #available(iOS 14.0, *) {
return AppAttestProvider(app: app)
} else {
return DeviceCheckProvider(app: app)
}
}