![](/img/trans.png)
[英]Firebase Phone Number Authentification IOS Error: Optional(“Token mismatch”)
[英]Token Mismatch using Phone Number Authentication - iOS
我正在尝试使用 firebase 使用电话号码身份验证登录帐户。
最初,我使用我的设备部署了该应用程序,并且运行良好。 但是,当我尝试将应用程序部署到另一台设备时,它会抛出错误Token Mismatch
。
我在stackoverflow
搜索了几个答案,然后我找到了这个答案并遵循了它,但它对我不起作用。
我检查了以下内容:
didRegisterForRemoteNotificationsWithDeviceToken
的方法内部,将值从.sandbox
更改为.prod
,或更改为.unknown
以让应用程序包确定要使用的令牌类型,基于在您的配置文件中。这第三我也改变了
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)
但是当我在另一台设备上运行这个应用程序时,它总是向我抛出那个错误。
但是当我注释这行代码// Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
然后我运行应用程序之后我取消注释那行代码Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
然后我再次运行该应用程序,最后它可以工作了。 但遗憾的是,当我运行另一台 iOS 设备时,它仍然给我错误。 我想知道为什么?
按照步骤操作
1) 导入 Firebase 和 FirebaseAuth
import Firebase import FirebaseAuth
2) 在 didFinishLaunchingWithOptions 中配置 firebase。
FirebaseApp.configure()
3) 在 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写代码。
@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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.