我正在使用一个针对生产环境的构建方案和一个针对暂存环境的构建方案(带有2个不同的包标识符)并且我正在尝试为每个构建方案使用单独的GoogleService-Info.plist。
是否有办法在初始化GCM(和Google登录)时手动选择要使用的plist文件?或者是否可以避免使用plist并手动进行设置?
谢谢!
我正在使用一个针对生产环境的构建方案和一个针对暂存环境的构建方案(带有2个不同的包标识符)并且我正在尝试为每个构建方案使用单独的GoogleService-Info.plist。
是否有办法在初始化GCM(和Google登录)时手动选择要使用的plist文件?或者是否可以避免使用plist并手动进行设置?
谢谢!
测试过的版本:
PATH_TO_GOOGLE_PLISTS="${PROJECT_DIR}/SM2/Application/Firebase"
case "${CONFIGURATION}" in
"Debug_Staging" | "AdHoc_Staging" )
cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-Info-dev.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;
"Debug_Production" | "AdHoc_Production" | "Distribution" | "Test_Production" )
cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-Info-prod.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;
*)
;;
esac
configure(options:)
也是如此。 https://github.com/firebase/quickstart-ios/issues/5 - Rob Bajorek@inidona的答案对我有用。在将其转换为Swift后,它可以正常工作。
适用于Swift 2.3:
let filePath = NSBundle.mainBundle().pathForResource("GoogleService-Info", ofType: "plist")
let options = FIROptions(contentsOfFile: filePath)
FIRApp.configureWithOptions(options)
对于Swift 3.0:
let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")!
let options = FIROptions(contentsOfFile: filePath)
FIRApp.configure(with: options)
对于Swift 4.0:
let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")!
let options = FirebaseOptions(contentsOfFile: filePath)
FirebaseApp.configure(options: options!)
GoogleService-Info.plist
的不同文件,或者可能是两个具有不同名称的文件。请提供更多关于实际文件名称和位置的信息。 - Varun Gupta如果GoogleService-Info.plist
名称不同,将会影响您的分析结果。 Firebase会提醒您。因此,这些运行时解决方案均无法提供最佳的分析结果。
有两个解决方案不会影响分析:
使用每个方案的不同目标并将每个GoogleService-Info.plist
的版本与其自己的目标相关联。在Xcode右侧的文件检查器中查看目标成员资格。更多信息请参见此问题。
使用构建阶段脚本复制正确的版本的GoogleService-Info.plist
到构建目录中。我针对暂存和生产使用不同的束 ID。这使我能够并行安装应用程序的两个版本。下面的脚本还意味着我可以使用束 ID 命名不同的GoogleService-Info.plist
文件。例如:
GoogleService-Info-com.example.app.plist
GoogleService-Info-com.example.app.staging.plist
PATH_TO_CONFIG=$SRCROOT/Config/GoogleService-Info-$PRODUCT_BUNDLE_IDENTIFIER.plist
FILENAME_IN_BUNDLE=GoogleService-Info.plist
BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
echo cp $PATH_TO_CONFIG "$BUILD_APP_DIR/$FILENAME_IN_BUNDLE"
cp $PATH_TO_CONFIG "$BUILD_APP_DIR/$FILENAME_IN_BUNDLE"
PATH_TO_CONFIG
以适应您的设置。
错误(Xcode):无法从构建环境中获取 Google Services 文件中的 GOOGLE_APP_ID
。 - Parth Bhanderi在 Xcode 中,创建两个目录:Debug
和 Release
。将每个GoogleService-Info.plist
文件放入其中。
在 AppDelegate.m
文件中的 didFinishLaunchingWithOptions
方法内添加以下代码:
Objective-C
NSString *filePath;
#ifdef DEBUG
NSLog(@"[FIREBASE] Development mode.");
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist" inDirectory:@"Debug"];
#else
NSLog(@"[FIREBASE] Production mode.");
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist" inDirectory:@"Release"];
#endif
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
[FIRApp configureWithOptions:options];
Swift 4
var filePath:String!
#if DEBUG
print("[FIREBASE] Development mode.")
filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist", inDirectory: "Debug")
#else
print("[FIREBASE] Production mode.")
filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist", inDirectory: "Release")
#endif
let options = FirebaseOptions.init(contentsOfFile: filePath)!
FirebaseApp.configure(options: options)
将Debug
和Release
文件夹同时拖到Build Phases > Copy Bundle Resources
中:
就这样 :)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
[FIRApp configureWithOptions:options];
无法找到配置文件:'GoogleService-Info.plist'
。 - Babken Vardanyan这个回答受到了@abbood的启发,但更具体地说明了如何操作。
对于每个目标,例如dev、stg、prod:
GoogleService-Info.plist
下载到以目标命名的单独文件夹中Add files to "your app"
GoogleService-Info.plist
的文件夹,确保选中Copy items if needed
和Create groups
,只勾选列表中对应的目标,然后按Add
就这样。现在您应该有类似于这样的结构
当您构建目标时,将使用正确的GoogleService-Info.plist
。
GoogleServices-Info.plist
的引用,否则您可能会遇到以下错误:error: Multiple commands produce GoogleServices-Info.plist...
- Dennis Barzanoff我注意到谷歌希望代码中的文件名为GoogleServiceInfo.plist:
* The method |configureWithError:| will read from the file GoogleServices-Info.plist bundled with
* your app target for the keys to configure each individual API. To generate your
* GoogleServices-Info.plist, please go to https://developers.google.com/mobile/add
*
* @see GGLContext (Analytics)
* @see GGLContext (SignIn)
*/
@interface GGLContext : NSObject
关键短语是这个
从与您的应用程序目标捆绑的GoogleServices-Info.plist文件中读取
所以我只是将相同的文件复制到不同的目录中,并将其绑定到不同的目标:
虽然已经晚了,但我认为我必须发布这个答案来帮助新开发者。我发现了一篇非常好的文章,解决了我的问题,我保证它也能帮助到你:
请查看这篇文章,同样可以解决你的问题。
步骤1:
将与Firebase开发环境相对应的GoogleService-Info.plist复制到Dev目录中。类似地,将与Firebase生产环境相对应的GoogleService-Info.plist复制到Prod目录中。确保取消选中“如果需要,复制项目”以及“添加到目标”的所有目标。
步骤2:
在Xcode项目导航器中,选择应用程序目标。切换到顶部的Build Phases选项卡,然后添加一个新的运行脚本阶段。将该阶段命名为“设置Firebase环境GoogleService-Info.plist”或类似名称,并将其放置在“复制Bundle资源”步骤之前。
步骤3:
实现一个shell脚本,根据构建配置将适当的GoogleService-Info.plist复制到应用程序包中。将以下shell脚本复制并粘贴到刚创建的运行脚本阶段中:
# 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}/${TARGET_NAME}/Firebase/Dev/${GOOGLESERVICE_INFO_PLIST}
GOOGLESERVICE_INFO_PROD=${PROJECT_DIR}/${TARGET_NAME}/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
我发现在只有单个目标的情况下,唯一可行的方法是在构建过程中复制与构建配置对应的plist;但是这里的答案在如何做方面存在细节上的差异,而且没有一个方便我使用的。我的答案基于@KnightFighter的回答和Medium上的这篇文章。
首先添加所有不同的plist到工程中,并使用不同的名称(它们不能作为资源添加到目标中):
接下来创建用户定义的构建设置,在其中可以为每个构建配置分配特定的plist:
最后添加“运行脚本”阶段,其中包含以下代码:
GOOGLE_SERVICE_INFO_PLIST_SOURCE=${PROJECT_DIR}/${TARGET_NAME}/${GOOGLE_SERVICE_INFO_PLIST_FILENAME}
if [ ! -f $GOOGLE_SERVICE_INFO_PLIST_SOURCE ]
then
echo "${GOOGLE_SERVICE_INFO_PLIST_SOURCE} not found."
exit 1
fi
GOOGLE_SERVICE_INFO_PLIST_DESTINATION="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
cp "${GOOGLE_SERVICE_INFO_PLIST_SOURCE}" "${GOOGLE_SERVICE_INFO_PLIST_DESTINATION}"
我认为这种方法有一些优点:
假设我们有两个配置集,develop
和 production
。你需要完成以下两件事:
FIREBASE_PLIST_PATH="${PROJECT_DIR}/App/Resources/Plists/GoogleService-Info-${CONFIGURATION}.plist"
echo "Firebase plist path: ${FIREBASE_PLIST_PATH}"
cp -r ${FIREBASE_PLIST_PATH} "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
在FirebaseCrashlytics
脚本之前放置运行脚本。
您可以像单一方案一样初始化Firebase:FirebaseApp.configure()