我有一个项目,但是有4个不同的环境(开发,标记,质量检查,生产)。我从移动设备的设置中给出了它们(环境的Web服务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-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.
答案 0 :(得分:0)
将此代码段放置在应用程序的$rootScope
委托函数的AppDelegate.swift中,位于didFinishLaunchingWithOptions
之前的某个位置
return true
您需要确保相应地命名了plist文件,并确保它们是目标的一部分:
您的文件应放置在主文件夹中,就像放置普通的Google-Info.plist文件一样。
答案 1 :(得分:0)
要进行确认,您必须执行以下步骤:
//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_DEV = $ {PROJECT_DIR} / projectFolder / Firebase / Dev / $ {GOOGLESERVICE_INFO_PLIST} GOOGLESERVICE_INFO_PROD = $ {PROJECT_DIR} / projectFolder / Firebase / Prod / $ {GOOGLESERVICE_INFO_PLIST}
projectFolder 是您当前的项目文件夹
答案 2 :(得分:0)
我找到了解决方案。我通过各自的环境将GoogleService-Info.plists文件重命名。并在构建阶段->复制捆绑资源中添加了这些文件。然后根据所选环境添加以下代码
guard let plistPath = Bundle.main.path(forResource: "nameOfTheGoogleService-Info.plistFileAsPerTheEnvironment", ofType: "plist"),
let options = FirebaseOptions(contentsOfFile: plistPath)
else { return }
if FirebaseApp.app() == nil{
FirebaseApp.configure(options: options)
}
较小的变化是,当用户更改应用程序的环境时,他应该从后台删除应用程序,然后再次打开它。然后,AppDeleagte会根据其选择的环境采用相应GoogleService-Info.plist的路径