簡體   English   中英

使用電話號碼身份驗證的令牌不匹配 - iOS

[英]Token Mismatch using Phone Number Authentication - iOS

我正在嘗試使用 firebase 使用電話號碼身份驗證登錄帳戶。

最初,我使用我的設備部署了該應用程序,並且運行良好。 但是,當我嘗試將應用程序部署到另一台設備時,它會拋出錯誤Token Mismatch

我在stackoverflow搜索了幾個答案,然后我找到了這個答案並遵循了它,但它對我不起作用。

我檢查了以下內容:

  1. 確保我已將有效的開發和生產 APNS 證書上傳到 Firebase 儀表板的“項目設置”>“雲消息傳遞”下。 (我的 APNS 證書有效期到明年)。
  2. 在 Xcode 的 .entitlements 文件中,確保 APS Environment 值設置為“development”或“production”,具體取決於您的測試情況。 (我也查過)。
  3. 最后(這是我所缺少的),檢查您的 AppDelegate.swift 內部和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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM