使用swift4

时间:2019-06-17 07:19:47

标签: swift firebase firebase-realtime-database macros

我有一个项目,但是有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.

3 个答案:

答案 0 :(得分:0)

将此代码段放置在应用程序的$rootScope委托函数的AppDelegate.swift中,位于didFinishLaunchingWithOptions之前的某个位置

return true

您需要确保相应地命名了plist文件,并确保它们是目标的一部分:

  • 选择GoogleService-Info-DEV文件,然后在右侧的FileInspector中确保选中应用程序目标的复选框。对GoogleService-Info-QA文件执行相同的操作。

您的文件应放置在主文件夹中,就像放置普通的Google-Info.plist文件一样。

答案 1 :(得分:0)

要进行确认,您必须执行以下步骤:

  1. 转到项目设置
  2. 选择开发目标
  3. 转到构建阶段
  4. 点击图标并创建名为GOOGLESERVICE_INFO_PLIST的新运行脚本
  5. 使用以下脚本

//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 是您当前的项目文件夹

enter image description here

答案 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的路径