xcode 从不同的目标获取 Bundle ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25794613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:32:06  来源:igfitidea点击:

Get Bundle ID from a different target

iosobjective-cxcodeios-app-extensionxcode-build-settings

提问by djibouti33

In my Xcode project, I have 4 different schemes (prod, dev, staging, staging2) which changes the bundle identifier of the app that gets installed to the phone. That way I can have all 4 versions installed side by side, similar to this technique.

在我的 Xcode 项目中,我有 4 个不同的方案(prod、dev、staging、staging2),它们更改了安装到手机的应用程序的包标识符。这样我就可以并排安装所有 4 个版本,类似于这种技术

Now I'm building a Today Extension for iOS 8. It's a new Target requiring its own bundle identifier.

现在我正在为 iOS 8 构建一个 Today Extension。它是一个新的 Target,需要它自己的包标识符。

The Extension's Bundle Identifier must be prefixed with the Parent App's Bundle Identifier, or a warning is thrown:

扩展的捆绑标识符必须以父应用程序的捆绑标识符为前缀,否则会抛出警告:

error: Embedded binary's bundle identifier is not prefixed with the parent app's bundle identifier.

    Embedded Binary Bundle Identifier:  com.company.app.TodayExtension
    Parent App Bundle Identifier:       com.company.app.staging

In this case, the Embedded Binary Bundle Identifier must be com.company.app.staging.TodayExtension.

在这种情况下,嵌入式二进制包标识符必须是com.company.app.staging.TodayExtension

Is there a way I can configure my Today Extension target to set its bundle identifier by first looking at the Parent target's bundle identifier? Perhaps through a custom Run Script Build Phase?

有没有办法通过首先查看父目标的包标识符来配置我的 Today Extension 目标来设置它的包标识符?也许通过自定义运行脚本构建阶段?

回答by Alexis

The build settings of the targets are not available for the other targets. However the build settings of the PROJECT are available.

目标的构建设置不可用于其他目标。但是,项目的构建设置可用。

You can thus add a user-defined build setting in your project and call it PARENT_APP_BUNDLE_IDENTIFIER and set the correct bundle id for all your schemes.

因此,您可以在项目中添加用户定义的构建设置并将其命名为 PARENT_APP_BUNDLE_IDENTIFIER 并为所有方案设置正确的包 ID。

Then in the info tab of the app extension target set the bundle id to

然后在应用程序扩展目标的信息选项卡中将包 ID 设置为

$(PARENT_APP_BUNDLE_IDENTIFIER).$(PRODUCT_NAME:rfc1034identifier)

It worked perfectly fine for me.

它对我来说非常好。

回答by Mike Makhovyk

I have Siri extension and multiple targets, so to avoid duplicating extension for every target I added pre-action to every scheme that changes the BundleId of the extension's plist before build:

我有 Siri 扩展和多个目标,因此为了避免为每个目标重复扩展,我在构建之前将预操作添加到每个更改扩展 plist 的 BundleId 的方案中:

  1. Select 'Edit scheme'
  2. Click triangle near 'Build'
  3. Choose 'Pre-actions'
  4. Click '+', choose 'New Run Script Action'
  5. Choose target to provide build settings from
  6. In script field paste following with your BundleId and extension folder
  1. 选择“编辑方案”
  2. 单击“构建”附近的三角形
  3. 选择“预先行动”
  4. 单击“+”,选择“新建运行脚本操作”
  5. 选择目标以提供构建设置
  6. 在脚本字段中粘贴您的 BundleId 和扩展文件夹
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier 
YourDesiredBundleId" "$PROJECT_DIR/YourExtensionFolder/Info.plist"

And it works like a charm! You need to configure it for every scheme.

它就像一个魅力!您需要为每个方案配置它。

回答by Damian Rzeszot

In my project I need to build different versions of apps (differ in details, e.g. each application is branded with a different logo).

在我的项目中,我需要构建不同版本的应用程序(细节不同,例如每个应用程序都带有不同的标志)。

Let's say there is about 10 "app" targets, I can't imagine adding Notification Content and Notification Service extensions per each main target (in this case I would maintaining 30 targets in total - madness).

假设有大约 10 个“应用程序”目标,我无法想象为每个主要目标添加通知内容和通知服务扩展(在这种情况下,我总共会维护 30 个目标 - 疯狂)。

I run a script (https://gist.github.com/damian-rzeszot/0b23ad87e5ab5d52aa15c095cbf43c59) after "Embed App Extensions" phase. It overrides bundle id in app extension plists and re-signs the bundle.

我在“嵌入应用程序扩展”阶段后运行一个脚本(https://gist.github.com/damian-rzeszot/0b23ad87e5ab5d52aa15c095cbf43c59)。它覆盖应用程序扩展 plist 中的包 ID 并重新签名包。