Expensify / react-native-share-menu

A module for React Native that adds your app to the share menu of the device
MIT License
654 stars 239 forks source link

[iOS] Question - Share extension for multiple targets #201

Open b4lk0n opened 2 years ago

b4lk0n commented 2 years ago

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?

frozencap commented 2 years ago

@b4lk0n did you find a solution?

gregbrinker commented 1 year ago

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:

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): image

APP_BUNDLE_IDENTIFIER:

SHARE_MENU_SCHEME

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.

image

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!