使用电话号码的Firebase身份validation会返回内部错误

我设置了我的应用程序,以便能够使用Firebase发送Apple通知,并validation了它可以使用控制台。 现在我想做一个build立在APN之上的电话authentication。

所以我写了这个:

PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { verificationID, error in if error != nil { print("Verification code not sent \(error!)") } else { print ("Successful.") } 

我得到:

 Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x170046db0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={ code = 500; message = "<null>"; }}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.} 

任何想法? 我应该提交一个针对Firebase的错误吗?

我正在使用iOS SDK 4.0.0(我可以find最新的zip。)

更新:

通过将FirebaseAppDelegateProxyEnabled添加到info.plist并将其设置为NO禁用方法调整

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Pass device token to auth. Auth.auth().setAPNSToken(deviceToken, type: .prod) } 

使用最新的Firebase iOS SDK(即4.0.0Xcode 8.3)进行testing

首先,从info.plist中删除这个关键的FirebaseAppDelegateProxyEnabled 。 这是不需要的。

现在在AppDelegate.swift中添加以下function

 import Firebase import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate{ var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() FirebaseApp.configure() return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Pass device token to auth. let firebaseAuth = Auth.auth() //At development time we use .sandbox firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox) //At time of production it will be set to .prod } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { let firebaseAuth = Auth.auth() if (firebaseAuth.canHandleNotification(userInfo)){ print(userInfo) return } }* 

向用户的手机发送validation码:

在想要整合电话身份validation的课程中写入:

注意 :我已经为印度添加了+91作为其国家代码。 您可以根据您的地区添加国家/地区代码。

  PhoneAuthProvider.provider().verifyPhoneNumber("+919876543210") { (verificationID, error) in if ((error) != nil) { // Verification code not sent. print(error) } else { // Successful. User gets verification code // Save verificationID in UserDefaults UserDefaults.standard.set(verificationID, forKey: "firebase_verification") UserDefaults.standard.synchronize() //And show the Screen to enter the Code. } 

使用validation码login用户

  let verificationID = UserDefaults.standard.value(forKey: "firebase_verification") let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID! as! String, verificationCode: self.txtEmailID.text!) Auth.auth().signIn(with: credential, completion: {(_ user: User, _ error: Error?) -> Void in if error != nil { // Error }else { print("Phone number: \(user.phoneNumber)") var userInfo: Any? = user.providerData[0] print(userInfo) } } as! AuthResultCallback) 

在我的情况下,它是错误的apns令牌types:

 Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod) 

本来应该:

 Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox) 

仔细检查Xcode中的应用程序包ID是否与Firebase中的包ID 完全匹配。 确切地说 ,确保他们的大小写匹配 – Xcode喜欢使用默认混合大小写来表示程序包ID的应用程序名称部分。

如果最终在Xcode中更改了软件包ID,请确保在Xcode中生成新的应用程序之前手动删除应用程序的供应configuration文件,否则将反复失败(Apple明显忽略configuration文件名称上的大小写)。

那么,在我的情况下,我已经发送错误self.verificationIDFIRAuthCredential 。 如果你有这个错误,那么请打印你的verificationID FIRAuthCredential并检查,你是发送给FIRAuthCredential

这是我在objC代码:

 [[FIRPhoneAuthProvider provider] verifyPhoneNumber:self.phoneNumberTextField.text UIDelegate:nil completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error) { NSLog(@"error %@", error.localizedDescription); return; } NSLog(@"verificationID %@", verificationID); self.verificationID = [NSString stringWithFormat:@"%@", verificationID]; // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // [defaults setObject:verificationID forKey:@"authVerificationID"]; // NSString *verificationID = [defaults stringForKey:@"authVerificationID"]; // Sign in using the verificationID and the code sent to the user // ... }]; 

我在这里意外地发送了错误的verificationID:

self.verificationID = [NSString stringWithFormat:@"verificationID",];

正确的是这样的:

self.verificationID = [NSString stringWithFormat:@"%@", verificationID];

然后我把它发送到FIRAuthCredential像这样:

 FIRAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:self.verificationID verificationCode:self.pinCodeTextField.text]; [[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) { if (error) { NSLog(@"error %@", error); return; } NSLog(@"Success"); // User successfully signed in. Get user data from the FIRUser object // ... }]; 

哪个success返回成功。 希望这会对其他人有所帮助。