简体   繁体   English

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

[英]Token Mismatch using Phone Number Authentication - iOS

I am trying to login account using phone number authentication using firebase.我正在尝试使用 firebase 使用电话号码身份验证登录帐户。

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 .但是,当我尝试将应用程序部署到另一台设备时,它会抛出错误Token Mismatch

I have searched several answers in stackoverflow and then i found this answer and i followed but it didn't work for me.我在stackoverflow搜索了几个答案,然后我找到了这个答案并遵循了它,但它对我不起作用。

I have checked following :我检查了以下内容:

  1. Ensure I had both a valid development and production APNS certificate uploaded to the Firebase Dashboard, under 'Project Settings' > 'Cloud Messaging'.确保我已将有效的开发和生产 APNS 证书上传到 Firebase 仪表板的“项目设置”>“云消息传递”下。 (My APNS certificate is valid till next year). (我的 APNS 证书有效期到明年)。
  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.在 Xcode 的 .entitlements 文件中,确保 APS Environment 值设置为“development”或“production”,具体取决于您的测试情况。 (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.最后(这是我所缺少的),检查您的 AppDelegate.swift 内部和didRegisterForRemoteNotificationsWithDeviceToken的方法内部,将值从.sandbox更改为.prod ,或更改为.unknown以让应用程序包确定要使用的令牌类型,基于在您的配置文件中。

This 3rd i also changed这第三我也改变了

    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.但是当我在另一台设备上运行这个应用程序时,它总是向我抛出那个错误。

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.但是当我注释这行代码// Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)然后我运行应用程序之后我取消注释那行代码Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)然后我再次运行该应用程序,最后它可以工作了。 But sadly, when i run another iOS device it still gives me error.但遗憾的是,当我运行另一台 iOS 设备时,它仍然给我错误。 I wonder why?我想知道为什么?

Follow steps按照步骤操作

1) Import Firebase and FirebaseAuth 1) 导入 Firebase 和 FirebaseAuth

import Firebase import FirebaseAuth

2) In didFinishLaunchingWithOptions Configure firebase. 2) 在 didFinishLaunchingWithOptions 中配置 firebase。

FirebaseApp.configure()

3) Write these two func in AppDelegate. 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) In your ViewController class, repeat step first and write code for send OTP on Mobile Number, which you want. 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