使用电话号码身份验证的令牌不匹配 - iOS

Vis*_*mbo 3 xcode token ios swift firebase-authentication

我正在尝试使用 firebase 使用电话号码身份验证登录帐户。

最初,我在我的设备上部署了该应用程序,并且运行良好。但是当我尝试将应用程序部署到另一台设备时,它会抛出错误Token Mismatch

我搜索了几个答案,stackoverflow然后找到了这个答案,然后我遵循了,但它对我不起作用。

我已经检查了以下内容:

  1. 确保我将有效的开发和生产 APNS 证书上传到 Firebase 仪表板的“项目设置”>“云消息传递”下。(我的 APNS 证书有效期到明年)。
  2. 在 Xcode 的 .entitlements 文件中,确保 APS 环境值设置为“开发”或“生产”,具体取决于您的测试情况。(我也检查过)。
  3. 最后(这就是我所缺少的),检查您的 AppDelegate.swift 和 for 方法内部didRegisterForRemoteNotificationsWithDeviceToken,将值从.sandboxto.prod或 to更改.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)
Run Code Online (Sandbox Code Playgroud)

但当我在另一台设备上运行这个应用程序时,它总是会抛出该错误。

但是,当我注释这行代码// Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)然后运行该应用程序时,我取消注释该行代码Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown),然后再次运行该应用程序,最后它起作用了。但遗憾的是,当我运行另一个 iOS 设备时,它仍然给我错误。我想知道为什么?

Par*_*rth 5

按照步骤操作

1) 导入 Firebase 和 FirebaseAuth

import Firebase import FirebaseAuth

2)在didFinishLaunchingWithOptions中配置firebase。

FirebaseApp.configure()
Run Code Online (Sandbox Code Playgroud)

3)将这两个func写入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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

    }

}
Run Code Online (Sandbox Code Playgroud)


小智 5

    \n
  1. 确保在 AppDelegate.swift 中设置 FirebaseApp.configure()
  2. \n
\n
    override func application(\n        _ application: UIApplication,\n        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?\n    ) -> Bool {\n        FirebaseApp.configure() \n        GeneratedPluginRegistrant.register(with: self)\n        return super.application(application, didFinishLaunchingWithOptions: launchOptions)\n    }\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 将 APNS 令牌类型设置为 AuthAPNSTokenType.unknown。实际的令牌类型将从 app\xe2\x80\x99s 捆绑包中的配置文件中检测到。
  2. \n
\n
    override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {\n        let firebaseAuth = Auth.auth()\n        firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)\n    }\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 在 Runner.entitlements 文件中将 aps-environment 值从“development”更改为“unknown”
  2. \n
\n

跑步者权利

\n