我有一个项目,但有 4 个不同的环境(开发、暂存、QA、生产)。我已经从移动设备的设置中给出了他们的(环境的网络服务 url)路径。现在我想为所有这些不同的环境使用不同的 GoogleService-info.plist。就像当我从后端选择 Dev 时,该项目应该只采用 Dev 项目的 GoogleService-Info.plist。这些 GoogleService-Info.plists 是在 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.plist 的名称更改为 GoogleService-Info-QA.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.
最佳答案
对于此确认,您必须执行以下步骤:
- 转到项目设置
- 选择您的开发目标
- 转到构建阶段
- 点击图标并创建名为 GOOGLESERVICE_INFO_PLIST 的新运行脚本
- 使用以下脚本
//Name of the resource we're selectively copying
GOOGLESERVICE_INFO_PLIST=GoogleService-Info.plist
//Get references to dev and prod versions of the GoogleService-Info.plist
//NOTE: These should only live on the file system and should NOT be part of the target (since we'll be adding them to the target manually)
GOOGLESERVICE_INFO_DEV=${PROJECT_DIR}/projectFolder/Firebase/Dev/${GOOGLESERVICE_INFO_PLIST}
GOOGLESERVICE_INFO_PROD=${PROJECT_DIR}/projectFolder/Firebase/Prod/${GOOGLESERVICE_INFO_PLIST}
//Make sure the dev version of GoogleService-Info.plist exists
echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_DEV}"
if [ ! -f $GOOGLESERVICE_INFO_DEV ]
then
echo "No Development GoogleService-Info.plist found. Please ensure it's in the proper directory."
exit 1
fi
//Make sure the prod version of GoogleService-Info.plist exists
echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_PROD}"
if [ ! -f $GOOGLESERVICE_INFO_PROD ]
then
echo "No Production GoogleService-Info.plist found. Please ensure it's in the proper directory."
exit 1
fi
//Get a reference to the destination location for the GoogleService-Info.plist
PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
echo "Will copy ${GOOGLESERVICE_INFO_PLIST} to final destination: ${PLIST_DESTINATION}"
//Copy over the prod GoogleService-Info.plist for Release builds
if [ "${CONFIGURATION}" == "Release" ]
then
echo "Using ${GOOGLESERVICE_INFO_PROD}"
cp "${GOOGLESERVICE_INFO_PROD}" "${PLIST_DESTINATION}"
else
echo "Using ${GOOGLESERVICE_INFO_DEV}"
cp "${GOOGLESERVICE_INFO_DEV}" "${PLIST_DESTINATION}"
fi
- 最后一步:确保您已将 GoogleService-Info.plist 放置在项目中的正确位置路径中,您可以找到附件图像以供引用。
GOOGLESERVICE_INFO_DEV=${PROJECT_DIR}/projectFolder/Firebase/Dev/${GOOGLESERVICE_INFO_PLIST} GOOGLESERVICE_INFO_PROD=${PROJECT_DIR}/projectFolder/Firebase/Prod/${GOOGLESERVICE_INFO_PLIST}
projectFolder 是你当前的项目文件夹
关于swift - 使用 swift4 在 xcode 中对单个项目使用不同的 GoogleService-Info.plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56626467/