swift - 使用 swift4 在 xcode 中对单个项目使用不同的 GoogleService-Info.plist

标签 swift firebase firebase-realtime-database macros

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

最佳答案

对于此确认,您必须执行以下步骤:

  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

关于swift - 使用 swift4 在 xcode 中对单个项目使用不同的 GoogleService-Info.plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56626467/

相关文章:

javascript - Firebase注册一次,登录不同应用

javascript - Firebase onAuthStateChange 登录时触发两次

ios - 带有 nil 包装的自定义 flatMapLatest

ios - UITableViewCell 中的 ScrollView 不会保存位置

swift - 使用侧边菜单 CosmicMaterial 时嵌入 NavigationController

javascript - Angular 中的异步电子邮件验证

javascript - 使用 javascript 从 Firebase 检索数据

ios - 滚动时更改 Swift Collection View 数据

Firebase 实时数据库当前给出 TRIGGER_PAYLOAD_TOO_LARGE 错误

android - 使用 firebase 的用户数据的 json 格式