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

嘿,我正在玩新的firebase iOS SDK,在我的项目中,我只有一个目标,我创build了两个configuration,debugging和发布,具有不同的捆绑器标识符,但似乎从firebase下载的configuration文件只支持一个捆绑标识符。

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

谢谢!

更新:请参阅Firebase官方文档 https://firebase.google.com/docs/configure/#support_multiple_environments_in_your_ios_application

更新更简单的解决scheme:

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 

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

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

 // 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) 

如果我运行暂存目标,我将收到Firebase关于“无法findconfiguration文件:”GoogleService-Info.plist“”的投诉。 但之后它会显示“Firebase Analytics v.300000开始”。

当我在Firebase仪表板上进行检查时,生产和临时应用程序都会logging传入的用户事件。

上面提到的configureWithOptions方法在Firebase文档中没有logging,我通过检查它的源代码来了解它。 我不确定调用它的缺点是什么。 我很想听听其他的方法。

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

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

 ROOT/config/UAT/GoogleService-Info.json ROOT/config/PROD/GoogleService-Info.json 

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

Xcode项目建议文件夹结构

现在您需要在Build阶段添加一个运行脚本。 这将需要在Compile Sources阶段之前添加:

将运行脚本拖到上面的编译源

这个运行脚本需要find合适的json文件并将其复制到构build应用程序目录中,这意味着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}/" 

我估计你也可以对Google Analytics使用相同的逻辑,它使用类似的jsonconfiguration文件设置。

如果你有单一的目标,更好的方法是适当地命名你的configuration并加载它们如下。 似乎工作正常的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) 

我没有以不同的名称存储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 

将Google Service plists添加到您的项目中,使用不同的名称:

在这里输入图像说明

然后,对于每个Build Scheme,selectEdit Scheme并为环境variables添加一个唯一的值,如下所示:

在这里输入图像说明

因此,另一个构buildscheme的BUILD_FOR环境variables将被设置为“DEV”。

然后,您将在App Delegate中检查此variables,并根据构build的scheme来configurationFIRApp:

  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!) 

在AppDeletage.m中使用Objective-C:

 #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]; 

Firebase包括现在如何执行此操作的官方文档 。 我在我的应用程序中使用这种方法,它工作得很好。

更具体地说,我在XCode中定义了一个自定义Swift标志“BUILD_TYPE”,并为每个scheme定义了该标志的唯一值。 然后,在我的AppDelegate中,我告诉Firebase哪些GoogleService-___.plist文件要在运行时通过此Swift标志值加载:

  let filePath = Bundle.main.path(forResource: "GoogleService-" + Bundle.main.infoDictionary!["BUILD_FLAVOR"] as! String, ofType: "plist") guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!) else { fatalError("Couldn't load config file") } FirebaseApp.configure(options: fileopts)