我有一个项目,但有 4 个不同的环境(开发、暂存、质量检查、生产)。我已经从移动设备的设置中给出了他们的(环境的网络服务 URL)路径。现在我想为所有这些不同的环境使用不同的 GoogleService-info.plist 。就像当我从后端选择 Dev 时,该项目应该仅采用 Dev 项目的 GoogleService-Info.plist 。这些 GoogleService-Info.plist 是在 4 个不同的帐户上创建的。项目应以编程方式采用 GoogleService-info.plist 的路径。我尝试过以下代码
1]通过参考this https://medium.com/@brunolemos/how-to-setup-a-different-firebase-project-for-debug-and-release-environments-157b40512164url ,我创建了两个文件夹 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
on
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.