我设置了我的应用程序,以便能够使用firebase发送Apple Notifications,并且我使用控制台验证了它的工作原理。现在我想做一个建立在APN之上的电话认证。
所以我写了这个:
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(我能找到最新的zip。)
更新
我通过将FirebaseAppDelegateProxyEnabled
添加到info.plist
并将其设置为NO
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth.
Auth.auth().setAPNSToken(deviceToken, type: .prod)
}
答案 0 :(得分:8)
使用最新的 Firebase iOS SDK测试,即4.0.0 和 Xcode 8.3
首先,从info.plist中删除此密钥FirebaseAppDelegateProxyEnabled
。这不是必需的。
现在在 AppDelegate.swift 中添加以下功能
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
}
}*
向用户的手机发送验证码:
在要集成电话身份验证的类中写:
注意:我已添加+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.
}
使用验证码:
登录用户 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)
答案 1 :(得分:2)
仔细检查Xcode中的应用包ID是否与Firebase 完全中的包ID匹配。并且通过完全,确保他们的案例匹配 - Xcode喜欢默认使用混合大小写作为捆绑ID的应用名称部分。
如果您最终更改了Xcode中的捆绑包ID,请确保在Xcode中生成新的捆绑包之前手动删除该应用程序的配置文件,否则它将反复失败(Apple显然忽略了配置文件名称的大小写)。
答案 2 :(得分:2)
在我的情况下,apns令牌类型是错误的:
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
应该是:
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
答案 3 :(得分:0)
嗯,就我而言,我向self.verificationID
发送了错误的FIRAuthCredential
。
如果您遇到此错误,请打印verificationID
并检查是否与您发送给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
// ...
}];
我不小心在这里发送了错误的验证ID:
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
。希望它对别人有所帮助。
答案 4 :(得分:0)
我很容易将.sandbox键入
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)
}
并从代码中删除此行
Auth.auth().settings.isAppVerificationDisabledForTesting = TRUE