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