firebase 为生产和开发构建方案添加两个GoogleService-Info.plist

oxcyiej7  于 2023-02-05  发布在  Go
关注(0)|答案(1)|浏览(169)

之前的一切,我已经读了这篇文章Use different GoogleService-Info.plist for different build schemes
但不知怎么的我还是不喜欢。
我有两个生产和开发构建方案,并且有两个不同的GoogleService-Info.plist
在我的第一次尝试中,我将两个谷歌文件重命名为GoogleService-Info-prodGoogleService-Info-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-Dev.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.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

之后应用程序可以正常运行。但在日志中我有这个警告

[FirebaseCore][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.

因此,它可能意味着文件名必须为GoogleService-Info.plist,并且不适用于GoogleService-Info-prodGoogleService-Info-dev
在我的第二次尝试中,我将所有两个文件都设置为GoogleService-Info.plist,因此会导致错误:

"Multiple commands produce GoogleService-Info.plist"

为了避免此错误,我从构建阶段〉复制捆绑包资源中删除了这两个文件
并使firebase设置功能如下:

FirebaseApp.configure()
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

然后加上这个脚本

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}

# 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

顺便说一句:我必须检查For install builds only,否则我会得到错误:

Command PhaseScriptExecution failed with a nonzero exit code

然后运行后,应用程序将崩溃,它说firebase找不到GoogleService-Info.plist-这是因为我从构建阶段〉复制捆绑资源中删除了它,以避免多文件名问题。
任何人都可以帮助如何解决这个问题,并成功添加谷歌文件。
顺便说一句:我不关心任何Firebase分析
非常感谢

avwztpqn

avwztpqn1#

我已经在我开发的一个应用程序中实现了这个功能。你很快就可以解决它了。我的方法是在我的项目中添加不同的GoogleService-Info.plist文件(我有4个方案,添加了4个文件),放在不同的文件夹下。这些文件不包括在任何目标中(所有目标都在身份检查器下的目标成员中取消选中),它们只是项目的一部分。
我编写了一个构建阶段脚本,根据我的配置(它又链接到我的方案)将适当的文件复制到构建中。

FILENAME=GoogleService-Info.plist

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

if [ "${CONFIGURATION}" == "MY_CONFIG_1" ]
then
    CONFIGFILE1=${PROJECT_DIR}/MYPROJ/CONFIG1DIRECTORY/${FILENAME}
    
    if [ ! -f $CONFIGFILE1 ]
    then
        echo "File not found"
        exit 1
    fi
    
    cp "${CONFIGFILE1}" "${DESTINATION}"
    
elif [ "${CONFIGURATION}" == "MY_CONFIG_2" ]
then
    CONFIGFILE2=${PROJECT_DIR}/MYPROJ/CONFIG2DIRECTORY/${FILENAME}
    
    if [ ! -f $CONFIGFILE2 ]
    then
        echo "File not found"
        exit 1
    fi
    
    cp "${CONFIGFILE2}" "${DESTINATION}"
    
fi

这里的关键是,我使用的名称GoogleService-Info.plist的所有4个文件,并只是复制所需的一个应用程序,而建设。

相关问题