【发布时间】:2020-12-16 19:38:28
【问题描述】:
我是 iOS 开发新手,正在努力使用 SwiftUI 中的新 @App 协议实现 Firebase 手机身份验证。 Firebase 的文档是为 UIKit 编写的,SwiftUI 的在线教程使用 AppDelegate 和 SceneDelegate 而不是 App 结构中的新 @main 协议。
我的具体问题如下:我将如何/在哪里注入我创建的这个身份验证状态类?
import Foundation
class AuthenticationState: NSObject, ObservableObject
{
@Published var loggedInUser: User?
@Published var isAuthenticating = false
@Published var error: NSError?
static let shared = AuthenticationState();
private let auth = Auth.auth();
fileprivate var currentNonce: String?
func login(with loginOption: LoginOption) {
self.isAuthenticating = true
self.error = nil
switch loginOption {
case .signInWithApple:
handleSignInWithApple()
}
}
func signup(email: String, password: String, passwordConfirmation: String) {
// TODO
}
private func handleSignInWithApple() {
// TODO
}
}
其次,AuthenticationState 类不知道 Firebase Auth 对象,我认为是因为它配置不正确。到目前为止,我在 AppDelegate 类中配置 Firebase:
import UIKit
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
然而,我还有一个 MapworkApp.swift 文件,我认为它应该替换 AppDelegate,但我不确定:
import SwiftUI
import Firebase
@main
struct MapworkApp: App {
let persistenceController = PersistenceController.shared
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
struct MapworkApp_Previews: PreviewProvider {
static var previews: some View {
/*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/
}
}
我目前收到的运行时错误:
020-12-16 13:22:34.416917-0700 Mapwork[1351:332863] 7.3.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more:
2020-12-16 13:22:34.417240-0700 Mapwork[1351:332633] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App Delegate's `application:didFinishLaunchingWithOptions:` (`application(_:didFinishLaunchingWithOptions:)` in Swift).'
*** First throw call stack:
(0x1863a586c 0x19b314c50 0x18629e4a4 0x102bc8918 0x1028465d8 0x1028466f4 0x102845c1c 0x102845be4 0x102ec96c0 0x102ecb1f8 0x18a32c5bc 0x102845c64 0x102854aa0 0x102854cbc 0x18cdba724 0x102854c18 0x102855028 0x185fda6b0)
libc++abi.dylib: terminating with uncaught exception of type NSException
terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App Delegate's `application:didFinishLaunchingWithOptions:` (`application(_:didFinishLaunchingWithOptions:)` in Swift).'
Message from debugger: The LLDB RPC server has exited unexpectedly. Please file a bug if you have reproducible steps.
任何帮助或指导将不胜感激,因为在线文档现在不适用。
【问题讨论】:
-
您不需要 AppDelegate。在 MapworkApp.swift 文件中,添加一个 init() 函数并在 init 中调用 FirebaseApp.configure()。
-
当我尝试这个时,我收到一个编译器错误,“init cannot be used as a keyword here”
-
init() 是一个函数。它应该看起来像: init() { FirebaseApp.configure() } ... 把它放在身体外面。
-
不幸的是,在这样做之后,我仍然收到上面相同的运行时错误。还是谢谢你的帮助。