ios - 如何在 Xcode 中更改 $(PRODUCT_BUNDLE_IDENTIFIER)?

标签 ios xcode firebase flutter android-productflavors

我正在使用不同的 Firebase 环境(开发和生产)构建不同风格的 Flutter 应用程序。我需要在 Xcode for iOS 应用程序中为开发和生产设置不同的包 ID。

我使用方案来配置不同的风格(在build设置中,我为每个配置添加环境值)。

但是我对更改有很大的问题$(PRODUCT_BUNDLE_IDENTIFIER) .我需要添加后缀 .development为开发应用程序 ID 的普通应用程序 ID。

我已经尝试关注 this method (使用用户定义的设置)并更改 info.plist从用户定义的设置中获取变量,但它不起作用。

错误是:

The operation couldn’t be completed. Application “$(EXAMPLE_BUNDLE_ID)" is unknown to FrontBoard.



因此,当传入用户定义的设置时,似乎插值不正确。

我也尝试过添加默认 PRODUCT_BUNDLE_IDENTIFIER 的混合方法和用户定义的设置。例如:com.example.app$(EXAMPLE_BUNDLE_ID)在哪里 EXAMPLE_BUNDLE_ID = .development
我也尝试引用用户定义设置$(EXAMPLE_BUNDLE_ID)通过直接将其添加到“Identity”下的 Target General 选项卡中的 Bundle Identifier。但这随后更改为:-- EXAMPLE_BUNDLE_ID-
我也试过info.plist使用 $(PRODUCT_BUNDLE_IDENTIFIER)$(EXAMPLE_BUNDLE_ID)用于捆绑标识符值。但这给出了类似的错误:

The operation couldn’t be completed. Application “com.example.app$(EXAMPLE_BUNDLE_ID)" is unknown to FrontBoard.



这看起来又像插值问题。

有谁知道解决方案?我看过但找不到答案。

这对 android 来说很容易,因为只需使用 applicationIdSuffix ".development”productFlavors .但我无法为 Xcode 找到这样的方法。

最佳答案

需要不同的package name (Android) 和 bundle id (iOS) 因为你需要使用 Firebase Auth插入?

在这种情况下,对于 iOS 项目,您应该考虑使用 PlistBuddy你可以设置它添加一个Run Script在您的 XCode build phases像那样

if [ "${CONFIGURATION}" = "Debug" ]; then
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.example.developmento.appName" "$PROJECT_DIR/Runner/Info.plist"
echo "Changed bundle id for developement $PROJECT_DIR/Runner/Info.plist"
else
echo "Nothing to do"
fi

enter image description here

无论如何,如果你不使用 Firebase Auth ,您可以在不同的 firebase 项目中拥有相同的 bundle id。

如果您需要在暂存和生产之间区分 firebase 项目文件,您可以在这里查看:

How to choose between development and production firebase project based on build flavours?

更新

所以跟随 OP 聊天,知道他在关注这个 tutorial设置 flutter flavors我试着自己看看我们被困在哪里。

