使用电话号码身份验证的令牌不匹配-iOS [英] Token Mismatch using Phone Number Authentication - iOS

查看:118
本文介绍了使用电话号码身份验证的令牌不匹配-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to login account using phone number authentication using firebase.

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

Initially, i deployed the app with my device and it works fine. But when i tried to deploy app to another device and then it throws me error Token Mismatch.

我在stackoverflow中搜索了几个答案,然后找到了这个 answer 并且我遵循了,但是它对我没有用.

I have searched several answers in stackoverflow and then i found this answer and i followed but it didn't work for me.

我已经检查了以下内容:

I have checked following :

  1. 确保在项目设置">云消息传递"下,我已将有效的开发和生产APNS证书上载到Firebase仪表板. (我的APNS证书的有效期至明年).
  2. 在Xcode的.entitlements文件中,确保APS Environment值设置为开发"或生产",具体取决于您的测试情况. (我也检查过).
  3. 最后(这是我所缺少的),检查您的AppDelegate.swift内部和didRegisterForRemoteNotificationsWithDeviceToken方法内部,将值从.sandbox更改为.prod或更改为.unknown以使应用程序捆绑根据您的配置文件确定要使用的令牌类型.
  1. Ensure I had both a valid development and production APNS certificate uploaded to the Firebase Dashboard, under 'Project Settings' > 'Cloud Messaging'. (My APNS certificate is valid till next year).
  2. In Xcode, in the .entitlements file, make sure the APS Environment value is set to either 'development' or 'production', depending on your testing situation. (I also checked).
  3. Finally (this is what I was missing), check inside your AppDelegate.swift and inside the method for didRegisterForRemoteNotificationsWithDeviceToken, change the value from .sandbox to .prod, or to .unknown to let the app bundle determine which token type to use, based on your provisioning profile.

这第三次我也改变了

    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)

但是,当我在其他设备上运行此应用程序时,总是会引发该错误.

But still when i run this app on another device which it always throws me that error.

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

But when i comment this line of code // Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown) and then i run the app after that i uncomment that line of code Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown) and then i run the app again and finally it works. But sadly, when i run another iOS device it still gives me error. I wonder why?

推荐答案

按照步骤

1)导入Firebase和FirebaseAuth

1) Import Firebase and FirebaseAuth

import Firebase import FirebaseAuth

import Firebase import FirebaseAuth

2)在didFinishLaunchingWithOptions中配置firebase.

2) In didFinishLaunchingWithOptions Configure firebase.

FirebaseApp.configure()

3)在AppDelegate中编写这两个函数.

3) Write these two func in 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类中,重复步骤 first ,并编写用于在所需的手机号码上发送OTP的代码.

4) In your ViewController class, repeat step first and write code for send OTP on Mobile Number, which you want.

@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

    }

}

这篇关于使用电话号码身份验证的令牌不匹配-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