使用电话号码身份验证的令牌不匹配 - iOS [英] Token Mismatch using Phone Number Authentication - iOS
问题描述
我正在尝试使用 firebase 使用电话号码身份验证登录帐户.
I am trying to login account using phone number authentication using firebase.
最初,我使用我的设备部署了该应用程序,并且运行良好.但是,当我尝试将应用程序部署到另一台设备时,它会抛出错误 Token Mismatch
.
Initially, i deployed the app with my device and it works fine. But when i tried to deploy app to another device and then it throws me error Token Mismatch
.
我在 stackoverflow
中搜索了几个答案,然后我找到了这个 answer 并且我遵循了但它对我不起作用.
I have searched several answers in stackoverflow
and then i found this answer and i followed but it didn't work for me.
我检查了以下内容:
- 确保我已将有效的开发和生产 APNS 证书上传到 Firebase 仪表板的项目设置">云消息传递"下.(我的 APNS 证书有效期到明年).
- 在 Xcode 的 .entitlements 文件中,根据您的测试情况,确保将 APS 环境值设置为开发"或生产".(我也查过了).
- 最后(这是我所缺少的),检查您的 AppDelegate.swift 和
didRegisterForRemoteNotificationsWithDeviceToken
的方法内部,将值从.sandbox
更改为.prod
或.unknown
以让应用程序包根据您的配置文件确定要使用的令牌类型.
- Ensure I had both a valid development and production APNS certificate uploaded to the Firebase Dashboard, under 'Project Settings' > 'Cloud Messaging'. (My APNS certificate is valid till next year).
- In Xcode, in the .entitlements file, make sure the APS Environment value is set to either 'development' or 'production', depending on your testing situation. (I also checked).
- Finally (this is what I was missing), check inside your AppDelegate.swift and inside the method for
didRegisterForRemoteNotificationsWithDeviceToken
, change the value from.sandbox
to.prod
, or to.unknown
to let the app bundle determine which token type to use, based on your provisioning profile.
这第三我也改变了
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("==== This is device token ",token)
let data = Data(token.utf8)
Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
但是当我在另一台设备上运行这个应用程序时,它总是向我抛出那个错误.
But still when i run this app on another device which it always throws me that error.
但是当我注释这行代码 //Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
然后我运行应用程序之后我取消注释那行代码Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
然后我再次运行应用程序,最后它工作了.但遗憾的是,当我运行另一台 iOS 设备时,它仍然给我错误.我想知道为什么?
But when i comment this line of code // Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
and then i run the app after that i uncomment that line of code Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
and then i run the app again and finally it works. But sadly, when i run another iOS device it still gives me error.
I wonder why?
推荐答案
按照步骤
1) 导入 Firebase 和 FirebaseAuth
1) Import Firebase and FirebaseAuth
导入 Firebase导入 FirebaseAuth
2) 在 didFinishLaunchingWithOptions 中配置 firebase.
2) In didFinishLaunchingWithOptions Configure firebase.
FirebaseApp.configure()
3) 在 AppDelegate 中编写这两个函数.
3) Write these two func in AppDelegate.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let firebaseAuth = Auth.auth()
firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.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
}
}
4) 在您的 ViewController 类中,重复步骤第一并编写代码以在您想要的手机号码上发送 OTP.
4) In your ViewController class, repeat step first and write code for send OTP on Mobile Number, which you want.
@IBAction func sendCodeAction(_ sender: Any) {
let ugrMgr = UserManager.userManager
let phoneNumber = Auth.auth().currentUser?.phoneNumber
print(phoneNumber!)
print(ugrMgr.mobile!)
PhoneAuthProvider.provider().verifyPhoneNumber("+91" + ugrMgr.mobile!, uiDelegate: nil) { (verificationID, error) in
if let error = error {
print(error.localizedDescription)
mainInstance.ShowAlertWithError(error.localizedDescription as NSString, msg: error.localizedDescription as NSString)
return
}
self.verificationID = verificationID
}
}
这篇关于使用电话号码身份验证的令牌不匹配 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!