繁体   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