![](/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.