【问题标题】:Token Mismatch using Phone Number Authentication - iOS使用电话号码身份验证的令牌不匹配 - iOS
【发布时间】:2019-05-23 14:20:43
【问题描述】:

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

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

我在stackoverflow 中搜索了几个答案,然后我找到了这个answer 并关注了它,但它对我不起作用。

我检查了以下内容:

  1. 确保我已将有效的开发和生产 APNS 证书上传到 Firebase 仪表板的“项目设置”>“云消息传递”下。 (我的 APNS 证书有效期到明年)。
  2. 在 Xcode 的 .entitlements 文件中,确保 APS 环境值设置为“开发”或“生产”,具体取决于您的测试情况。 (我也查过)。
  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 设备时,它仍然给我错误。 我想知道为什么?

【问题讨论】:

    标签: ios swift xcode firebase-authentication token


    【解决方案1】:

    按照步骤进行

    1) 导入 Firebase 和 FirebaseAuth

    import Firebase import FirebaseAuth

    2) 在 didFinishLaunchingWithOptions 中配置 firebase。

    FirebaseApp.configure()
    

    3) 在 AppDelegate 中编写这两个 func。

      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 类中,重复步骤first并编写代码以在您想要的手机号码上发送 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
    
        }
    
    }
    

    【讨论】:

    • 谢谢老板,它正在工作,主要我们需要设置AuthAPNSTokenType。这个值正确。它为我节省了很多时间。
    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多