1import { ExpoConfig } from 'expo/config';
2import { ConfigPlugin, withEntitlementsPlist } from 'expo/config-plugins';
3
4export type IosProps = {
5  /**
6   * Sets the `com.apple.developer.icloud-container-environment` entitlement which is read by EAS CLI to set
7   * the `iCloudContainerEnvironment` in the `xcodebuild` `exportOptionsPlist`.
8   *
9   * Available options: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_icloud-container-environment
10   */
11  iCloudContainerEnvironment?: 'Development' | 'Production';
12};
13
14export const withDocumentPickerIOS: ConfigPlugin<IosProps> = (
15  config,
16  { iCloudContainerEnvironment } = {}
17) => {
18  return withEntitlementsPlist(config, (config) => {
19    config.modResults = setICloudEntitlements(
20      config,
21      { iCloudContainerEnvironment },
22      config.modResults
23    );
24    return config;
25  });
26};
27
28export function setICloudEntitlements(
29  config: Pick<ExpoConfig, 'ios'>,
30  { iCloudContainerEnvironment }: IosProps,
31  { 'com.apple.developer.icloud-container-environment': _env, ...entitlements }: Record<string, any>
32): Record<string, any> {
33  if (config.ios?.usesIcloudStorage) {
34    // Used for AdHoc iOS builds: https://github.com/expo/eas-cli/issues/693
35    // https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_icloud-container-environment
36    entitlements['com.apple.developer.icloud-container-environment'] = iCloudContainerEnvironment;
37
38    entitlements['com.apple.developer.icloud-container-identifiers'] = [
39      `iCloud.${config.ios.bundleIdentifier}`,
40    ];
41    entitlements['com.apple.developer.ubiquity-container-identifiers'] = [
42      `iCloud.${config.ios.bundleIdentifier}`,
43    ];
44    entitlements[
45      'com.apple.developer.ubiquity-kvstore-identifier'
46    ] = `$(TeamIdentifierPrefix)${config.ios.bundleIdentifier}`;
47
48    entitlements['com.apple.developer.icloud-services'] = ['CloudDocuments'];
49  }
50  return entitlements;
51}
52