集成适用于iOS的Flutter Firebase手机身份验证的问题



我知道这个问题已经被问了好几次,但没有一个帮助我解决了我的问题。我按照文档中提到的所有内容将Firebase Auth配置为iOS。另外,我正在使用Flutter进行应用程序开发。

这是用于验证电话号码的飞镖代码:

await _auth.verifyPhoneNumber(
phoneNumber: _phoneNumberController.text,
timeout: const Duration(minutes: 2),
verificationCompleted: (user) {
print('verification completed');
},
verificationFailed: (AuthException authException) {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}'),
));
},
codeSent: (String verificationId, [int forceResendingToken]) {
_verificationId = verificationId;
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: const Text(
'Please check your phone for the verification code.'),
));
},
codeAutoRetrievalTimeout: (String verificationId) {
_verificationId = verificationId;
});

当我提供电话号码并进行验证时,它会给我一条错误消息,指出:

如果禁用了应用程序委托重排,则需要将 UIApplicationDelegate 收到的远程通知转发到 FIRAuth 的 canHandleNotification: 方法。

由于我使用的是 Flutter,我只是将以下代码粘贴到 AppDelegate 中.swift我不确定它是否正确。我将在下面粘贴整个类:

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
//return super.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
// Further handling of the device token if needed by the app
// ...
}
override func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
// This notification is not auth related, developer should handle it.
}
// For iOS 9+
override func application(_ application: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
if Auth.auth().canHandle(url) {
return true
}
// URL not auth related, developer should handle it.
return true;
}
}

到目前为止我尝试过:

  1. 在启用推送通知的情况下创建了应用 ID,在 Apple 开发人员帐户中生成了密钥和配置文件
  2. 已将密钥上传到 iOS 应用下的 Firebase 控制台
  3. 在 Xcode 中为 reCaptcha 配置了自定义 URL 方案,在 Xcode 功能下启用了推送通知和后台远程通知
  4. 将上述指定的代码添加到应用程序委托中.swift
  5. 清理并运行项目

版本信息:

  • 火力核心:0.3.0^2
  • 火力基础身份验证:0.8.1+4

我可能错过了一些信息,但我尽力包括所有必要的东西。

任何解决此问题的解决方案/想法将不胜感激。

谢谢。

我知道已经有一段时间了,但最近我在最新版本的 Flutter/iOS 项目中遇到了同样的问题,我以这种方式解决了:

My Firebase Flutter 包版本:

firebase_crashlytics: ^3.3.3
firebase_analytics: ^10.4.3
firebase_app_check: ^0.1.4+3
firebase_auth: ^4.6.3
firebase_core: ^2.14.0
cloud_firestore: ^4.8.2
firebase_remote_config: ^4.2.3
firebase_dynamic_links: ^5.3.3
firebase_messaging: 14.4.1

1 - 添加到 info.plist> FirebaseAppDelegateProxyEnabled 为 true(我认为这是可选的设置为 true):

<key>FirebaseAppDelegateProxyEnabled</key>
<true/>

在应用程序委托中

2 - 导入:

import FirebaseMessaging
import FirebaseAuth

3 - 将 apnstoken 从您的应用程序添加到身份验证库:

override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Auth.auth().setAPNSToken(deviceToken, type: .unknown)
}

4 - 然后添加应用程序 didReceiveRemoteNotification 谁负责在您的应用程序在后台时接收消息

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
let firebaseAuth = Auth.auth()
if (firebaseAuth.canHandleNotification(userInfo)){
completionHandler(.noData)
return
}
}

最新更新