为Prod和Dev构建方案配置不同GoogleService-Info.plist遇阻求助
解决不同Build Scheme配置不同GoogleService-Info.plist的方案
方案一:保留代码指定配置文件,消除警告
你的第一种尝试本身是可行的,日志里的警告是Firebase默认会尝试查找GoogleService-Info.plist,但你已通过代码手动完成配置,这个警告可忽略,也可以通过以下方式消除:
- 确保重命名后的两个plist文件(
GoogleService-Info-prod.plist、GoogleService-Info-dev.plist)都添加到Build Phases > Copy Bundle Resources中,且对应Scheme的Configuration(比如Production关联Release、Development关联Debug)都勾选了文件的Target Membership。 - 修正代码里的错误文案(Production分支误写了Dev文件缺失提示):
#if Production if let filePath = Bundle.main.path(forResource: "GoogleService-Info-prod", ofType: "plist"), let options = FirebaseOptions(contentsOfFile: filePath) { FirebaseApp.configure(options: options) } else { fatalError("GoogleService-Info-prod.plist is missing!") } #elseif Development if let filePath = Bundle.main.path(forResource: "GoogleService-Info-dev", ofType: "plist"), let options = FirebaseOptions(contentsOfFile: filePath) { FirebaseApp.configure(options: options) } else { fatalError("GoogleService-Info-dev.plist is missing!") } #endif FirebaseConfiguration.shared.setLoggerLevel(.min) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in } application.registerForRemoteNotifications() Messaging.messaging().delegate = self
- 若完全不想看到警告,可在Firebase初始化前添加一行配置,禁止自动查找默认plist:
FirebaseConfiguration.shared.setDefaults(fromPlist: nil) // 之后执行上述手动配置代码
方案二:用脚本替换文件,确保Bundle存在正确plist
针对你第二种尝试的崩溃问题,是脚本执行时机或路径错误导致的,调整步骤如下:
- 将两个
GoogleService-Info.plist分别放在不同文件夹(比如Firebase/Dev/和Firebase/Prod/),不要添加到Copy Bundle Resources。 - 修改脚本并调整执行时机:
- 取消勾选
For install builds only,将脚本放在Build Phases > Copy Bundle Resources之前执行。 - 修正脚本路径与变量逻辑:
- 取消勾选
# 直接指定文件名,避免变量歧义 GOOGLESERVICE_INFO_PLIST="GoogleService-Info.plist" GOOGLESERVICE_INFO_DEV="${PROJECT_DIR}/${TARGET_NAME}/Application/Firebase/Dev/${GOOGLESERVICE_INFO_PLIST}" GOOGLESERVICE_INFO_PROD="${PROJECT_DIR}/${TARGET_NAME}/Application/Firebase/Prod/${GOOGLESERVICE_INFO_PLIST}" # 检查文件是否存在 if [ ! -f "$GOOGLESERVICE_INFO_DEV" ]; then echo "错误:找不到开发环境的GoogleService-Info.plist,请检查路径" exit 1 fi if [ ! -f "$GOOGLESERVICE_INFO_PROD" ]; then echo "错误:找不到生产环境的GoogleService-Info.plist,请检查路径" exit 1 fi # 目标路径:复制到项目主目录,让Copy Bundle Resources能识别 PLIST_TARGET="${PROJECT_DIR}/${TARGET_NAME}/Application/Firebase/${GOOGLESERVICE_INFO_PLIST}" if [ "${CONFIGURATION}" == "Release" ]; then echo "使用生产环境配置文件" cp "$GOOGLESERVICE_INFO_PROD" "$PLIST_TARGET" else echo "使用开发环境配置文件" cp "$GOOGLESERVICE_INFO_DEV" "$PLIST_TARGET" fi
- 将主目录下的
GoogleService-Info.plist(脚本复制后的文件)添加到Copy Bundle Resources,确保它会被打包进App。 - 代码中保持
FirebaseApp.configure()即可,此时Bundle中已存在正确的plist文件。
额外注意事项
- 确认Scheme与Configuration的关联正确(比如Debug对应Development、Release对应Production)。
- 执行Command+Shift+K清理项目缓存后重新构建,避免旧文件残留。
内容的提问来源于stack exchange,提问作者martin wotterman
相关产品推荐
相关产品推荐

