在新的Firebase中,如何在xcode中使用多个配置文件?

coo*_*eet 34 xcode ios firebase

嘿我正在玩新的firebase iOS SDK,在我的项目中,我只有一个目标,我创建了两个配置,Debug和Release,具有不同的bundler标识符,但似乎从firebase下载的配置文件只支持一个bundle标识符.

所以任何人都知道如何在多包标识符xcode项目中使用firebase?

谢谢!

Chr*_*hen 47

更新:请参阅Firebase官方文档 https://firebase.google.com/docs/projects/multiprojects

更新了更简单的解决方案:

1. keep the same names for both GoogleService-Info.plist
2. put one GoogleService-Info.plist inside a subfolder, say "staging"
3. add references to both files in Xcode while linking them to corresponding targets
4. just use FIRApp.configure() in your AppDelegate, done 
Run Code Online (Sandbox Code Playgroud)

我的第一次尝试没有解决问题:

我将第二个GoogleService-Info.json重命名为其他内容,并使用以下代码从AppDelegate配置Firebase.

// Swift code
#if STAGING
    let firebasePlistFileName = "GoogleService-Staging-Info"
#else
    let firebasePlistFileName = "GoogleService-Info"
#endif
let firbaseOptions = FIROptions(contentsOfFile: NSBundle.mainBundle().pathForResource(firebasePlistFileName, ofType: "plist"))
FIRApp.configureWithOptions(firbaseOptions)
Run Code Online (Sandbox Code Playgroud)

如果我运行暂存目标,我将收到Firebase关于"无法找到配置文件:'GoogleService-Info.plist'."的投诉.但随后它说'Firebase Analytics v.3300000已启动'.

在我查看Firebase仪表板时,生产和暂存应用程序都记录了传入的用户事件.

Firebase文档中没有记录上述configureWithOptions方法,我通过检查其源代码来解决它.我不确定这可能是什么原因.我很乐意听到其他方法.

  • 此处记录了FIRApp.configureWithOptions:https://firebase.google.com/docs/reference/ios/firebaseanalytics/interface_f_i_r_app#a2e4b970bfb6ed8b606c456f42a82b967 (4认同)
  • 如果您有不同的目标,您的更新答案将有效.原来的答案对我没用.Firebase配置仍尝试打开Goog​​leService-Info文件.谢谢! (2认同)
  • 它不适用于一个目标. (2认同)
  • 您的原始答案现在可以使用最新版本的Firebase.configureWithOptions文档在这里,如果有人有兴趣:https://firebase.google.com/docs/reference/ios/firebasecore/api/reference/Classes/FIROptions#/c:objc(cs)FIROptions(im)initWithContentsOfFile: (2认同)

Chr*_*way 17

我已经实现了类似的东西,因为我有两个方案用于具有不同包标识符的单个目标.在下面的例子中,我有两个不同的方案,一个用于UAT,一个用于PROD.

创建两个GoogleService-Info.json文件,并将它们放入不同文件夹中的项目目录(不是Xcode项目),例如

ROOT/config/UAT/GoogleService-Info.json
ROOT/config/PROD/GoogleService-Info.json
Run Code Online (Sandbox Code Playgroud)

然后将文件添加到Xcode项目中,如下所示:

Xcode项目建议文件夹结构

现在,您需要在Build Phases中添加Run Script.这需要在Compile Sources阶段之前添加:

将

此运行脚本获取适当定位的json文件并将其复制到构建应用程序目录中,这意味着Firebase/Google将识别它与在单个标识符设置中识别文件的方式相同.

isUAT=`expr "$GCC_PREPROCESSOR_DEFINITIONS" : ".*UAT=\([0-9]*\)"`

RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/config/PROD

if [ $isUAT = 1 ]; then
    RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/config/UAT
fi

BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app

echo "Copying all files under ${RESOURCE_PATH} to ${BUILD_APP_DIR}"
cp -v "${RESOURCE_PATH}/"* "${BUILD_APP_DIR}/"
Run Code Online (Sandbox Code Playgroud)

我估计您也可以将相同的逻辑用于Google Analytics,它使用类似的json配置文件设置.


小智 12

如果您有单个目标,更好的方法是恰当地命名您的配置并加载它们如下.似乎适用于UAT环境示例:Swift 3.0

    var fireBaseConfigFile = Bundle.main.path(forResource: "GoogleService-Info-PROD", ofType: "plist")
   #if UAT
       fireBaseConfigFile = Bundle.main.path(forResource: "GoogleService-Info-UAT", ofType: "plist")
   #endif

   guard let firOptions = FIROptions(contentsOfFile: fireBaseConfigFile) else {
       assert(false, "Failed to load Firebase config file")
       return
   }

   FIRApp.configure(with: firOptions)
Run Code Online (Sandbox Code Playgroud)

  • 这是我发现并且第一次完美工作的最简单的方法.我写了一个关于该方法的快速要点:https://gist.github.com/cohenadair/3a2aff5084603bfa65824f09cf74206e (5认同)
  • 您永远不要在运行时执行此类操作,因为它会影响您的生产分析 (2认同)

Tai*_* Le 10

我确实存储了2个不同名称的GoogleService-Info:

  • GoogleService-Info.plist 用于生产
  • GoogleService-Info-Debug.plist 发展

然后转到Build Phases,添加新的运行脚本:

if [ "${CONFIGURATION}" == "Release" ]; then
cp -r "${PROJECT_DIR}/PathToYourGoogleServiceInfoFile/GoogleService-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"

echo "Production plist copied"

elif [ "${CONFIGURATION}" == "Debug" ]; then

cp -r "${PROJECT_DIR}/PathToYourGoogleServiceInfoFile/GoogleService-Info-Debug.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"

echo "Development plist copied"
fi
Run Code Online (Sandbox Code Playgroud)


vik*_*lla 8

将两个Google服务plist添加到您的项目中,使用不同的名称:

在此输入图像描述

然后,对于每个构建方案,选择Edit Scheme并为环境变量添加唯一值,如下所示:

在此输入图像描述

因此,例如,其他构建方案的BUILD_FOR环境变量将设置为"DEV".

然后,您将在App Delegate中检查此变量,并根据构建的方案配置FIRApp:

    let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
    var firebasePlistFileName = "GoogleService-Info"

    if buildFor == "PROD" {
        firebasePlistFileName = "GoogleService-Prod-Info"
    }

    let firbaseOptions = FIROptions(contentsOfFile: Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist"))
    FIRApp.configure(with: firbaseOptions!)
Run Code Online (Sandbox Code Playgroud)


Bru*_*mos 7

在你的 AppDeletage.m 中使用 Objective-C:

NSString *filePath;
#ifdef DEBUG
  filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info-Debug" ofType:@"plist"];
#else
  filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info-Production" ofType:@"plist"];
#endif

  FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
  [FIRApp configureWithOptions:options];
Run Code Online (Sandbox Code Playgroud)


Rod*_*ici 6

我看到很多使用 Targets 的回复,但如果您想按方案进行区分,您可以转到:

编辑方案... > 选择方案 > 构建 > 操作后

  • 添加运行脚本
  • 提供构建设置:(选择您想要的目标)
  • 添加以下脚本
PATH_TO_GOOGLE_PLISTS="${PROJECT_DIR}/MVCVMRC/Supporting Files/FirebasePlist"

cp -r "$PATH_TO_GOOGLE_PLISTS/Brazil/GoogleService-Info-preprod.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
Run Code Online (Sandbox Code Playgroud)

不要忘记更改PATH_TO_GOOGLE_PLISTS

样本: 样本