【发布时间】:2019-10-30 18:16:30
【问题描述】:
我有一个项目,但有 4 个不同的环境(开发、分期、QA、生产)。我已经从移动设备的设置中给出了他们的(环境的 web 服务 url)路径。现在我想为所有这些不同的环境使用不同的 GoogleService-info.plist。就像我从后端选择 Dev 时,项目应该只使用 Dev 项目的 GoogleService-Info.plist。这些 GoogleService-Info.plist 是在 4 个不同的帐户上创建的。项目应该以编程方式采用 GoogleService-info.plist 的路径。我试过下面的代码
1] 通过参考this url,我创建了两个文件夹 Dev 和 QA(目前)并尝试通过编程方式给出它们的路径
#if DEV
print("[FIREBASE] Development mode.")
filePath = Bundle.main.path(forResource: "GoogleService-Info",
ofType: "plist", inDirectory: "Dev")
#elseif QA
print("[FIREBASE] QA mode.")
filePath = Bundle.main.path(forResource: "GoogleService-Info",
ofType: "plist", inDirectory: "QA")
#endif
let options = FirebaseOptions.init(contentsOfFile: filePath)!
FirebaseApp.configure(options: options)
但是会报错
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
在
let options = FirebaseOptions.init(contentsOfFile: filePath)!
这一行
2] 其次,我通过 GoogleService-Info-QA.plist 更改了 GoogleService-Info.plist 的名称,并尝试以编程方式访问此文件
private func configureFirebase() {
guard let plistPath = Bundle.main.path(forResource:
"GoogleService-Info-QA", ofType: "plist"),
let options = FirebaseOptions(contentsOfFile: plistPath)
else { return }
FirebaseApp.configure(options: options)
}
但是会报错
Terminating app due to uncaught exception 'FIRAppNotConfigured',
reason: 'Failed to get default Firebase Database instance. Must
call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift)
before using Firebase Database.
【问题讨论】:
-
你的第二个错误表明你没有在你的appdelegate FirebaseApp.configure()中配置firebase
-
是的,它是正确的,因为我想以编程方式提供 GoogleService-Info.plist 路径。并且编译器无法找到该路径。我不知道如何以编程方式给出该路径
标签: swift firebase firebase-realtime-database macros