Vis*_*mbo 3 xcode token ios swift firebase-authentication
我正在尝试使用 firebase 使用电话号码身份验证登录帐户。
最初,我在我的设备上部署了该应用程序,并且运行良好。但是当我尝试将应用程序部署到另一台设备时,它会抛出错误Token Mismatch。
我搜索了几个答案,stackoverflow然后找到了这个答案,然后我遵循了,但它对我不起作用。
我已经检查了以下内容:
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 设备时,它仍然给我错误。我想知道为什么?
按照步骤操作
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
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 }\nRun Code Online (Sandbox Code Playgroud)\n override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {\n let firebaseAuth = Auth.auth()\n firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)\n }\nRun Code Online (Sandbox Code Playgroud)\n| 归档时间: |
|
| 查看次数: |
4769 次 |
| 最近记录: |