起点如下:
  • 两个 Firebase project
  • 使用 Firebase Auth模块(因此需要在项目之间更改捆绑 ID)
  • 当然还有两个不同的GoogleService-Info.plist

  • 我从 Xcode bundle id 开始和 GoogleService-Info.plist设置为生产(只是一个选项)

    enter image description here

    然后我保存了GoogleServices-Info-staging.plistGoogleServices-Info-production.plist保存在我的 ios/Runner 文件夹中

    enter image description here

    然后我在 Compile Sources 的脚本之前设置此构建脚本
    # Type a script or drag a script file from your workspace to insert its path.
    if [ "${CONFIGURATION}" == "Debug" ] || [ "${CONFIGURATION}" == "Debug-Runner-staging" ]; then 
    
    echo "Setting up staging firebase environment"
    /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.example.staging.flutterAppAuthFlavours" "${PROJECT_DIR}/Runner/Info.plist"
    cp -r "${PROJECT_DIR}/Runner/GoogleService-Info-staging.plist" "${PROJECT_DIR}/Runner/GoogleService-Info.plist" 
    echo "$(date) staging flavour - Configuration: ${CONFIGURATION}" > "${PROJECT_DIR}/environment.txt"
    
    elif [ "${CONFIGURATION}" == "Debug-Runner-production" ]; then 
    
    echo "Setting up production firebase environment"
    /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.example.flutterAppAuthFlavours" "${PROJECT_DIR}/Runner/Info.plist" 
    cp -r "${PROJECT_DIR}/Runner/GoogleService-Info-production.plist" "${PROJECT_DIR}/Runner/GoogleService-Info.plist"
    echo "$(date) production flavour - Configuration:  ${CONFIGURATION}" > "${PROJECT_DIR}/environment.txt"
    
    fi
    

    我称之为 Setup Firebase Environment (你可以随心所欲地称呼它)

    enter image description here

    该脚本还将一些日志(带有时间戳)存储在一个名为 environment.txt 的文件中。里面 ios文件夹,以便轻松检查 xcode 构建所做的工作

    enter image description here

    现在关于 SchemesBuild Configurations :

    我已经完成了两个 Build Configuration那是我的 Debug Build Configuration 的精确副本我调用他们

    enter image description here
  • Debug-Runner-staging
  • Debug-Runner-production

  • 经验法则是将构建配置命名为 'Debug-<your flavor>'你需要每种口味都有一个方案你有,所以我有这些:
  • Runner-staging其运行调用 Debug-Runner-staging 构建配置
  • Runner-production其运行调用 Debug-Runner-production 构建配置

  • enter image description here

    enter image description here

    所以现在如果我调用 flutter run --flavor Debug-staging我有一个在我的 staging firebase 项目上运行的构建。

    如果我调用 flutter run --flavor Debug-production我有一个在我的生产 firebase 项目上运行的构建。

    enter image description here

    enter image description here

    更新 2

    为了完整起见,您也可以在这里更改捆绑 ID:

    enter image description here

    反正好像有奇怪的行为一旦你建立了一个 flavour第二次flutter命令正确构建 flavor ,但运行 previos 构建 flavor 。

    XCode 一起构建并且切换方案都按预期工作(即使是正确的应用程序的运行)我猜这可能是一个 flutter 的命令问题。所以我建议你尝试提交问题 here也链接这个 SO 问题/答案。

    更新 3

    经过一点英特尔,我发现 flutter tools在构建项目之前设置应用程序启动环境。所以当我们改变 CFBundleIdentifier里面 Info.plist第一次,第二次我们推出flutter run它采用先前修改的值并尝试在构建期间启动此捆绑 ID,我们正在更改它,因为我们正在构建不同的变体。

    一个可能的解决方案是启动 脚本 改变 CFBundleIdentifier里面 Info.plist在调用 fluetter run 之前.

    例如以 Info.plist 开头生产捆绑包 ID 为 com.example.flutterAppAuthFlavours我们可以做这样的事情

    enter image description here

    enter image description here

    这里我使用了sed命令只是为了想不同,但您可以始终调用我们下面的 PlistBuddy在调用 flutter run 之前进行更改.

    关于ios - 如何在 Xcode 中更改 $(PRODUCT_BUNDLE_IDENTIFIER)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506458/

    相关文章:

    ios - Swift 参数中的默认关键字

    ios - UIDocumentBrowserViewController 与 UIDocumentPickerViewController 有什么区别?

    ios - 将一些日期字符串转换为 NSDate

    xcode - 在 AppDelegate 之外使用 NIB 创建 UITabBarController?

    ios - 在 Swift 中将参数传递给 NSTimer 调用的方法

    javascript - 读取并返回 Firebase 数据库条目,该条目也可用于更新

    java - 致命异常 : main com. firebase.client.FirebaseException:无法弹回键入

    java - 视频连续播放 firebase

    ios - App Store连接。禁用新版本应用的应用内购买

    android - Google map 或地点 : API to fetch bulk uploaded (and verified) locations