[iOS] Question - Share extension for multiple targets
I have 2 targets for the app: release and preview. Is there the ability to dynamically specify bundle IDs and app groups, so a single share extension works with both targets?
@b4lk0n did you find a solution?
My team ran into a similar issue, however our issue was for multiple build configurations, not multiple targets. Leaving this solution here, as it might help with your situation as well. The two targets we had were our main App target, and the ShareExtension target.
Note: screenshots aren't from my team's project, all were found on Google. I'm just trying to show the general locations of where the settings are.
Build Configurations
The following will vary project to project, but let's assume you have 3 different build configurations:
- Dev
- Sandbox
- Release
Build Settings - User Defined Settings
You need to add 2 user-defined settings at the Project level, APP_BUNDLE_IDENTIFER and SHARE_MENU_SCHEME. Click on your Project, click the workspace, then go to "Build Settings" tab. Tap the "+" and add the settings. Values must be unique for each build configuration.
This screenshot shows the Build Settings of a Target, but you want to go to the Build Settings of the Project (which all targets can reference):

APP_BUNDLE_IDENTIFIER:
- Dev: com.company.AppDev
- Sandbox: com.company.AppSandbox
- Release: com.company.App: com.company.App
SHARE_MENU_SCHEME
- Dev: ShareMenuModuleDev
- Sandbox: ShareMenuModuleSandbox
- Release: ShareMenuModule
Info.plist
Add these to your ShareExtension target in ShareExtension/Info.plist:
<key>HostAppBundleIdentifier</key>
<string>$(APP_BUNDLE_IDENTIFIER)</string>
<key>HostAppURLScheme</key>
<string>$(SHARE_MENU_SCHEME)://</string>
Add this dict to your App target's CFBundleURLTypes in App/Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>$(SHARE_MENU_SCHEME)</string>
</array>
</dict>
...
</array>
App Group
Create an App Group if you haven't already. Go to each Target and under "Signing & Capabilities", ensure all targets are in the same App Group.

Then go to the .entitlements files (it's in the root directory of your targets - you won't be able to see this from Xcode UI, you'll have to find with a text editor). This file is where the App Group value is kept. You'll want to replace with the following:
Target/Target.entitlements
<key>com.apple.security.application-groups</key>
<array>
<string>group.$(APP_BUNDLE_IDENTIFIER)</string>
</array>
ShareExtension/ShareExtension.entitlements
<key>com.apple.security.application-groups</key>
<array>
<string>group.$(APP_BUNDLE_IDENTIFIER)</string>
</array>
This ensures that the App Group is consistent for all the targets.
This took quite a while to figure out. Hope it helps someone in the future!